简体   繁体   English

如何在 R shiny 中使用 downloadHandler 导出 HTML Output?

[英]How to export HTML Output using downloadHandler in R shiny?

I have an R shiny app using the compare_df function as part of the compareDF package and it produces an HTML output. I was wondering how to export this as an HTML file using downloadHandler?我有一个 R shiny 应用程序,它使用 compare_df function 作为 compareDF package 的一部分,它会生成一个 HTML output。我想知道如何使用 download 将其导出为 88789878223 文件? This is my attempt:这是我的尝试:

Partial Code部分代码


ui <- fluidPage(
sliderPanel(
   downloadButton("Export_HTML", "Export as Data Listing")
  ),
  # Main panel for displaying outputs ----
  mainPanel(uiOutput('html'))
  )

server <- function(input,output){

a<-- c("1","2","3")

diff<-compare_df(filedata2, filedata1, group_col = a)
   output$html <- renderUI({

      HTML(knit2html(text=diff[["html_output"]], fragment.only=TRUE))
})

  output$Export_HTML <- downloadHandler(
    filename = function() {
      paste("Comparison-", Sys.Date(), ".html", sep = "")
    },
    content = function(file) {

      saveWidget(as_widget(diff[["html_output"]]), file, selfcontained = TRUE)
    }
  )
}

您可能需要查看Shiny Tutorial页面,使用下载处理程序通过R Markdown模板生成HTML: https//shiny.rstudio.com/articles/generating-reports.html

To download html file when comparing two datasets, we need to have two files in application structure要在比较两个数据集时下载 html 文件,我们需要在应用程序结构中有两个文件

  1. app.R申请.R
  2. report.Rmd报告.Rmd

app.R申请.R

library(shiny) 
library(diffobj)
library(rmarkdown)


ui <- fluidPage(
  sidebarPanel(
    downloadButton('downloadReport')
  ),
  # Main panel for displaying outputs ----
  mainPanel(htmlOutput('html'))
)

server <- function(input,output){
  
  filedata1 <- data.frame(a = c(1,2,3,4), b= c(3,5,8,9))
  filedata2 <- data.frame(a = c(1,2,3,4), b= c(4,5,8,10))
  

  
  output$html <- renderUI({
    
    
    HTML(as.character(diffPrint(filedata2, filedata1, color.mode="rgb", format="html",
                                style=list(html.output="diff.w.style"))))
  })
  
  output$downloadReport <- downloadHandler(
    filename = function() {
      paste('Compare-report', "html", sep = '.')
    },
    
    content = function(file) {
      src <- normalizePath('report.Rmd')
      
      # temporarily switch to the temp dir, in case you do not have write
      # permission to the current working directory
      owd <- setwd(tempdir())
      on.exit(setwd(owd))
      file.copy(src, 'report.Rmd', overwrite = TRUE)
      
      out <- render('report.Rmd')
      file.rename(out, file)
    }
  )
}

shinyApp(ui = ui, server = server)

report.Rmd报告.Rmd

```{r, echo=FALSE}


filedata1 <- data.frame(a = c(1,2,3,4), b= c(3,5,8,9))
filedata2 <- data.frame(a = c(1,2,3,4), b= c(4,5,8,10))

HTML(as.character(as.character(diffPrint(filedata2, filedata1, color.mode="rgb", format="html",
                                 style=list(html.output="diff.w.style")))))


```

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM