简体   繁体   English

在RMarkdown中包含来自外部源R的功能

[英]Include a function from external source R in RMarkdown

Suppose I have a functions.R script in which I have defined a few functions: 假设我有一个functions.R脚本,其中定义了一些函数:

foo <- function(x) print(x-2)
bar <- function(x) print(x^3)
...

Now I want to include only foo (and not the whole functions.R in a chunk in RMarkdown. 现在,我只想包括foo (而不是整个 functions.R在RMarkdown一大块。

If I wanted all functions to be included, following this answer , I could have done this via: 如果我希望包括所有功能,请按照以下答案进行操作:

``` {r, code = readLines("functions.R")}
```

However, I only need foo in the chunk. 但是,我只需要foo就可以了。 How can I do it? 我该怎么做? Thanks in advance. 提前致谢。

I adapt the approach from this answer to a related question to solve this problem. 我将解决方案从此答案改编为一个相关问题。 1 1

I stored in the file functions.R your two example functions above: 我在文件functions.R存储了.R上面的两个示例函数:

foo <- function(x) print(x-2)
bar <- function(x) print(x^3)

I then create the following R Markdown document: 然后,我创建以下R Markdown文档:

---
title: "SO Answer"
author: "duckmayr"
date: "7/8/2019"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

We set up a function to source an R script, but only assign to the global environment the functions we want:

```{r source_function}
source_functions <- function(fcn_names, file_name) {
    env <- new.env()
    source(file_name, local = env)
    for ( i in 1:length(fcn_names) ) {
        assign(fcn_names[i], get(fcn_names[i], envir = env), envir = .GlobalEnv)
    }
}
```

Then we can call it to get *only* the function `foo()`:

```{r get_foo}
source_functions("foo", "functions.R")
ls()
```

This will also work for multiple functions:

```{r get_foo_and_bar}
source_functions(c("foo", "bar"), "functions.R")
ls()
```

Which renders as: 呈现为:

在此处输入图片说明

We see that in the first call to source_functions() , only the foo() function is added to the global environment, but not the bar() function, which should work to solve your problem. 我们看到在对source_functions()的第一次调用中,仅foo()函数被添加到了全局环境中,而没有 bar()函数被添加到了全局环境中,这应该可以解决您的问题。 If you wanted multiple functions from functions.R , this will also work as well, as demonstrated in the second call to source_functions() . 如果您需要来自functions.R多个功能,这也将起作用,如第二次调用source_functions()

Update: Showing the Functions' Code 更新:显示函数代码

We can edit our source_functions() function to show the code of the functions it sources, like so: 我们可以编辑source_functions()函数以显示其源函数的代码,如下所示:

source_functions <- function(fcn_names, file_name) {
    env <- new.env()
    source(file_name, local = env)
    n <- length(fcn_names)
    result <- character(n)
    for ( i in 1:n ) {
        name <- fcn_names[i]
        fcn <- get(name, envir = env)
        code <- capture.output(show(fcn))
        code <- paste(code[-which(grepl("<env", code))], collapse = " ")
        assign(name, fcn, envir = .GlobalEnv)
        result[i] <- paste(name, "<-", code, collapse = " ")
    }
    return(result)
}

Then our document renders as 然后我们的文档呈现为

在此处输入图片说明


1 That question and answer (1) was not in the context of R Markdown, and more importantly (2) only demonstrated how to get all functions from an R script (without getting other objects) rather than particular functions. 1 这个问题和答案(1)不在R Markdown的上下文中,更重要的是(2)仅演示了如何从R脚本获取所有函数(不获取其他对象)而不是特定函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM