仅来源文件的一部分 [英] Source only part of a file

查看:50
本文介绍了仅来源文件的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 R 工作流程通常是这样的:我打开一个文件,在其中键入 R 命令,我想在单独打开的 R shell 中执行这些命令.

My R workflow is usually such that I have a file open into which I type R commands, and I’d like to execute those commands in a separately opened R shell.

最简单的方法是在 R 中说 source('the-file.r').但是,这总是重新加载整个文件,如果大量数据,这可能需要相当长的时间被处理.它还需要我再次指定文件名.

The easiest way of doing this is to say source('the-file.r') inside R. However, this always reloads the whole file which may take considerable time if big amounts of data are processed. It also requires me to specify the filename again.

理想情况下,我只想从文件中获取特定的一行(或几行)(我正在使用复制和粘贴不起作用的终端).

Ideally, I’d like to source only a specific line (or lines) from the file (I’m working on a terminal where copy&paste doesn’t work).

source 似乎没有提供这个功能.还有其他方法可以实现吗?

source doesn’t seem to offer this functionality. Is there another way of achieving this?

推荐答案

使用正确的工具来完成工作……

正如评论中所讨论的,真正的解决方案是使用允许获取文件特定部分的 IDE.现有的解决方案有很多:

Using the right tool for the job …

As discussed in the comments, the real solution is to use an IDE that allows sourcing specific parts of a file. There are many existing solutions:

对于 Emacs,有 ESS.

For Emacs, there’s ESS.

当然还有出色的独立 RStudio IDE.

And of course there’s the excellent stand-alone RStudio IDE.

需要特别注意的是,上述所有解决方案都可以在本地和服务器上运行(例如通过 SSH 连接访问).R 甚至可以在 HPC 集群上运行 - 如果设置正确,它仍然可以与 IDE 通信.

As a special point of note, all of the above solutions work both locally and on a server (accessed via an SSH connection, say). R can even be run on an HPC cluster — it can still communicate with the IDEs if set up properly.

如果,无论出于何种原因,上述解决方案都不起作用,这里是一个小模块[gist] 可以完成这项工作.不过,我通常不建议使用它.1

If, for whatever reason, none of the solutions above work, here’s a small module[gist] that can do the job. I generally don’t recommend using it, though.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)
    }
})

使用示例:

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

<小时>

1 有趣的故事:我最近发现自己在一个配置混乱的集群上,无法安装所需的软件,但由于截止日期迫在眉睫,迫切需要调试 R 工作流.我真的别无选择,只能手动将 R 代码行复制并粘贴到控制台中.这种是上述可能派上用场的情况.是的,这确实发生了.


1 Funny story: I recently found myself on a cluster with a messed-up configuration that made it impossible to install the required software, but desperately needing to debug an R workflow due to a looming deadline. I literally had no choice but to copy and paste lines of R code into the console manually. This is a situation in which the above might come in handy. And yes, that actually happened.

这篇关于仅来源文件的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