簡體   English   中英

Shiny 不會將下載的文件保存在使用 output_dir 設置的文件夾中

[英]Shiny does not save downloaded file in folder set with output_dir

我有下面的 shiny 應用程序,我在其中創建.rmd報告,然后嘗試下載它。 當我處於瀏覽器模式時,它保存在“下載”文件夾中,而不是保存在我設置的文件夾中: output_dir = "C:/Users/User/Documents/Hodgkins/www",

ex.rmd

---
title: "An example Knitr/R Markdown document"
output: pdf_document
---


{r chunk_name, include=FALSE}
x <- rnorm(100)
y <- 2*x + rnorm(100)
cor(x, y)

和應用程序

library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(shinyjs)
library(knitr)
mytitle <- paste0("Life, Death & Statins")
dbHeader <- dashboardHeaderPlus(
  titleWidth = "0px",
  tags$li(a(
    
    
    div(style="display: inline;padding: 0px 0px 0px 0px;vertical-align:top; width: 150px;", actionButton("res", "Results"))
    
  ),
  class = "dropdown")
  
  
)

shinyApp(
  ui = dashboardPagePlus(
    header = dbHeader,
    sidebar = dashboardSidebar(width = "0px",
                               sidebarMenu(id = "sidebar", # id important for updateTabItems
                                           
                                           menuItem("Results", tabName = "res", icon = icon("line-chart"))
                               )           ),
    body = dashboardBody(
      
      useShinyjs(),
      tags$script(HTML("$('body').addClass('fixed');")),
      
      tags$head(tags$style(".skin-blue .main-header .logo { padding: 0px;}")),
      tabItems(
        
        tabItem("res",
                tags$hr(),
                tags$hr(),
                
                fluidRow(
                  column(3,
                         div(style="display: block; padding: 5px 10px 15px 10px ;",
                             downloadButton("report",
                                            HTML(" PDF"),
                                            style = "fill",
                                            color = "danger",
                                            size = "lg",
                                            block = TRUE,
                                            no_outline = TRUE
                             ) )
                  ),
                  column(6,
                         uiOutput('markdown'))))
      ),
      
      
      
    )
    
  ),
  server<-shinyServer(function(input, output,session) { 
    hide(selector = "body > div > header > nav > a")
    
    
          output$markdown <- renderUI({
            HTML(markdown::markdownToHTML(knit('ex.rmd', quiet = TRUE)))
          })
          
          
          output$report <- downloadHandler(
            
            filename = "report.pdf",
            content = function(file) {
              src <- normalizePath('ex.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, 'ex.Rmd', overwrite = TRUE)
              
              library(rmarkdown)
              out <- render(input = 'ex.Rmd', 
                            output_format = pdf_document(),
 output_dir = "C:/Users/User/Documents/Hodgkins/www",

                            params = list(data = data)
              )
              file.rename(out, file)
              
            }
          )
        
  }
  )
)

定義output_dir時需要定義output_file 請試試這個

 output$report <- downloadHandler(

            filename = "report.pdf",
            content = function(file) {
              src <- normalizePath('ex.Rmd')
              
              owd <- setwd(tempdir())
              on.exit(setwd(owd))
              file.copy(src, 'ex.Rmd', overwrite = TRUE)

              library(rmarkdown)
              out <- render(input = 'ex.Rmd',
                            output_format = pdf_document(),
                            output_file = "reportt.pdf",
                            #output_dir = "C://My Disk Space//temp//",
                            output_dir = "C://Users//User//Documents//Hodgkins//www//",
                            params = list(data = data)
              )
              #file.rename(out, file)
              file.copy(out, file)
            }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM