Shiny downloadHandler not working - r

I have a Shiny downloadHandler
in server.R:
output$DownloadButton <- downloadHandler(
filename = function() {
paste("test", Sys.Date(), ".csv",sep="")
},
content = function(con) {
print("in download")
print(con) # this prints C:\\Users\\me\\Local\\Temp\\RtmpI1EjY7\\file668338e4c33
Data<-ReactiveGetData()$Data #Here I get the data I want to download
print(head(Data)) #This prints out the data with no errors
write.csv(Data, con)
}
)
here is ui.r:
sidebarPanel(
downloadButton("DownloadButton", label = "Download",class = NULL), ....
So far it printed the temp file:
C:\\Users\\me\\Local\\Temp\\RtmpI1EjY7\\file668338e4c33
BUT When I go to this path manually I get an error saying "File not found"
and then when I click on the download button I do not get an error and nothing happens.
Any idea why the temp file doesn't seem to be created?
Should the temp file end in csv?
HERE IS AN EVER SIMPLER EXAMPLE which you can run if you run the server.r and ui.r files belwo. I cannot download the file below:
The "file" object does not exist below any idea why?
ui.r
library(shiny)
shinyUI(fluidPage(
sidebarPanel(
downloadButton("Download", label = "Download",class = NULL)
),
mainPanel(
tabsetPanel(
tabPanel("test",
h3("test")
)
)
)
))
server.r
library(rJava)
shinyServer(function(input, output, session) {
output$Download <- downloadHandler(
filename = function() {
paste("test.csv",sep="")
},
content = function(file) {
print("in download")
print(file) #this file does not exist ???
Data<- data.frame(name= c(1,2,3,4))
print(head(Data))
write.csv(Data, file)
}
)
})#end of server function
you can run this by:
library(rJava)
library(shiny)
runApp("C://Users//me//pathToShinyProjectFolder")
SOULTION: click "open in browser" in upper left and user CHROME OR FIREFOX as default browser.

Try opening the application in another browser. Not all browsers are created equally. This can be done by simply typing the following in another browser of your choosing.
localhost:5586
Note, that the port number may be different for you.

Related

How to have users download pre-loaded, formatted excel document in R Shiny?

I'm building a Shiny App where users complete a survey, and based on their responses, it suggests different templates for them to use. The templates are all excel files that are heavily formatted (e.g., have pictures on them, misaligned headings, etc.), like the screenshot that I've uploaded here. Unfortunately, stackoverflow won't let me upload the excel file to make this fully reproducible, but if you can run it with any non-tabular excel file, it'll work.[![enter image description here][1]][1]
These templates are all uploaded to the server, and the users input does not affect them. I've tried following the example by others, like this [one][2], but I keep getting errors.
How do I get it so when users click the download button, they get the excel file exactly as it appears?
library(readxl)
library(shiny)
library(writexl)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
),
mainPanel(
downloadButton("downloadData", "Download Fancy Excel File")
)))
server <- function(input, output) {
output$downloadData <- downloadHandler(
filename = function() {
paste("file", "xlsx", sep='')
},
content = function(file) {
myfile <- srcpath <- 'Home/Other Layer/Fancy Template.xlsx'
file.copy(myfile, file)
}
)
}
# Run the application
shinyApp(ui = ui, server = server)
~~~~
[1]: https://i.stack.imgur.com/FK034.png
[2]: https://stackoverflow.com/questions/39449544/shiny-download-an-excel-file
You are missing a . in the file name. Also, you can keep all the files you want the users to download in www folder. The following works for me.
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
),
mainPanel(
downloadButton("downloadData", "Download Fancy Excel File")
)))
server <- function(input, output) {
output$downloadData <- downloadHandler(
filename = function() {
paste("testfile", ".xlsx", sep='')
},
content = function(file) {
# myfile <- srcpath <- 'Home/Other Layer/Fancy Template.xlsx'
myfile <- srcpath <- "./www/test141.xlsx"
file.copy(myfile, file)
}
)
}
# Run the application
shinyApp(ui = ui, server = server)

Shiny App Downloads Button Only respond with HTML

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

R shiny trigger downloadHandler on page loading

I'm building an app that operates regularly when nothing is given in a url query, however if a certain string is given there it should download the file immediately. The download works fine, but when it is run at start up it return a 'download.htm' file instead of the .csv. The reproducible example is not querying the url, but triggers in an observe:
library(shiny)
library(shinyjs)
ui <- fluidPage(
useShinyjs()
,downloadButton("downloadData", "Download")
)
server <- function(input, output) {
data <- mtcars
observe({
print("click MacClickFace")
runjs("document.getElementById('downloadData').click();")
})
output$downloadData <- downloadHandler(
filename = function() {
paste("data-", Sys.Date(), ".csv", sep="")
},
content = function(file) {
write.csv(data, file)
}
)
}
shinyApp(ui, server)
How can you trigger the download at launch or are there some security things going on here?
Probably the downloadHandler is not ready. You can use a setTimeout(..., 0):
runjs("setTimeout(function(){document.getElementById('downloadData').click();},0);")

Shiny downloadButton and downloadLink default directory or filename

The download handler is a remarkably popular shiny tool - even for local work. However it is quite annoying when it opens up all the time in the R-Studio directory. Here is the current example program from the Shiny documantion.
library(shiny)
## Only run examples in interactive R sessions
if (interactive()) {
ui <- fluidPage(
downloadLink("downloadData", "Download")
)
server <- function(input, output) {
# Our dataset
data <- mtcars
output$downloadData <- downloadHandler(
filename = function() {
paste("data-", Sys.Date(), ".csv", sep="")
},
content = function(file) {
write.csv(data, file)
}
)
}
shinyApp(ui, server)
}
It opens like this:
A few related questions:
Is there anyway to override that directory to something more reasonable?
Can I specify the default name anywhere (the default is a bit bizarrely take from the name of the output list name).
Any insights as to why this is so would be welcome. There seems to be nothing in the docs around this.

"Download" button opens a new app window without downloading - Shiny

My "download" button does not work as expectation. It opens a new app window every time I click on it. I am wondering why it functions in this way?
download function in server.R:
output$down_load <- downloadHandler(
# specify the file name
filename = function() {
paste('cls_result_export', Sys.Data(),'.csv', sep='')
},
# Write the plot back
content = function(file){
write.csv(cls_output()$raw_data, file)
}
)
download function in ui.R:
downloadButton(outputId = "down_load", label = "Download the CLS Raw Data")
Try using an actionButton wired to an observe clause like this:
library(shiny)
ui <- fluidPage( actionButton("dodo", "Download" ) )
server <- function(input, output)
{
observe({
if (input$dodo>0){
fname <- paste0('cls_result_export', Sys.Date(),'.csv')
write.csv(mtcars,fname)
}
})
}
shinyApp(ui = ui, server = server)
Another possible way to try to fix this issue, is to include this line in your server.R script:
outputOptions(output, 'down_load', suspendWhenHidden=FALSE)

Resources