firmo23
firmo23

Reputation: 8454

Save downloaded files in your working directory in a shiny app

I have the shiny app below in which I create .rmd report and then try to downlaod it. The issue is that when Im not in browser mode the pdf instantly opens when I download it and I do not know where it goes and when I download it while Im in browser mode it is saved in the 'Downloads' folder instead of my working directory. How can I set it to be saved instantly in my working directory?

the 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)

and the app

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(), 
                            params = list(data = data)
              )
              file.rename(out, file)
              
            }
          )
        
  }
  )
)

Upvotes: 1

Views: 811

Answers (2)

firmo23
firmo23

Reputation: 8454

You need to define output_file when you define output_dir. Please try this

 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)
            }

Upvotes: 0

Arnaud Feldmann
Arnaud Feldmann

Reputation: 765

I've not understood exactly what you need so I propose two answers depending of what you need

  1. You can't make a client browser download "instantly" into a folder without any popup. If that was the case any website would deal with your hard drive openly. Though, you can sepecify the "save to" menu instead of the pdf reader using contentType="application/octet-stream" as an arg of downloadHandler
  2. You can render your pdf anywhere you want server side with the output_file arg of render.

Upvotes: 1

Related Questions