从shinyapp内生成并渲染rmd

发布于 2025-01-20 06:40:25 字数 2991 浏览 0 评论 0原文

我编写了一个闪亮的应用程序,其中包括生成 rmd 文件,然后将其渲染为 html 报告。

如下面的简单示例所示,shinyapp 的服务器函数内部创建了一个变量,那么创建的 rmd 文件在渲染为 html 时应该可以访问该变量。

根据其他帖子和文章,似乎我们必须将 rmd 文件复制到临时文件夹中才能工作,这就是我在下面尝试做的事情。

该应用程序无法运行。 当我在本地尝试时,我收到此错误:

Quitting from lines 8-9 (x3.Rmd) 

Warning: Error in print: object 'xz' not found

所以看起来shiny能够找到生成的rmd,开始渲染它但无法访问shiny中生成的xz变量。我做了一些阅读,它似乎与渲染环境有关(即它在新会话中渲染),但我不知道如何解决这个问题。 (在我正在开发的实际应用程序中,渲染过程应该能够访问数据帧而不仅仅是字符串变量,但我相信出于说明目的,概念是相同的)。

当我在shinyapps.io上进行测试时,当我单击下载按钮时,它显示服务器故障问题。令人惊讶的是,应用程序日志中没有任何内容,它说当前没有日志。

当我测试我正在编写的原始应用程序(不是这个简单的示例)时,我在shinyapp.io日志中收到此错误:

2022-04-10T18:01:45.357461+00:00 shinyapps[6055802]: Warning in normalizePath(path, winslash = winslash, mustWork = mustWork) :
2022-04-10T18:01:45.357710+00:00 shinyapps[6055802]:   [No stack trace available]
2022-04-10T18:01:45.357627+00:00 shinyapps[6055802]: Warning: Error in abs_path: The file '/tmp/Rtmp27RVU8/x3.Rmd' does not exist.
2022-04-10T18:01:45.357543+00:00 shinyapps[6055802]:   path[1]="/tmp/Rtmp27RVU8/x3.Rmd": No such file or directory

我的目标是从shinyapp.io开始这项工作。有关在哪里进行的任何建议从那里开始,我认为有两个问题: 1-确保 rmd 渲染可以访问在闪亮中生成的变量 2-将 rmd 文件保存在一个“方便”的地方,以便闪亮查找和渲染。


library(shiny)

ui <- fluidPage(
  
  downloadButton("report", "Download sample report.")
  
  
)

server <- function(input, output, session) {
  
  
  #create content and export it into rmd
  
  observe({
    
    xz=  "hello there"
    
    
    
    x3 = '--- 
title: sample
output: html_document
---


``` {r}
print(xz)
```

';write(x3, file="x3.rmd", append = FALSE)
    
    
  })
  


  #Render the report and pass it to download handler
  output$report <- downloadHandler(
    
    filename = "sample.html",
    
    content = function(file) {
      
      tempReport <- file.path(tempdir(), "x3.Rmd")
      file.copy("x3.Rmd", tempReport, overwrite = TRUE)
      
      
      output <- rmarkdown::render(
        input = tempReport
      )
      file.copy(output, file) 
      
      
      
      
    })
  
  
}

shinyApp(ui, server)

这些是我使用的资源(但仍然无法使其工作)。所有处理用户上传的 .rmd 的参数化报告,这在我的情况下不起作用:

https://shiny.rstudio.com/articles/generate-reports.html https://community.rstudio.com/t/where-do-i-save-the-rmd-file-i-am-generating-with-a-shiny-r-app/65987/3 https://community.rstudio.com/t/generate-downloadable-reports/ 39701

https://mastering-shiny.org/action-transfer.html#downloading-reports

I wrote a shiny app that includes generating an rmd file and then rendering it into html report.

As shown in the simple example below, there is a variable created inside the server function of shinyapp, then the created rmd file should have access to this variable while rendering into html.

according to other posts and articles, it seems that we have to copy the rmd file into a temporary folder to work and that's what I tried to do below.

the app is not working.
when I try locally, I get this error:

Quitting from lines 8-9 (x3.Rmd) 

Warning: Error in print: object 'xz' not found

