Shiny unable download report

Hello, Dear R Community I need your help regarding shiny. I am getting error downloading report when app is deployed on shiny and rsconnect. But locally it is working fine. I have attched the log

library(shiny)
library(sdcMicro)


ui <- fluidPage(    
  titlePanel("Sample SDC Report"),
  uiOutput("ui_export_report")
)


server <- function(input, output) {
  data(testdata)
  sdcObj <- createSdcObj(
    testdata,
    keyVars = c('urbrur', 'roof', 'walls', 'water', 'electcon', 'relat', 'sex'),
    numVars = c('expend', 'income', 'savings'), w = 'sampling_weight'
  )
  
  output$ui_export_report <- renderUI({
    fluidRow(column(12,  
                    shiny::downloadButton("myRepDownload", "Save report", btn.style = "primary"), align = "center"
    ))
  })
  
  output$myRepDownload <- shiny::downloadHandler(
    filename = function() {
      paste0("sdcreport-", Sys.Date(), ".html")
    },
    
    content = function(file) {
      sdcMicro::report(
        sdcObj,
        outdir = tempdir(),
        filename = "tmp",
        title = "SDC-Report",
        internal = TRUE,
        verbose = FALSE
      )
      f_tmp <- file.path(tempdir(), "tmp.html")
      on.exit(file.remove(f_tmp))
      file.copy(from = f_tmp, to = file)
    }
  )  
}

shinyApp(ui = ui, server = server)


image

my assumption would be that sdcMicro::report wants to use interim files, and the default working directory isnt appropriate for that, so maybe using someithng like withr package, with_tempdir() would help.

Thank you @nirgrahamuk, Do you mean changing tempdir() to with_tempdir(), if so I did it and it did not work as well

no. tempdir() is for your output and you would still want that, but you want the working directory to be different (perhaps) so with_tempdir() should wrap your function

with_dir(tempdir(), sdcMicro::report(
        sdcObj,
        outdir = tempdir(),
        filename = "tmp",
        title = "SDC-Report",
        internal = TRUE,
        verbose = FALSE
      ))

Oh, I see your point let me try that, Thank you for your support. I will update you shortly

Unfortunately, It did not solve the problem same issue

image

I changed server part to this

  output$myRepDownload <- shiny::downloadHandler(
    filename = function() {
      paste0("sdcreport-", Sys.Date(), ".html")
    },
    
    content = function(file) {
      with_dir(tempdir(), sdcMicro::report(
        sdcObj,
        outdir = tempdir(),
        filename = "tmp",
        title = "SDC-Report",
        internal = TRUE,
        verbose = FALSE
      ))
      f_tmp <- file.path(tempdir(), "tmp.html")
      on.exit(file.remove(f_tmp))
      file.copy(from = f_tmp, to = file)
    }
  )  
}

shinyApp(ui = ui, server = server)

I suppose the next thing to do is to try and set Knitr options for your project...
and/or raise an issue with the sdcMicro package authors.

Thank you @nirgrahamuk, I will explore it

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.