我正在开发一个shiny 的应用程序,用户在其中上传一个文件,然后对其进行处理,生成一份报告,用户可以将其作为可编辑的单词下载.doctor .

除此之外,它还能按预期工作,尽管出现了一个"另存为"对话框窗口,似乎允许您 Select 目标目录,结果是.文档文件最终被保存到一个临时目录中,并带有一个随机生成的名称(这是在Windows下).

我怀疑这是由于使用了tempdir命令,这是使用rmarkdown生成下载文件的一部分.

应如何修改以下代码以允许 Select 目标文件夹?

#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
#    http://shiny.rstudio.com/
#

library(shiny)
library(knitr)

# Define UI for application that draws a histogram
ui <- fluidPage(
  uiOutput('markdown'),
    # Application title
    titlePanel("Apptitle"),

    # Sidebar with file input
    sidebarLayout(
        sidebarPanel(
          fileInput(
            inputId = "file1",
            label = "Select file(s)",
            multiple = TRUE,
            accept = NULL,
            width = NULL,
            buttonLabel = "Browse...",
            placeholder = "No file(s) selected"
          ),
        downloadButton("report", "Generate report")
        ),
    )
)

server <- function(input, output) {
      
        output$report <- downloadHandler(
          reactive(file <- input$file1),
          filename = "wordreport.doc",
          content = function(file) {
            tempReport <- file.path(tempdir(), "wordreport.Rmd")
            file.copy("wordreport.Rmd", tempReport, overwrite = TRUE)
            params <- list(report.data = input$file1)
            rmarkdown::render(tempReport, output_file = "wordreport.doc",
                              params = params,
                              envir = new.env(parent = globalenv()))
    })
}

shinyApp(ui = ui, server = server)

谢谢你的帮助!

编辑:已修复,使用下面的解决方案,此处建议的代码编辑:Passing a dataframe as a parameter from Shiny app to RMarkdown

推荐答案

你把reactive(file <- input$file1)作为contentType的参数传给了downloadHandler(),这可不好.此外,您没有向作为content函数参数给出的file写入任何内容.

go 掉reactive(file <- input$file1)行,在rmarkdown::render()中指定output_file = file,你的下载就可以了.

正如 comments 中所讨论的,您将无法控制下载路径——这是由用户的web浏览器及其设置决定的.

这里有一个更简单的应用程序,可以下载文件,供参考:

library(shiny)

ui <- fluidPage(
  sliderInput("value", "Some value", 1, 5, 2),
  downloadButton("report", "Generate report")
)

server <- function(input, output) {
  output$report <- downloadHandler(
    filename = "wordreport.doc",
    content = function(file) {
      params <- list(value = input$value)
      rmarkdown::render(
        system.file("examples/knitr-minimal.Rmd", package = "knitr"),
        output_file = file,
        params = params,
        envir = new.env(parent = globalenv())
      )
    }
  )
}

shinyApp(ui, server)

R相关问答推荐

如何按行和列组合多个格式?

即使声明引发错误,R函数也会在第二次try 时返回结果

导入到固定列宽的R中时出现问题

R的GG平行坐标图中的排序变量

如何在R中正确对齐放射状图中的文本

R等效于LABpascal(n,1)不同的列符号

如何在RMarkdown LaTex PDF输出中包含英语和阿拉伯语?

基于多列将值链接到NA

gganimate在使用shadow_mark选项时不保留所有过go 的标记

为什么舍入POSIXct会更改能力以匹配等效的POSIXct?

R中的时间序列(Ts)函数计数不正确

为了网络分析目的,将数据框转换为长格式列联表

展开对数比例绘图的轴(添加填充)

具有重复元素的维恩图

为左表中的所有行使用值Fill滚动左连接

停止ggplot将多行减少到一行

手动指定从相同数据创建的叠加图的 colored颜色

错误包arrowR:READ_PARQUET/OPEN_DATASET&QOT;无法反序列化SARIFT:TProtocolException:超出大小限制&Quot;

如何使用包metaviz更改标签的小数位数?

在同一单元格中创建包含整数和百分比的交叉表