内联代码未在闪亮的应用程序中执行,并带有rmarkDown报告

发布于 2025-02-13 15:52:36 字数 2602 浏览 1 评论 0原文

亲爱的所有闪亮的开发人员!

我有一个有关可下载rmarkDown报告的闪亮应用程序的疑问。 该应用基于此 https://shiny.rstudio.com/articles/articles/generation-generating-generating-generating-generating-generating-generating-generating-gen reports.html ,在这里作为一个景象。

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.pdf",
      content = function(filename) {
        # 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(), "Template.Rmd")
        print(tempReport)
        file.copy("Template.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 = filename,
                          params = params,
                          envir = new.env(parent = globalenv())
        )
      }
    )
  }
)

使用我的.rmd文档,

---
title: ''
output: pdf_document
geometry: "left=2cm,right=2cm,top=1cm,bottom=2cm"
params:
  n: NA
---


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

A plot of `r params$n` random points.

代码运行正常,创建了文档,但是当我打开它时,我可以看到块中的代码还可以,但在内联代码中不在,请参见下文:

“在此处输入图像描述”

但是,在此典范中画廊/下载-knitr-reports.html“ rel =” nofollow noreferrer“> https://shiny.rstudio.com/gallery/download-knitr-reports.html 我们看到内联代码很好(我没有RmarkDown Dounctunatelly的代码)。

我看到了这个主题( https://community.rstudio.com/t/embedding-shiny-with-with-inline-not-not-not-with-with-html-actput/41175 )可能相似,但它是html和html似乎不合适。

您有一个想法为什么它不起作用吗?还是有探索的轨道?

非常感谢!!

Dear all shiny developers!

I have a question about a shiny app with a Rmarkdown report downloadable.
The app is based on this https://shiny.rstudio.com/articles/generating-reports.html, taken here as an exemple.

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.pdf",
      content = function(filename) {
        # 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(), "Template.Rmd")
        print(tempReport)
        file.copy("Template.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 = filename,
                          params = params,
                          envir = new.env(parent = globalenv())
        )
      }
    )
  }
)

with my .Rmd document

---
title: ''
output: pdf_document
geometry: "left=2cm,right=2cm,top=1cm,bottom=2cm"
params:
  n: NA
---


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

A plot of `r params$n` random points.

The code is running ok, the document is created but when I open it, I can see that the code within the chunk is ok but not in the inline code, see below:

enter image description here

However, in this exemple https://shiny.rstudio.com/gallery/download-knitr-reports.html we see that the inline code is good (I don't have the code of the Rmarkdown unfortunatelly).

I saw this subject (https://community.rstudio.com/t/embedding-shiny-with-inline-not-rendering-with-html-output/41175) that could be similar but it is html and the answer does not seem to fit.

Do you have an idea why it does not function ? Or any track to explore ?

Many thanks !!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小情绪 2025-02-20 15:52:37

请小心文件功能!

多亏了Limey的评论,我注意到有一个错字的RmarkDown片刻之前没有更新!小心文件正确!

这是脚本工作的脚本:

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.pdf",
      content = function(filename) {
        # 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(), "rules.Rmd")
        print(tempReport)
        file.copy("Data/rules.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 = filename,
                          params = params,
                          envir = new.env(parent = globalenv())
        )
      }
    )
  }
)

rmarkown

---
title: ''
output: pdf_document
geometry: "left=2cm,right=2cm,top=1cm,bottom=2cm"
params:
  n: NA
---

```{r}
# The `params` object is available in the document.
params$n
```
A plot of `r params$n` random points. 

感谢所有人给了这个主题的时间!

Be careful to the file.copy function !

Thanks to the remark of Limey, I notice that the Rmarkdown that had a typo few moments ago was not updated! Careful that the file.copy is correct!

Here is the script working:

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.pdf",
      content = function(filename) {
        # 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(), "rules.Rmd")
        print(tempReport)
        file.copy("Data/rules.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 = filename,
                          params = params,
                          envir = new.env(parent = globalenv())
        )
      }
    )
  }
)

and the Rmarkown

---
title: ''
output: pdf_document
geometry: "left=2cm,right=2cm,top=1cm,bottom=2cm"
params:
  n: NA
---

```{r}
# The `params` object is available in the document.
params$n
```
A plot of `r params$n` random points. 

Thanks to all of you that gave time to this subject!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文