【问题标题】:Modularized R markdown structure模块化的 R markdown 结构
【发布时间】:2017-03-24 00:38:38
【问题描述】:

已经有一些关于此的问题,但它们要么不清楚,要么提供了不起作用的解决方案,可能是因为它们已经过时了:

大型项目的模块化代码结构

R Markdown/Notebook 很好,但它的呈现方式通常是一个包含所有文本和所有代码块的文件。我经常有这样的单个文件结构不是一个好的设置的项目。相反,我使用单个.R 主文件按顺序加载其他.R 文件。我想使用 R Notebook 复制这个结构,即我有一个 .Rmd 文件,我从多个 .R 文件中调用代码。

以这种方式处理项目的好处在于,它允许使用 RStudio 使用 .R 文件实现良好的正常工作流程,而且还可以在不复制代码的情况下从 R Notebook/Markdown 获得整洁的输出。

小例子

这被简化以使示例尽可能小。两个.R 文件和一个主.Rmd 文件。

start.R

# libs --------------------------------------------------------------------
library(pacman)
p_load(dplyr, ggplot2)
#normally load a lot of packages here

# data --------------------------------------------------------------------
d = iris
#use iris for example, but normally would load data from file

# data manipulation tasks -------------------------------------------------
#some code here to extract useful info from the data
setosa = dplyr::filter(d, Species == "setosa")

plot.R

#setosa only
ggplot(setosa, aes(Sepal.Length)) +
  geom_density()

#all together
ggplot(d, aes(Sepal.Length, color = Species)) +
  geom_density()

然后是笔记本文件:

notebook.Rmd:

---
title: "R Notebook"
output:
  html_document: default
  html_notebook: default
---

First we load some packages and data and do slight transformation:

```{r start}
#a command here to load the code from start.R and display it
```

```{r plot}
#a command here to load the code from plot.R and display it
```

期望的输出

所需的输出是手动将start.Rplot.R 中的代码复制到notebook.Rmd 中的代码块中得到的输出。这看起来像这样(由于屏幕空间不足而丢失了一些):

我尝试过的事情

source

这会加载代码,但不会显示它。它只显示source 命令:

knitr::read_chunk

here 中提到了这个命令,但据我所知,它实际上与source 的作用相同:它加载代码但不显示任何内容。

如何获得所需的输出?

【问题讨论】:

    标签: r r-markdown rnotebook


    【解决方案1】:

    解决方法是使用knitr的chunk选项code。根据knitr docs

    code: (NULL; character) 如果提供,它将覆盖 当前块;这允许我们以编程方式将代码插入 当前块;例如一个块选项代码 = capture.output(dump('fivenum', '')) 将使用源代码 函数 Fivenum 替换当前块

    但是没有提供示例。听起来必须给它一个字符向量,所以让我们试试readLines

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

    这会产生所需的输出,从而允许模块化的项目结构。

    直接向其提供文件不起作用(即code="start.R"),但会是一个很好的增强功能。

    【讨论】:

    • 这很棒。由于我有一个文件夹结构,因此我将其与 here 库相结合,这有助于 RStudio 中的项目文件夹。
    【解决方案2】:

    为了与 R Notebooks 的互操作性,您可以使用 knitr 的 read_chunk 方法,如上所述。在 notebook 中,必须在 setup chunk 中调用 read_chunk;由于您可以按任意顺序运行笔记本块,因此可以确保外部代码始终可用。

    这是一个使用read_chunk 将代码从外部 R 脚本导入笔记本的最小示例:

    example.Rmd

    ```{r setup}
    knitr::read_chunk("example.R")
    ```
    
    ```{r chunk}
    ```
    

    example.R

    ## ---- chunk
    1 + 1
    

    当您在笔记本中执行空块时,将插入来自外部文件的代码,并内联显示结果,就好像该块包含该代码一样。

    【讨论】:

      【解决方案3】:

      根据我上面的评论,我使用 here 库来处理文件夹中的项目:

       ```{ r setup, echo=FALSE, message=FALSE, warning=FALSE, results='asis'}
      
      library(here) 
      
      insert <- function(filename){
        readLines(here::here("massive_report_folder", filename))
      }
      ```
      

      然后每个块看起来像

      ```{ r setup, echo=FALSE, message=FALSE, warning=FALSE, 
              results='asis', code=insert("extra_file.R")}
      ```
      

      【讨论】:

        猜你喜欢
        • 2012-12-11
        • 2010-12-22
        • 1970-01-01
        • 2012-03-29
        • 1970-01-01
        • 2020-08-18
        • 1970-01-01
        • 1970-01-01
        • 2020-01-16
        相关资源
        最近更新 更多