【问题标题】:RStudio: Alternative hotkey to source selected part without echoRStudio:替代热键源选择的部分没有回声
【发布时间】:2018-01-17 10:46:59
【问题描述】:

在 RStudio 中,[Ctrl] + [Enter] 运行当前突出显示的代码部分,但带有回显。 [Ctrl] [Shift] + [S] 获取整个文件而不回显。

是否可以运行突出显示/选定的代码部分而不会让控制台输入混乱?或者这是运行代码而不是采购时的隐含要求? (其他SO帖子中似乎提到了细微的差异)

结论:是否有一个热键可以按下来获得 [Ctrl]+[Enter] 的确切作用,但又不会让控制台与我的脚本代码混淆?

【问题讨论】:

  • 听起来你应该使用 notebook/rmarkdown 格式来获得你想要的东西

标签: r rstudio


【解决方案1】:

我不相信有内置的方法可以做到这一点。但是,您可以创建一个插件,将source() 代码和sink() 结果生成一个文本文件,该文件可以自动打开。

在 RStudio 中,创建一个新项目 > R 包。

创建一个名为 runSelected 的新 R 脚本:

#' runSelected
#'
#' RStudio addin to run selected code and show results in 
#' default text editor.
#'
#' @export
runSelection <- function() {
  context <- rstudioapi::getActiveDocumentContext()
  selection <- rstudioapi::primary_selection(context)
  tmp_code <- tempfile()
  f <- file(tmp_code)
  writeChar(object = selection$text, con = f)
  close(f)

  tmp_results <- tempfile()
  sink(tmp_results)
  source(tmp_code)
  sink()
  shell.exec(tmp_results)
}

在包的目录下,创建文件inst/rstudio/addins.dcf:

Name: Run Selection
Description: Run selected text and see results in text editor
Binding: runSelection
Interactive: false

使用roxygen2 生成文档,构建并瞧瞧!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-09
    • 2021-01-30
    • 2016-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-19
    • 1970-01-01
    相关资源
    最近更新 更多