Kon*_*lph 13 r read-eval-print-loop
我的R工作流通常是这样的,我打开一个文件,我输入R命令,我想在一个单独打开的R shell中执行这些命令.
最简单的方法是source('the-file.r')在R里面说.然而,这总是重新加载整个文件,如果处理大量数据可能需要相当长的时间.它还要求我再次指定文件名.
理想情况下,我只想从文件中获取特定行(或行)(我正在使用复制和粘贴不起作用的终端).
source似乎没有提供此功能.有没有另一种方法来实现这一目标?
Mat*_*rde 15
这是R的另一种方式:
source2 <- function(file, start, end, ...) {
file.lines <- scan(file, what=character(), skip=start-1, nlines=end-start+1, sep='\n')
file.lines.collapsed <- paste(file.lines, collapse='\n')
source(textConnection(file.lines.collapsed), ...)
}
Run Code Online (Sandbox Code Playgroud)
正如评论中所讨论的,真正的解决方案是使用允许获取文件特定部分的 IDE。现有的解决方案有很多:
对于Vim,有Nvim-R。
对于Emacs,有ESS。
当然还有出色的独立RStudio IDE。
需要特别指出的是,上述所有解决方案都可以在本地和服务器上运行(例如通过 SSH 连接访问)。R 甚至可以在 HPC 集群上运行——如果设置正确,它仍然可以与 IDE 通信。
如果无论出于何种原因,上述解决方案都不起作用,这里有一个小模块[gist]可以完成这项工作。不过,我通常不建议使用它。1
#' (Re-)source parts of a file
#'
#' \code{rs} loads, parses and executes parts of a file as if entered into the R
#' console directly (but without implicit echoing).
#'
#' @param filename character string of the filename to read from. If missing,
#' use the last-read filename.
#' @param from first line to parse.
#' @param to last line to parse.
#' @return the value of the last evaluated expression in the source file.
#'
#' @details If both \code{from} and \code{to} are missing, the default is to
#' read the whole file.
rs = local({
last_file = NULL
function (filename, from, to = if (missing(from)) -1 else from) {
if (missing(filename)) filename = last_file
stopifnot(! is.null(filename))
stopifnot(is.character(filename))
force(to)
if (missing(from)) from = 1
source_lines = scan(filename, what = character(), sep = '\n',
skip = from - 1, n = to - from + 1,
encoding = 'UTF-8', quiet = TRUE)
result = withVisible(eval.parent(parse(text = source_lines)))
last_file <<- filename # Only save filename once successfully sourced.
if (result$visible) result$value else invisible(result$value)
}
})
Run Code Online (Sandbox Code Playgroud)
用法示例:
# Source the whole file:
rs('some_file.r')
# Re-soure everything (same file):
rs()
# Re-source just the fifth line:
rs(from = 5)
# Re-source lines 5–10
rs(from = 5, to = 10)
# Re-source everything up until line 7:
rs(to = 7)
Run Code Online (Sandbox Code Playgroud)
1有趣的故事:我最近发现自己在一个配置混乱的集群上,无法安装所需的软件,但由于迫在眉睫的截止日期,迫切需要调试 R 工作流。我真的别无选择,只能手动将 R 代码行复制并粘贴到控制台中。在这种情况下,上述内容可能会派上用场。是的,这确实发生了。