rstudio / rstudioapi

Safely access RStudio's API (when available)
http://rstudio.github.io/rstudioapi
Other
170 stars 38 forks source link

Method for getting R console contents / buffer? #160

Open dwbapst opened 5 years ago

dwbapst commented 5 years ago

From what I can tell, the methods for getting the terminal buffers don't extend to their neighbor in RStudio, the R console itself. Thus if I want to do something like pull out the full text of the console (the R header, the input history, the output, the error messages, all of it) I can't presently do that.

(Well, not without hacking the HTML that powers RStudio apart...)

kevinushey commented 5 years ago

You can use:

rstudioapi:::getConsoleEditorContext()

to get the 'editor context' associated with the R console. Is that sufficient?

dwbapst commented 5 years ago

Not at all?

See, let's say my console currently looks something like:


R version 3.6.1 (2019-07-05) -- "Action of the Toes"
Copyright (C) 2019 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> sal_data <- rnorm(48)+runif(48)
> sum(sal_data/1:3)
[1] 14.69943
Warning message:
In sal_data/1:3 :
  longer object length is not a multiple of shorter object length

I want all that. All of it. Which is pretty similar to what I get if I pull the buffer from a terminal... that isn't the R console.

For example, if my terminal is like so:

dwbapst@nemagraptus:~$ ls | grep workspace
workspace

I get back such from terminalBuffer:

> rstudioapi::terminalBuffer(id = rstudioapi::terminalList())
[1] "ls | grep workspace"     "workspace"               "dwbapst@nemagraptus:~$ "
> 

Note that giving the id of the console using the functions for documents doesn't work either:

> rstudioapi::terminalBuffer(id = rstudioapi::getConsoleEditorContext()$id)
Error in f(...) : Unknown terminal identifier '#console'

I could call documentSave on the console id, but it doesn't seem to save anything:

> consoleID <- rstudioapi::getConsoleEditorContext()$id
> rstudioapi::documentSave(id = consoleID)
[1] TRUE

...despite returning TRUE.

Am I missing something? is there some other function I'm overlooking?

kevinushey commented 5 years ago

Got it. No, you aren't missing anything -- the functionality you want just is not implemented yet.

DesiQuintans commented 1 year ago

Hello, is this functionality present yet? I was interested in making an Addin that copies the last output from the console and pastes it at the cursor position as a comment.

Edit: Didn't need it after all! Grabbing .Last.value was enough in most cases: https://github.com/DesiQuintans/carboncopy/