r - 将参数传递给 R Markdown

标签 r r-markdown

我正在尝试根据以下教程在 R Markdown 中创建参数化报告:http://rmarkdown.rstudio.com/developer_parameterized_reports.html#passing-parameters

我正在尝试使用渲染从 r 控制台传递文件路径作为参数。 像这样:

render('rmarkdownfile.rmd',params= list( client= "clientdata.csv"))

我的 Markdown 文件如下所示:

title: "Liquidity Report"
output: pdf_document
params: client:"clientdata.csv"
---
```{r plot, echo=FALSE, warning=FALSE}
cftest <- read.csv(params$client)

但我收到一条错误消息:

Eror in read.table(file=file, header=header, sep=sep, quote=quote, : 'file' must be a character string or connection Calls:

即使我按照教程的步骤操作,Markdown 似乎也无法识别参数。有人能够在 R Markdown 中成功使用参数吗?

此外,我遵循本教程的建议,并使用 R Studio 预览版以及 r markdown 和 knit 的最新版本。

感谢您的帮助!

拉斐尔

最佳答案

我喜欢做的不仅仅是指定文件名,还指定参数化报告上的目录。

---
title: Liquidity Report
date: '`r strftime(Sys.time(), format = "%B %d, %Y")`'
output:
  pdf_document:
    number_sections: yes
    theme: cerulean
    toc: yes
    toc_depth: 2
params:
  directory:
    value: x
  file:
    value: x
---

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

## Pull in the data
dataset <- read.csv(file.path(params$directory, params$file))
```

然后在渲染函数中您可以:

rmarkdown::render(
      input  = 'LiquidityReport.Rmd'
    , params = list(
          directory = '~/path/to/data'
        , file      = 'clientdata.csv'
        )
)

knitr 文档可以添加更多信息:> ?knitr::knit_params

关于r - 将参数传递给 R Markdown,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32479130/

相关文章:

在顶部使用时减少行间距

r - 使用 FileInput Shiny App 上传 Json 文件

css - 在 R 中的 DT 中悬停图像弹出窗口

r - 浏览器刷新后交互式 rmarkdown 文档无法正确显示

yaml - R Markdown YAML "Scanner error: mapping values..."

latex - R markdown:如何创建包含图像和文本的表格,这些表格应被编织为PDF?

r - R按data.table中的条件分组

r - 如何使用 xts 包计算日内数据的每日平均相关性?

r - 使用 dplyr::select 排列列而不进行硬编码

R Markdown - 没有 ODT 和 LaTeX 选项作为输出