sdcTools / sdcMicro

sdcMicro
http://sdctools.github.io/sdcMicro/
79 stars 23 forks source link

Generate report on Shiny app using download button #301

Closed Ibrokhimsadikov closed 4 years ago

Ibrokhimsadikov commented 4 years ago

Dear SDCMicro team and users,

I am having trouble to generate report on my shiny app using report() function in SdcMicro. It is working fine on my local pc, but once I deploy my app it is not generating report and shiny app Disconnected from the server.

Sample code:

library(shiny)
library(sdcMicro)
library(R2HTML)

ui <- fluidPage(    
  titlePanel("Old Faithful Geyser Data"),
  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, 
      actionButton("myRepDownload", "Save report", btn.style = "primary"), align = "center"
    ))
  })

  observeEvent(input$myRepDownload, {
    report(
      sdcObj,
      outdir = getwd(),
      filename = "SDC-Report",
      title = "SDC-Report",
      internal = TRUE,
      verbose = FALSE
    )
  })
}

shinyApp(ui = ui, server = server)
bernhard-da commented 4 years ago

hi @Ibrokhimsadikov, thx for reporting this. I just tried to reproduce but aside from the fact, that in your ui_export_report there is a typo, a file called SDC-report.html is correctly created. I think you need to save that file somewhere and use shiny::downloadButton() if you want to download it.

also, i could not reproduce the "app disconnected" error you mentioned on a fully updated R 4.0.2 system.

could you please tell us, how/where you deploy this?

Ibrokhimsadikov commented 4 years ago

Thank you so much @bernhard-da for reply to my question. I am new to shiny and thus I am having trouble to incorporate Downloadbutton with report() function of SDCMicro. I am not sure on how to call downloadbutton with report() in server as file path is already specified in report(). Please help if you can

When I ran the sample code above I am getting this error on deployed shinyapp.io, may be because of the reason getwd() is in report()
Untitled_ Aug 21, 2020 5_47 PM

Main Question is how to call downloadhandler in server with report()

bernhard-da commented 4 years ago

hi @Ibrokhimsadikov i really think your issue has nothing to do with sdcMicro as the report is correctly generated. the following should however work inside your server-function.

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)
    }
  )  
Ibrokhimsadikov commented 4 years ago

Thank you so much @bernhard-da, for your help and code you shared it was very helpful to integrate downloadhandler, and it is working locally good but still getting same error while deploying app in rsconnect or shinyapps.io

Here is the logs image

Ibrokhimsadikov commented 4 years ago

Thank you @bernhard-da, Is it bug?

bernhard-da commented 4 years ago

no, not really a bug.

i think rmarkdown::render() (which is used in sdcMicro::report()) writes auxiliary files by default in the current dir; when deploying via rstudio-connect it might be that the default is to try to write in the current directory wich seems to be read-only. i have made some updates in https://github.com/sdcTools/sdcMicro/commit/49e80dc9558e890f86ce370b72d997b5b73c85e1 which i will probably merge soon into master and which will end up in the next cran version; i set parameter intermediates_dir to tempdir() which solved the issue for me (under rstudio-connect)

Ibrokhimsadikov commented 4 years ago

Thank you @bernhard-da quick response, So if I download package directly from github through devtools, will it be possible. Or it will work out once you merge it to master.

Thanks once again for your support and patience

bernhard-da commented 4 years ago

depends; you would need to install the version with the fix in rstudio-connect (which could take a while); better wait for an updated cran-version.

Ibrokhimsadikov commented 4 years ago

Sure @bernhard-da immense thanks for your support, look forward to new changes, hope it will solve the problem

Ibrokhimsadikov commented 4 years ago

Dear @bernhard-da , I hope you are good. If you remember we had issue rendering internal report when app is deployed on rsconnect or shinyapp.io. I wanted to know whether new version of package is released with this new modification, if not when we can expect release in the future.

Thank you

GregorDeCillia commented 4 years ago

There was no recent release of sdcMicro on CRAN. However, you can install the development version of this package with

remotes::install_github("sdcTools/sdcMicro")

This should also work with shinyapps.io as far as I can tell.

Ibrokhimsadikov commented 4 years ago

Thank you for prompt reply @GregorDeCillia