首页 > 解决方案 > 保存和删除文件

问题描述

我希望在闪亮的应用程序中具有在用户在文本区域中输入输入后下载 word doc 的功能。用户将以 html 形式提供输入。下载按钮应生成 docx 文件。

library(shiny)

ui <- fluidPage(
  fluidRow(
    column(6, offset = 3,
           hr(),
           h2('Editor:'),
           textInput('editor1', 'MY TEXT',
                   HTML('<b>Sam</b> <i>Dave</i>'), 
                   ),
           hr(),
           h2('Editor Content:'),
           htmlOutput('editor1_content')
    )
  )
)

server <- function(input, output, session) { 
  output$editor1_content <- renderUI({HTML(enc2utf8(input$editor1))})
}

shinyApp(ui = ui, server = server)

标签: rshinyr-markdown

解决方案


如果可以选择创建临时文件,我们创建一个文件,将输入写入其中,进行转换,然后删除该文件

file1 <- file.path(tempdir(), "test.html")
writeLines(text = "<b>Sam</b> <i>Dave</i>", con = file1)
rmarkdown::pandoc_convert(file1, to = "docx", output = "word.docx")
file.remove(file1)

推荐阅读