so it seems that shiny was able to find the generated rmd, started rendering it but did not have access to the xz variable generated in shiny. I did some reading, and it seems to be related to the render environment (ie it renders in a new session) but I do not know how to fix that. (in the real app I am working on, the render process should have access to a dataframe not just a string variable, but I believe the concept is the same for illustration purpose).

when I tested on shinyapps.io, it says Failed-server problem when I click on the download button. Surprisingly, there is nothing in the application log, it says currently no logs.

when I test the original app I am writing (not this simple example), I get this error in shinyapp.io logs:

2022-04-10T18:01:45.357461+00:00 shinyapps[6055802]: Warning in normalizePath(path, winslash = winslash, mustWork = mustWork) :
2022-04-10T18:01:45.357710+00:00 shinyapps[6055802]:   [No stack trace available]
2022-04-10T18:01:45.357627+00:00 shinyapps[6055802]: Warning: Error in abs_path: The file '/tmp/Rtmp27RVU8/x3.Rmd' does not exist.
2022-04-10T18:01:45.357543+00:00 shinyapps[6055802]:   path[1]="/tmp/Rtmp27RVU8/x3.Rmd": No such file or directory

My goal is to make this work from shinyapp.io. Any suggestions on where to go from there, I believe there are two issues:
1- make sure the rmd render will have access to the variables generated in shiny
2- save the rmd file in a place that is "convenient" for shiny to find and render.


library(shiny)

ui <- fluidPage(
  
  downloadButton("report", "Download sample report.")
  
  
)

server <- function(input, output, session) {
  
  
  #create content and export it into rmd
  
  observe({
    
    xz=  "hello there"
    
    
    
    x3 = '--- 
title: sample
output: html_document
---


``` {r}
print(xz)
```

';write(x3, file="x3.rmd", append = FALSE)
    
    
  })
  


  #Render the report and pass it to download handler
  output$report <- downloadHandler(
    
    filename = "sample.html",
    
    content = function(file) {
      
      tempReport <- file.path(tempdir(), "x3.Rmd")
      file.copy("x3.Rmd", tempReport, overwrite = TRUE)
      
      
      output <- rmarkdown::render(
        input = tempReport
      )
      file.copy(output, file) 
      
      
      
      
    })
  
  
}

shinyApp(ui, server)

These are the resources I used (but still unable to make it work). All deal with parameterized reports of a user uploaded .rmd, which won't work in my case:

https://shiny.rstudio.com/articles/generating-reports.html
https://community.rstudio.com/t/where-do-i-save-the-rmd-file-i-am-generating-with-a-shiny-r-app/65987/3
https://community.rstudio.com/t/generating-downloadable-reports/39701

https://mastering-shiny.org/action-transfer.html#downloading-reports

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

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

发布评论

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

评论(1

海拔太高太耀眼 2025-01-27 06:40:25

我已经使用闪亮的应用程序完成了此操作,但在闪亮的服务器上(在我的工作场所),并且只能在此处分享该方法。目前,访问确切的代码需要时间,但这会给您带来想法。不知道它如何与shinyapps.io一起使用,但有时会尝试一下。

在闪亮的 UI 中提供了一个“生成报告”按钮。

在服务器中,

observeEvent(input$btn_Id,{

#Code to render the html in rmarkdown and then also `downloadHandler()`

})

请同时检查:
使用 downloandHanlder() 引用此文档< /a>.
这给了
下载数据

传递变量的解决方案并不特殊。只需确保调用 render() 时数据已存在即可
像这样:

rmarkdown::render(input = "D:/YourWorkingDirectly/Letters.rmd")

如果这对您没有帮助,请告诉我,我将删除答案。

I have done this with Shiny App but on a shiny server (in my work place) and can share only the approach here. Currently the access to exact code will take time but this shall give you idea. No idea how it works with shinyapps.io but will try that sometime.

Within UI of shiny provide for a "Generate Report" Button.

In server, with

observeEvent(input$btn_Id,{

#Code to render the html in rmarkdown and then also `downloadHandler()`

})

Please check this also :
Use the downloandHanlder() referring this documentation.
This one gives idea to download data

Solution to passing variables is not special. Just ensure data is already present when you are calling render()
like this:

rmarkdown::render(input = "D:/YourWorkingDirectly/Letters.rmd")

If this doesn't help you , please let me know and will delete the answer.

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