将闪亮的HTML输出下载为txt或pdf

时间:2018-08-30 18:47:07

标签: html r shiny

我正在尝试从闪亮的应用程序以txt或pdf格式下载HTML输出。我已经设法将其下载为txt文件,但它随附了希望摆脱的html代码。在浏览器中打开应用程序后,下载工作正常。任何帮助将不胜感激。我的代码如下。

 library(shiny)

 server <- shinyServer(function(input, output, session) {

  output$textview <- renderUI({
         movie <- "Crazy Rich Asians"
         summary <- "Rachel Chu is happy to accompany her longtime 
                     boyfriend, Nick, to his best friend's wedding in Singapore. She's 
                   also surprised to learn that Nick's family is extremely 
                   wealthy and he's considered one of the country's most 
                    eligible bachelors. "
          director <- "Jon M. Chu"
         text <- data.frame(movie,summary,director)
       brief=""
       details <- text
       if (dim(details)[1]<5){
            for(i in seq(from=1,to=dim(details)[1])){
                  brief <-paste(brief,
                  paste("Movie: ",details[i,"movie"]),
                  sep="<br/><br/>")
                  brief <-paste(brief,
                  paste("Director: ",details[i,"director"]),
                  sep="<br/><br/>")
                  brief <-paste(brief,
                  paste("Summary: ",details[i,"summary"]),
                 sep="<br/><br/>")
     }

       save01 <<- brief
  }
 HTML(brief) 

 })


output$downloadData <- downloadHandler(

  filename = function() {
    file <- "saveddetails.txt"
    file
   },

  content = function(file) {
    write(save01, file)
   }
  )


})


 ui_panel <- 
      tabPanel("Multi-Select Input Test",
         sidebarLayout(
             sidebarPanel(
              downloadButton('downloadData', 'Download'),
             br()
           ),
           mainPanel(
             tabsetPanel(tabPanel("Text",htmlOutput("textview"))

             )
            )
          ))


 ui <- shinyUI(navbarPage(" ",ui_panel))

  runApp(list(ui=ui,server=server))

对于如何将html输出下载为txt或pdf文件的任何建议或见解,将受到高度赞赏。

1 个答案:

答案 0 :(得分:0)

  

对于如何将html输出下载为txt或pdf文件的任何建议或见解,将受到高度赞赏。

使用rvest包,此函数将返回不带html标签的文本。可以将此代码放置在服务器和ui函数外部的闪亮应用程序中。

function centuryFromYear(year) {
    return Math.floor((year + 99) / 100);
}

发件人:https://stackoverflow.com/a/34344957/10297551

然后,您可以在Shiny服务器功能的library(rvest) strip_html <- function(s) { html_text(read_html(s)) } 代码中插入strip_html

downloadHandler