【问题标题】:Passing function in rmarkdown parameter to render plots in html report在 rmarkdown 参数中传递函数以在 html 报告中呈现图表
【发布时间】:2017-09-26 10:18:26
【问题描述】:

我将把从 shiny 生成的图传递给 rmarkdown 以生成 html 报告。问题是 rmarkdown 文档的 params 中接受了 wordcloud 和饼图。

问题:如何通过闪亮的方式在渲染的 html 中传递 wordcloud 和饼图?

abc.Rmd

title: "Report using R Markdown"
subtitle: "ABC "
author: "Author name"
output:
     prettydoc::html_pretty:
     theme: architect
params:
    wc : 'NULL'
    table: 'NULL'
    pie: 'NULL'

app.R(sn-p)

rmarkdown::render(input = "report.Rmd",
                  output_file = "report.html",
                  params = list(wc=getWordcloud(),
                                table=getTab(),
                                pie=getPie()))

注意:getWordcloud()、getTab()、getPie() 函数在闪亮的应用程序中完美地返回绘图。

【问题讨论】:

  • 你有什么问题?
  • wc,pie 的值在 rmarkdown 中显示为 NULL,即使我在应用程序的服务器端将绘图作为参数传递

标签: r r-markdown shiny


【解决方案1】:

您不能将函数作为参数类型。

参看这里的参数类型: http://rmarkdown.rstudio.com/developer_parameterized_reports.html

支持yaml::yaml.load 函数可以解析的所有标准 R 类型,包括 characterintegernumericlogical

我强烈建议您找到一种无需在参数中传递函数即可使您的代码正常工作的方法。也许您可以传递函数的选项并将函数包含在 rmd 中?

但是,有办法绕过它:

一种是在参数中使用函数的名称作为字符串,并使用eval()将字符串作为代码进行评估。

abc.Rmd

title: "Report using R Markdown"
subtitle: "ABC "
author: "Author name"
output:
     prettydoc::html_pretty:
     theme: architect
params:
    wc : wc_function
    table: table_function
    pie: pie_function


 eval(paste0(param$wc_function, "(", my_options_as_string, ")")) 

app.R(sn-p)

rmarkdown::render(input = "report.Rmd",
                  output_file = "report.html",
                  params = list(wc="getWordcloud",
                                table="getTab",
                                pie="getPie"))

另一种方法是拥有另一个带有函数的 r 脚本,在 rmarkdown 中使用 source 调用。

这样,您可以将文件的路径作为参数传递,它允许您访问 rmarkdown 中的函数(但这意味着函数的名称是固定的)

【讨论】:

  • 我的绘图是在闪亮应用程序的服务器端生成的,所以即使我的文件来源是不允许从闪亮输出对象读取对象。
  • 您想在 rmarkdown 中包含对闪亮应用的调用,例如 that exemple?
  • 其实反之亦然,我在闪亮的应用程序中添加了 下载 按钮,该按钮将 Markdown 文档与我生成的绘图以闪亮的 html 文档形式呈现
  • 为什么不直接在url中包含参数,比如here?这样,您可以在主页面中包含一组按钮来定义您的参数,然后是指向生成的 rmarkdown 参数的重定向链接。可以很容易地将 js 包含在您的 rmarkdown 中。
  • 这是一个好方法,但是一旦我将其下载为 html,绘图就会显示在保存的 html 文件中。
猜你喜欢
  • 1970-01-01
  • 2020-09-25
  • 1970-01-01
  • 2018-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-20
相关资源
最近更新 更多