【问题标题】:Run Selection of code interactively以交互方式运行代码选择
【发布时间】:2018-03-17 11:06:31
【问题描述】:

在 RStudio IDE 中,有没有一种方法来运行代码选择,它同时提交选择的所有行(如在全部运行中,但用于选择)而不是按顺序提交,从而保留 menu() 命令的交互性质?

背景

我有一个函数定义,后面跟着一些命令,类似于:

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')

运行脚本(例如,在 Windows RStudio IDE 中,运行全部或 Ctrl-Alt-R),按预期工作...

运行所有控制台输出(有效)

> 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)

如果用户输入2,那么:

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.

这就是我想要的。

问题在我运行选择时出现(例如,Ctrl-Enter,或单击“运行”图标)——即使该选择是整个 R 文件.

运行选择控制台输出(不起作用)

> 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: 

在运行选择的情况下,menu() 不等待用户输入,而是将脚本的下一行 ("f1('bar')") 作为Selection 拉入。

【问题讨论】:

  • RStudio 版本 1.0.153(截至发帖时最新)

标签: r rstudio


【解决方案1】:

RStudio 与此处的标准 R 前端执行相同的操作:“运行选择”复制选定的文本,并将其粘贴到控制台中。

要获得所需的内容,您需要复制选定的文本,并从剪贴板获取源代码。不幸的是,这并不是那么容易,但这里有一些代码可以提供帮助:

readClipboard <- function() {
  if (.Platform$OS.type == "windows") 
    lines <- readLines("clipboard")
  else
    lines <- system("pbpaste", intern=TRUE)
  lines
}

此功能适用于 Windows 和其他具有 pbpaste 命令的系统。它是 MacOS 内置的,这里有在 Linux 上模拟它的说明:https://whereswalden.com/2009/10/23/pbcopy-and-pbpaste-for-linux/

然后要获取所选文本,您需要复制它(Ctrl-C)并运行

source(textConnection(readClipboard()))

由于 RStudio 有一个 API 和可安装的命令(请参阅 https://rstudio.github.io/rstudioaddins/),您可能可以将所有这些(或等效)放入在击键时自动运行的代码中。 这是一个几乎未经测试的版本:

library(rstudioapi)
selection <- primary_selection(getSourceEditorContext())$text
source(textConnection(selection))

【讨论】:

    猜你喜欢
    • 2016-09-10
    • 2012-02-28
    • 2021-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多