Strange error when using RStudio addin with rstudioapi::setCursorPosition for the first time in a given session
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
Research direction
Start with the knitrdata call in R/rstudio_addins.R around line 313 and the stripped-down sequence using getSourceEditorContext, insertText, and setCursorPosition. Reproduce the first-use failure in the listed RStudio, R, and rstudioapi versions, comparing the varying tracebacks. Done means establishing whether the first-session sequence triggers an rstudioapi defect and documenting a confirmed workaround or fix.
Written by the indexing model from the issue text.
Description
I have been encountering a strange error when using a rstudio addin that I created that I am not sure even how to begin to debug and that would seem to be a rstudioapi bug related to the setCursorPosition function. The addin is the Insert filled data chunk addin of the knitrdata package (driven by the function knitrdata::insert_data_chunk_dialog). The basic functioning of the addin is that you select some options and when you hit the Create Chunk button it inserts a chunk of text into the active document in the RStudio editor. The first and only the first time I use the addin in a given RStudio session, the addin returns an error at the point where it uses rstudioapi::setCursorPosition here. In addition to the fact that this happens only the first time the addin is used (subsequent calls produce no error), the error is strange because it seems to happen in random different places in the process of setting the cursor position (i.e., the traceback is not always the same each time I generate the error even if all the steps I carried out to produce it are the same). It is as if there are several asynchronous operations being carried out, and which one runs into the problem varies slightly from run to run. I have looked at the tracebacks and at least the original setCursorPosition call seems correct (i.e., the position indicated is a valid position in the active document). At the end of this message I include a couple of screenshots showing a couple examples of the errors that can be produced.
The essential code (stripped of unnecessary code) related to rstudioapi that seems to be causing the problem is:
# Active document stuff
context = rstudioapi::getSourceEditorContext()
ln = context$selection[[1]]$range$start["row"]
dp = rstudioapi::document_position(ln,1) # position object
# Run shiny app for addin
chunk = create_data_chunk_dialog(title=title,infobar=infobar)
# Insert text
rstudioapi::insertText(dp,paste0(chunk,platform.newline(),collapse=""),context$id)
# Set position - sometimes causes errors for some unknown reason
rstudioapi::setCursorPosition(dp,context$id)
The questions I have are:
- Is this bug my fault in some way or is this a
rstudioapiproblem? - Is there a fix or workaround?
- If not, how would I go about debugging this problem? As it happens differently each time and only happens the first time I launch the addin, I am at a loss of what to do.
My RStudio installation is as follows:
- Ubuntu 20.04 focal
- RStudio Version 1.3.1056
- R 4.0.3
- rstudioapi 0.13
- knitrdata 0.6.0
Screenshots



- Dominant language
- R
- Stars
- 175
- Forks
- 37
- PR merge metrics
- No merged PRs in 30d
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from rstudio/rstudioapi
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
rstudio/rstudioapi#313 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 48/100
rstudio/rstudioapi#321 · 1 comment ·
-
Difficulty 4/5 3-5 days Newbie friendliness 45/100
rstudio/rstudioapi#318 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
rstudio/rstudioapi#315 ·
-
Difficulty 3/5 1-2 days Newbie friendliness 35/100
rstudio/rstudioapi#314 ·
All issues in rstudio/rstudioapi
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
r-lib/pkgdepends#485 · 3 comments ·
-
Difficulty 1/5 Under an hour Newbie friendliness 92/100
-
beginners blocker
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
enviPathR OpenBuild Error Build OK Build Warning policies-accepted pre-review precheck-passed
Difficulty 1/5 Under an hour Newbie friendliness 84/100
Bioconductor/BiocContributions#207 · 6 comments ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
datacarpentry/semester-biology#1255 ·