Strange error when using RStudio addin with rstudioapi::setCursorPosition for the first time in a given session

Open
#211 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
r
Domain
api

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:

  1. Is this bug my fault in some way or is this a rstudioapi problem?
  2. Is there a fix or workaround?
  3. 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

Screenshot from 2020-12-07 11-41-44
Screenshot from 2020-12-07 11-57-05
Screenshot from 2020-12-07 11-58-28

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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from rstudio/rstudioapi

All issues in rstudio/rstudioapi

Similar issues

More R issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.