1

I have met an issue when I try to implement a download button in Shiny App. Every time I run the app, it will only show me an HTML file not the actual content file. Here is my code for both the server and UI parts.

library(shiny)
library(reticulate)

shinyServer(function(input,output){

  reticulate::source_python("function.py")
  data_xi <- run_xi(26)

  output$downloadData <- downloadHandler(

    filename = function(){
      paste(Sys.time(), 'site_mtx.xlsx')
    },

    content = function(file){
      write_xlsx(data_xi, file)
    }
  )
})

Here is the UI:

library(shiny)

shinyUI(fluidPage(
    downloadButton("downloadData", "Download Metrics Reports")
))

I just tried to use the reticulate function in my python file and save the processed dataframe to Shiny App which can be downloads, thank you very much!

YihanBao
  • 501
  • 1
  • 4
  • 15

1 Answers1

1

I ran an example out of your code with some adjustions (unfortunately i don't have your file) and it downloads normally a xlsx file. Add the data.frame( run_xi(26))and if this is not the problem maybe the "writexl" library can be the solution. Hope it will help.

library(shiny)
library(reticulate)
library(writexl)


if (interactive()) {

ui <-fluidPage(
    downloadButton("downloadData", "Download Metrics Reports")
)


server <- function(input,output){

data_xi <- data.frame(s = c(1:3),r = c(4:6), x =c(19:21))


    output$downloadData <- downloadHandler(

        filename = function(){
            paste(Sys.time(), 'site_mtx.xlsx')
        },

        content = function(file){
            write_xlsx(data_xi, file)
        }
    )
}

shinyApp(ui, server)

}

Simos Lazarou
  • 119
  • 1
  • 5
  • Hi! Thanks a lot and I can run it with your code and the function.py is just to extract the data from the database and the output is a data frame. Then I just tried to replace your code "data_xi <- data.frame(s = c(1:3),r = c(4:6), x =c(19:21))" to my code "reticulate::source_python("function.py") data_xi <- run_xi(26)" but it seems it can't open the browser even, I think It is the issue with how to use reticulate library in R? – YihanBao Jun 14 '20 at 00:57
  • I am not familiar with the Reticulate library but i yeah i agree is something about it, it seems that your python fuction doesn't get imported properly. I would also double check the data_xi data type with "class(data_xi)". Hope you will solve your problem! :) – Simos Lazarou Jun 14 '20 at 14:30