简言之,我希望能够生成一个动态的Rmarkdown报告文件(pdf或html)从我的shiny 的应用与按钮点击.为此,我想我将使用参数化的报表.但不知何故,我无法将单个谜题转移到预期目标:

有了这段代码,我们可以在R Shinny中生成并下载一个react 式雷达艺术:

library(shiny)
library(radarchart)

js <- paste0(c(
  "$(document).ready(function(){",
  "  $('#downloadPlot').on('click', function(){",
  "    var el = document.getElementById('plot1');",
  "    // Clone the chart to add a background color.",
  "    var cloneCanvas = document.createElement('canvas');",
  "    cloneCanvas.width = el.width;",
  "    cloneCanvas.height = el.height;",
  "    var ctx = cloneCanvas.getContext('2d');",
  "    ctx.fillStyle = '#FFFFFF';",
  "    ctx.fillRect(0, 0, el.width, el.height);",
  "    ctx.drawImage(el, 0, 0);",
  "    // Download.",
  "    const a = document.createElement('a');",
  "    document.body.append(a);",
  "    a.download = 'radarchart.png';",
  "    a.href = cloneCanvas.toDataURL('image/png');",
  "    a.click();",
  "    a.remove();",
  "    cloneCanvas.remove();",
  "  });",
  "});"
), collapse = "\n")

ui <- pageWithSidebar(
  headerPanel('Radarchart Shiny Example'),
  sidebarPanel(
    checkboxGroupInput('selectedPeople', 'Who to include', 
                       names(radarchart::skills)[-1], selected="Rich"),
    actionButton('downloadPlot', 'Download Plot'),
    downloadButton('report', 'Generate Report')
  ),
  mainPanel(
    tags$head(tags$script(HTML(js))),
    chartJSRadarOutput("plot1", width = "450", height = "300"), width = 7
  )
)

server <- function(input, output) {
  output$plot1 <- renderChartJSRadar({
    chartJSRadar(skills[, c("Label", input$selectedPeople)], 
                 maxScale = 10, showToolTipLabel=TRUE) 
  })
}
shinyApp(ui, server)

enter image description here

What I would like to do is to implement: Generating downloadable reports 100

此网站的代码如下所示:

应用程序.R

shinyApp(
  ui = fluidPage(
    sliderInput("slider", "Slider", 1, 100, 50),
    downloadButton("report", "Generate report")
  ),
  server = function(input, output) {
    output$report <- downloadHandler(
      # For PDF output, change this to "report.pdf"
      filename = "report.html",
      content = function(file) {
        # Copy the report file to a temporary directory before processing it, in
        # case we don't have write permissions to the current working dir (which
        # can happen when deployed).
        tempReport <- file.path(tempdir(), "汇报Rmd")
        file.copy("汇报Rmd", tempReport, overwrite = TRUE)

        # Set up parameters to pass to Rmd document
        params <- list(n = input$slider)

        # Knit the document, passing in the `params` list, and eval it in a
        # child of the global environment (this isolates the code in the document
        # from the code in this app).
        rmarkdown::render(tempReport, output_file = file,
          params = params,
          envir = new.env(parent = globalenv())
        )
      }
    )
  }
)

汇报Rmd

---
title: "Dynamic report"
output: html_document
params:
  n: NA
---

```{r}
# The `params` object is available in the document.
params$n
```

A plot of `params$n` random points.

```{r}
plot(rnorm(params$n), rnorm(params$n))
```

我在这里try 了很多:

How to pass table and plot in Shiny app as parameters to R Markdown?

Shiny: pass a plot variable to Rmarkdown document when generating a downloadable report

但对我来说,无法将我的代码转换为上面提供的示例代码!单击"生成报告"按钮后,所需的输出如下:

enter image description here

推荐答案

基本上你的问题已经包括了所有的构建模块.我只更新了报告模板,加入了绘制雷达图的代码.作为一个参数,我决定传递过滤后的数据集.在服务器中,我只调整了params的规格:

server <- function(input, output) {
  output$plot1 <- renderChartJSRadar({
    chartJSRadar(skills[, c("Label", input$selectedPeople)], 
                 maxScale = 10, showToolTipLabel=TRUE) 
  })
  
  output$report <- downloadHandler(
    filename = "report.html",
    content = function(file) {
      tempReport <- file.path(tempdir(), "report.Rmd")
      file.copy("report.Rmd", tempReport, overwrite = TRUE)
      
      params <- list(scores = skills[, c("Label", input$selectedPeople)])

      rmarkdown::render(tempReport, output_file = file,
                        params = params,
                        envir = new.env(parent = globalenv())
      )
    }
  )
}

汇报Rmd

---
title: "Dynamic report"
output: html_document
params:
  scores: NA
---

```{r}
chartJSRadar(params$scores, maxScale = 10, showToolTipLabel=TRUE)
```

enter image description here

R相关问答推荐

按R中不同长度的组将日期时间列值四舍五入到小时

收件箱摘要表布局在第一列上显示子类别

返回句子中最长的偶数长单词

分组时间连续值

在Julia中调用R函数

如何在R中正确对齐放射状图中的文本

在边界外添加注释或标题

更新合适的R mgcv::bam模型报告无效类型(关闭).'';错误

如何将在HW上运行的R中的消息(错误、警告等)作为批处理任务输出

如何求解arg必须为NULL或deSolve包的ode函数中的字符向量错误

R s iml包如何处理语法上无效的因子级别?'

使用R闪光显示所有数据点作为默认设置

根据现有列的名称和字符串的存在进行变异以创建多个新列

在另一个包中设置断点&S R函数

减go R中列表的所有唯一元素对

将文本批注减少到gglot的y轴上的单个值

在r中整理图例和堆叠图的问题

用满足特定列匹配的另一行替换NA行

是否有一个R函数可以输出在输入的字符向量中找到的相应正则表达式模式?

填充图例什么时候会有点?