Reputation: 19025
In RStudio IDE, is there a way to Run Selection of code that submits all lines of the selection simultaneously (as in Run All, but for a selection) rather than sequentially, thus preserving the interactive nature of the menu()
command?
Background
I have a function definition followed by some commands, similar to:
f1 <- function(x){
if( x == 'questionable_value'){
if( menu( title = 'Detected a questionable value',
choices = c('[Abort process]',
'Continue with questionable value (not recommended)')
) %in% c(0L,1L) ){
stop('Process stopped by user.')
} else warning('Questionable value ignored by user.')
}
return(paste('Did something with',x))
}
f1('foo')
f1('questionable_value')
f1('bar')
Running the script (e.g., in Windows RStudio IDE, Run All or Ctrl-Alt-R), works as expected...
Run All console output (works)
> source('~/.active-rstudio-document', echo=TRUE)
> f1 <- function(x){
+ if( x == 'questionable_value'){
+ if( menu( title = 'Detected a questionable value',
+ choices = c('[Abort .... [TRUNCATED]
> f1('foo')
[1] "Did something with foo"
> f1('questionable_value')
Detected a questionable value
1: [Abort process]
2: Continue with questionable value (not recommended)
if the user enters 2, then:
Selection: 2
[1] "Did something with questionable_value"
> f1('bar')
[1] "Did something with bar"
Warning message:
In f1("questionable_value") : Questionable value ignored by user.
which is what I want.
The problem occurs when I am running a selection (e.g., Ctrl-Enter, or clicking the Run icon) -- even if that selection is the entire R file.
Run Selection console output (doesn't work)
> f1 <- function(x){
+ if( x == 'questionable_value'){
+ if( menu( title = 'Detected a questionable value',
+ choices = c('[Abort process]',
+ 'Continue with questionable value (not recommended)')
+ ) %in% c(0L,1L) ){
+ stop('Process stopped by user.')
+ } else warning('Questionable value ignored by user.')
+ }
+ return(paste('Did something with',x))
+ }
> f1('foo')
[1] "Did something with foo"
> f1('questionable_value')
Detected a questionable value
1: [Abort process]
2: Continue with questionable value (not recommended)
Selection: f1('bar')
Enter an item from the menu, or 0 to exit
Selection:
In the case of Run Selection, the menu()
is not waiting for user input but is instead pulling in the next line of the script ("f1('bar')"
) as the Selection
.
Upvotes: 1
Views: 1108
Reputation: 44997
RStudio does the same thing as the standard R front-ends here: "Run Selection" copies the selected text, and pastes it into the console.
To get what you want, you need to copy the selected text, and source from the clipboard. Unfortunately, this isn't all that easy, but here's some code to help:
readClipboard <- function() {
if (.Platform$OS.type == "windows")
lines <- readLines("clipboard")
else
lines <- system("pbpaste", intern=TRUE)
lines
}
This function works on Windows and other systems that have a pbpaste command. It's built-in on MacOS, and there are instructions for simulating it on Linux here: https://whereswalden.com/2009/10/23/pbcopy-and-pbpaste-for-linux/.
Then to source the selected text, you need to copy it (Ctrl-C) and run
source(textConnection(readClipboard()))
Since RStudio has an API and installable commands (see https://rstudio.github.io/rstudioaddins/), you can probably put all of this (or equivalent) into code that is run automatically on a keystroke. Here's a mostly untested version of that:
library(rstudioapi)
selection <- primary_selection(getSourceEditorContext())$text
source(textConnection(selection))
Upvotes: 2