Open dmkaplan2000 opened 3 years ago
First, this bug apparently has nothing specific to do with rstudioapi::setCursorPosition
as commenting out this line just moves the error to another line of the addin function (such as the rstudioapi::insertText
line).
Second, I have found something of a solution to this bug by moving all rstudioapi
related code into the Shiny server function itself. After doing this, I no longer receive the error. I would still like to understand why this change fixes the problem as doing this is not ideal as it limits some ways I would have liked to use the function and I see no clear reason why this should fix the problem.
First thing I would ask: do you see this issue with the preview release of RStudio, alongside the development version of rstudioapi
?
That said, some of rstudioapi
's operations are not synchronous (and unfortunately we do not document which operations are synchronous, and which are not, which is a somewhat severe limitation ...) which could be responsible for the behavior you're seeing.
A simple way of testing that hypothesis would be to add a short Sys.sleep(0.1)
after API calls, to see if that resolves the issue.
After a lot of playing around, I think I have an idea where this problem is coming from. Changing to the development release of RStudio made the problem go away, but when I reverted to rstudio 1.3, I couldn't make the error occur again, which made me think that the error had something to do with the way I was using RStudio. After a lot of tests, it seems to be caused by the 'Install and restart'
button of the 'Build'
tab in RStudio. If I open the project for my package in RStudio and use that button, the error appears indefinitely when I try to use the addin. If, however, I install the package from a tar.gz or from CRAN, it goes away. Updating to rstudio development cures the problems in all cases.
Also, the solution of moving the rstudioapi
code into the Shiny app seems to be more bandade than solution. When I do that, the addin works, but it seems to be primarily because RStudio doesn't react to the error as in the command window I get a message about an "interrupt" occurring:
> knitrdata:::insert_data_chunk_dialog()
Loading required package: shiny
Listening on http://127.0.0.1:3694
later: interrupt occurred while executing callback.
Also, I tried the solution of adding sleep commands and that moved the error onto those sleep commands. My impression is that the error is going to happen, but where and when are somewhat random.
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 thesetCursorPosition
function. The addin is theInsert filled data chunk
addin of the knitrdata package (driven by the functionknitrdata::insert_data_chunk_dialog
). The basic functioning of the addin is that you select some options and when you hit theCreate 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 usesrstudioapi::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 originalsetCursorPosition
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: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:
Screenshots