如何在 R 中将 uiOutput 下载为 HTML 文档

How to download uiOutput as HTML document in R

我有 R Shiny 应用,可以在浏览器中显示 uiOutput,现在我想添加 downloadHandler 以将 uiOutput 的内容下载到 HTML 文档。

下面是我的代码

library(shiny)
library(knitr)
library(rmarkdown)

ui <- shinyUI(
  fluidPage(
    uiOutput('markdown'),
    downloadButton('Download_Output')
    
  )
)
server <- function(input, output) { 
  
  output$markdown <- renderUI({
    HTML(markdown::markdownToHTML(knit('RMarkdownFile.rmd', quiet = TRUE)))
  })
  
  output$Download_Output <- downloadHandler(
    filename <- function() {
          paste("output_", Sys.Date(), ".html", sep = "")
          },
    content <-
      function(file) {
        #render("RMarkdownFile.rmd",html_document())
       
      }
  )
  
  }

shinyApp(ui, server)

下面是示例 R Markdown 文件。

``{python py-code, echo=FALSE, exercise=TRUE}
import matplotlib.pyplot as plt
left = [1, 2, 3, 4, 5]
height = [10, 24, 36, 40, 5] 
tick_label = ['one', 'two', 'three', 'four', 'five'] 
plt.bar(left, height, tick_label = tick_label, width = 0.8, color = ['red', 'green'])
# naming the x-axis 
plt.xlabel('x - axis') 
# naming the y-axis 
plt.ylabel('y - axis') 
# plot title 
plt.title('My bar chart!') 
# function to show the plot 
plt.show()```

感谢您的宝贵时间和帮助。

我终于找到了解决这个问题的方法,如果您发现更好的方法,请随时添加答案或评论。谢谢

        #Inside download_content function

        content <- function(file) {
          out <- rmarkdown::render('RMarkdownFile.rmd', output_format = 'html_document')
        }
        file.rename(out, file)