Why does my download button activate twice in shiny app? - r

So I have a shiny app (https://github.com/tadeu95/BAGs) (http://tadeu-apps.shinyapps.io/bags) and I have several download buttons. Recently what has started to happen is that when I click the download button it activates two times and downloads the same file twice. The thing is that the app worked for almost a year without any problem.
I've tried on chrome, edge and mozzila and happens every time. I don't know what happened because I didn't touch the part of the code where the downloads are implemented.
One thing I've just discovered is that if instead of clicking the download button with the left button of the mouse, I click the right button and choose "open link in a new tab", it downloads the file correctly only once.
This is a short reproducible app, I advise that you give as input "ursidae" as it downloads the file pretty quickly:
library(bold)
library(readr)
library(shiny)
library(shinyWidgets)
grades2<-function(groups){
taxon9<-bold_seqspec(taxon=groups, format = "tsv")
}
ui <- fluidPage(textInputAddon(inputId="taxa2",addon=icon("search"),width="500px",label=tags$h5(tags$strong("Enter the name of the taxonomic group or groups separated by commas, without spaces:")),placeholder="Example: Carnivora,Ursidae,Artiodactyla,Soricomorpha"),downloadBttn("downloadData_2",size="sm","Download"))
server <- function(input, output) {
taxaInput_2 <- reactive({grades2(unlist(strsplit(input$taxa2, ",")))})
output$downloadData_2 <- downloadHandler(
filename = function() {
paste(input$taxa2,sep_out=",", ".tsv")
},
content = function(file) {
shiny::withProgress(
message=paste0("Downloading and annotating library for ",input$taxa2,sep_out=","), detail='This may take several minutes',
value=10,
{
shiny::incProgress(10/10)
write_tsv(taxaInput_2(), file)
}
)
}
)
}
shinyApp(ui=ui,server=server)
If anyone has any idea what the reason might be, I will be very thankful.

Related

Shiny downloadHandler wait for data to be ready

The data in my shiny application takes a few seconds to be processed.
I have a download button and I would like it to be either unclickable until the data are prepared or have the download handler wait until prepared.
At the moment clicking the download button before 5 secs returns the default html file and then after 5 secs it behaves as expected.
My current solution is to use `shinyjs::hide/show. I’ve shown this below.
Is this best practice? Also, why the shinyjs::useShiny() at the start? It seems unique to that package.
ui <- fluidPage(
shinyjs::useShiny(),
shinyjs::hidden(downloadButton("downloadData", "Download"))
)
server <- function(input, output) {
# Our dataset
data <- mtcars
if(is.null(mtcars)){shinyjs::hide(“downloadData”)}
else{shinyjs::show(“downloadData”)}
output$downloadData <- downloadHandler(
filename = function() {
paste("data-", Sys.Date(), ".csv", sep="")
},
content = function(file) {
write.csv(data, file)}
)
}
shinyApp(ui, server)
What you describe is perfectly reasonable and has been suggested on stackoverflow in the past. You can also use disable/enable instead of hide/show, whatever you feel is a better user experience.
The useShinyjs() is required to set up all the R <--> JavaScript communication that is happening. Without it, when you try to call a shinyjs function from the server, nothing will happen because the UI didn't get initialized with the javascript. Some other pacakges have also adopted this pattern since I made shinyjs, especially packages that also deal with javascript.

R shiny dynamically render audio [close]

I am working on a project, trying to render audio dynamically. That is, I can click a button and play a selected local audio.
I have read this post and tried zedii's trick. The problem remains still.
Well I build a test app as shown below.
library(shiny)
# test set ----
ui <- fluidPage(
textInput('my_music','path:',value="questionF"),
actionButton("ok", "Okay"),
uiOutput('my_audio')
# tags$audio(src = "questionF.mp3", type = "audio/mp3")
)
get_audio_tag <- function(filename) {
tags$audio(src = filename,
type = "audio/mp3",
controls = "controls")
}
server <- function(input, output, session){
# Render the audio player
observeEvent(input$ok, {
wav_name = input$my_music
# output$my_audio <-renderUI(get_audio_tag("questionF.mp3"))
output$my_audio <-renderUI(get_audio_tag(wav_name))
})
}
shinyApp(ui = ui, server = server)
When I click the button, the first song turns out okay. But the following ones seemed pretty hard to load, for my computer would freeze with the memory used by Rstudio rising.
Any thoughts will be appreciated.
updates:
I tried on different browsers. These codes would failed on Chrome but works fine on Microsoft edges.
Looks like a caching problem.
So now my question is how can I make the codes work on every platform using shiny/R?
My codes works on most browser other than Chrome. I think it's more like a Chrome's problem.

r shiny: allow user to download shiny output in a desired directory

In my Shiny app, I run calculations, generate a data frame, and want the user to be able to save (download) it.
My relevant code in ui.R:
textInput("downFile","Save the File:", value = "Results File"),
downloadButton('downFile',"Save File"),
My relevant code in server.R:
output$downFile <- downloadHandler(
filename = function() {
paste0(input$downFile, " ", Sys.Date(), ".csv")
},
content = function(file) {
write.csv(MyMainFunction()$mydataframe, file, row.names = FALSE)
}
)
Everything works. But I have two issues:
If by mistake the user clicks on "Save File" before all the calculations in server.R have been completed, he gets an error ("Failed - Network Error").
After all the calculations, when the user clicks on the "Save File" button, the file is immediately saved in "Downloads" folder.
Two questions:
Is it possible to make the button invisible until the calculations in my main function (MainFunction) are completed?
How could I allow the user to select a desired folder to download the results to? (as in "Save in...")
Thank you very much!
for question 1: Use shinyjs package learn more in https://deanattali.com/shinyjs/
library(shinyjs)
disable("downFile") # or use hide() Before all calculations
enable("downFile") # or use show() After
For question 2: I think this is more about setting up your browser than the shiny
If you are using chrome go to the advanced settings and enable

How do I get the R Shiny downloadHandler filename to work?

I am setting up a Shiny app that allows the user to download a custom dataset. Following the tutorial, I set up the downloadHandler following the example given in the docs (reproduced here, since the same thing happens if I copy and paste this).
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)
Problem:
This issue only comes up on my Linux* system and seems to work just fine on a Mac. The download and everything works just fine, but the "Save" GUI does not offer me the right file name. There is no error message or warning. Based on my input,
I'd expect it to give me data-TIME.csv, i.e. the input to filename. (It does not work either if I give it simple string in that slot).
but it offers me DownloadData or whatever name I give to the output variable (cf. screenshot).
Question:
Is this a OS issue as I suspect, or am I doing something wrong?
How do i fix this? Can I get this to work on any system?
Thanks!
I'm running elementary OS 0.4 Loki, Built on "Ubuntu 16.04.2 LTS", GTK version: 3.18.9. & RStudio 1.0.143
If you are using the Rstudio Browser to test you App this could be the problem. I have the same issue on Windows.
When I use the Rstudio Browser the filename is not properly hand over, but if I use Firefox everything works fine. Your code works also fine in my Firefox.

Download handler does not save file shiny R

library(shiny)
library(plyr)
shinyServer(function(input, output){
myfile<-reactive({
#reading 3 csv files and merging them into a dataframe
})
output$downloadData<-downloadHandler(
filename = function(){
paste("mergedfile","csv",sep='.')
},
content= function(file){
write.csv(myfile(),file,sep=",")
}
)
})
I am reading 3-4 files reactively and then merging them. After that without displaying them I need to download the merged file.
I wrote the above code but a dialog box opens which asks me where to save but the file doesn't get saved. Am I doing something wrong with the download handler.
ui.R
downloadButton('downloadData','Download')
This is what I have in my main panel in ui.R file
Your probably using the Rstudio viewer to run the app? Open the app in your browser and your code will work
( click open in borwser or run runApp('/path/to/myApp',launch.browser=T) ).
See this link.
Also no need to set sep="," for write.csv since this is the default.

Resources