Previous to updating to Rstudio Version 2022.12.0+353 (2022.12.0+353) I was able to link to code scripts without a problem from within the RStudio window. Now I’m getting ‘No linkable item in RStudio’.
I am, however, able to link to the script from Finder, which is a good workaround for the time being.
The latest Rstudio is an electron which does not support AppleScript any more. Hookmark’s current script is unable to get the file url by running the following command in AppleScript:
cmd “clipr::write_clip( rstudioapi::getActiveDocumentContext()$path)”
I have been trying to figure out some way to run this R command but no luck yet.
I’ve looked through the humungous RStudio release notes and could not find a clear indication of the electron transition date. Last May it they referred in passing to “our upcoming Electron version”.
The Electron-based IDE will replace the QtWebEngine version entirely in a future release, but for now is considered experimental and should not be used for production work. Most users should continue using the QtWebEngine-based desktop release.
are you using a preview, or the official version?
Current script relies on rstudioapi and clipr packages. Presumably in electron version new packages will be provided. One of us here should (and RStudio enthusiasts can) look into what APIs are being developed / ported to electron.
All Hookmark needs is the path of current document, then Hookmark can work out the rest (document name, opening the file).
I don’t know what the ticketing system is for RStudio but that will be a path if they don’t have an API yet.
RStudio has an API. See Safely Access the RStudio API • rstudioapi. Could the API help? I’m willing to spend a bit of time on making this work, as I do a LOT of work in RStudio these days.
In fact, I just tried the API in the latest version of RStudio (2023.06.0+421), and it seems to work. For example, I can execute the following at the RStudio command prompt:
The return value (a string) gives the path to the document I was editing prior to executing the API call, namely
[1] “~/github/matsbyname/R/aggregates.R”
Is that enough information to enable Hookmark to create a 2-way link?
There are other API calls to get selected text [primary_selection()], to create a new document [documentNew()], to add text to the new document [insertText()], etc. The API looks to be quite full-featured.
Note that these are R language API calls, but one would not be using RStudio if one did not have the R language runtime installed.
To answer your question, it is possible to run that command from the terminal. One can run any R command using the Rscript command on the command line. For example
% Rscript -e “getwd()”
[1] “/Users/mkh2”
Unfortunately, the response to an rstudioapi:: call is
% Rscript -e “rstudioapi::getActiveDocumentContext()$path”
Error: RStudio not running
Execution halted
because the Terminal isn’t RStudio.
Barring Electron supporting AppleScript, we need a way to get a command into RStudio from the outside world. Anybody have any ideas?
All the documentation about the rstudioapi package says “This package contains a handful of useful wrapper functions to access the API,” which seems to imply that there is another way to access the API outside of the rstudioapi functions. I was unable to find documentation for another way to access the RStudio API from outside the app. But digging a little further into the RStudio source code at GitHub - rstudio/rstudio: RStudio is an integrated development environment (IDE) for R, I see this file:
Perhaps this is a C++ API for the app? Perhaps this is a way in from the outside? I note that line 391 includes the code for getActiveDocumentContext and getLastActiveEditorContext which now supersedes.
I’m out of my depth now, but maybe this helps someone else?