Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Button to send to console or to source #133

Open
mine-cetinkaya-rundel opened this issue Jul 13, 2023 · 2 comments
Open
Labels
feature a feature request or enhancement

Comments

@mine-cetinkaya-rundel
Copy link

The RStudio History pane has two buttons: To Console and To Source.

I wonder if these could be implemented instead of or in addition to the Copy button in gptstudio, so instead of copy-paste, one can easily send the code over.

An alternative or additional functionality could be what happens in the RStudio Help pane, where you can highlight a piece of code and Command+Return to send it to the Console.

@calderonsamuel
Copy link
Collaborator

Thank you for the suggestion! Btw, your OpenIntro IMS1 book is the one I tend to recommend the most on the topic! For now you can achieve this if instead of using the chat app you use the "ChatGPT in source" addin with a text selection. This works in both source and console, but is clearly not that nice to iterate for progressive better output as in the chat app.

For the developers: The To console and To source buttons might be possible if the background job process can talk to {rstudioapi} or an environment that can be reached by the global environment. Sadly, I think Command + Return is not possible to implement until RStudio allows to overwrite that shortcut in the viewer pane. Currently it will always send the current line (or selection) in the source document to console, even if you are not focused in it.

@JamesHWade JamesHWade added the feature a feature request or enhancement label Jul 15, 2023
@calderonsamuel
Copy link
Collaborator

An update on this. It is still not possible to send text to console or source when a background job has access to a pointer to the global environment, because a background job is actually a different system process.

However, I can think of three alternatives:

Access the R process

Somehow, be able to run code in the same process of the RStudio IDE's R session. The {processx} or {ps} packages might provide some methods for this. I think this could be very difficult as it mostly sounds like "hacking a terminal", which doesn't sound easy or even maintainable.

Use a lower level Rstudio API

rstudioapi::insertText() is a high level wrapper of these lines in the RStudio source code:

https://github.com/rstudio/rstudio/blob/4ce3c93ef1c1b2c277cce08383673b917b37d09e/src/cpp/r/R/Api.R#L1194-L1215

.rs.addApiFunction("selectionSet", function(value = NULL, id = NULL)
{
   # collapse value into single string
   value <- paste(value, collapse = "\n")
   
   # create data payload
   payload <- list(
      value  = .rs.scalar(value),
      doc_id = .rs.scalar(id)
   )
   
   # create request
   request <- .rs.api.createRequest(
      type    = .rs.api.eventTypes$TYPE_SET_EDITOR_SELECTION,
      sync    = TRUE,
      target  = .rs.api.eventTargets$TYPE_ACTIVE_WINDOW,
      payload = payload
   )
   
   # fire away
   .rs.api.sendRequest(request)
})

Which is itself a wrapper on a C++ function... Which might be another wrapper...

But in the end, we will get to the point where we will find an API that can receive requests (hopefully without authentication), and we could be able to handle that.

Escape R

Rstudio is built with Electron.js, and in the end is just a custom browser application. I assume we can take advantage of that and target a) an Electron API, or b) target element id.

Something really funny happens when you open Rstudio -> Help -> Diagnostics -> Toggle Developer Tools, and run the following in the console:

document.getElementsByClassName("ace_cursor")[0].innerText = "test"

Or ... maybe we should just block the interactive R session? 🤣

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants