I am working on a Shiny app and have it nearly done except that one of my download buttons isn’t getting through to the downloadHandler and just downloads an HTML file of the shiny app itself. The project is big so here are, I think, the relevant parts. First, the UI has two tabs. One tab has two download buttons, the other had one. The two download buttons on the first tab work fine, but when I try to replicate their code on the second tab, I run into this problem. I have confirmed that if I remove the first tab, the button works.
Here is the UI for this second tab:
<code>no_compare_elements <- function() {
accepted_data_types <- c("text/csv", "text/comma-separated-values,text/plain", ".csv")
file_input_width <- "100%"
class = "data-upload-container",
id = "introText_Non_Comparability",
column(4, selectInput('timeComparisons_NC',
"Time Comparisons (click and add all that apply)",
c("T5-T4", "T5-T3", "T5-T2", "T5-T1", "T4-T3",
"T4-T2", "T4-T1", "T3-T2", "T3-T1", "T2-T1"),
width = "80%", multiple = TRUE, selected = c("T4-T1", "T4-T3"))),
column(4, selectInput('startTime_NC',
c("T1", "T2", "T3", "T4", "T5"),
width = "80%", selected = "T1")),
column(4, selectInput('endTime_NC', "Last Time Period",
c("T1", "T2", "T3", "T4", "T5"),
width = "80%", selected = "T4"))
fluidRow(column(12, fileInput("cond_change_trend_file", "Upload Condition file with Trend",
accept = accepted_data_types, width = file_input_width
actionButton(width = "100%", "run_non_compare", "Generate Data Files", class = "btn btn-primary"),
"false", # always hide the download button
downloadButton("downloadNC"),
<code>no_compare_elements <- function() {
accepted_data_types <- c("text/csv", "text/comma-separated-values,text/plain", ".csv")
file_input_width <- "100%"
tags$div(
shinyjs::useShinyjs(),
class = "data-upload-container",
tags$div(
id = "introText_Non_Comparability",
tags$div(
fluidRow(
column(4, selectInput('timeComparisons_NC',
"Time Comparisons (click and add all that apply)",
c("T5-T4", "T5-T3", "T5-T2", "T5-T1", "T4-T3",
"T4-T2", "T4-T1", "T3-T2", "T3-T1", "T2-T1"),
width = "80%", multiple = TRUE, selected = c("T4-T1", "T4-T3"))),
column(4, selectInput('startTime_NC',
"First Time Period",
c("T1", "T2", "T3", "T4", "T5"),
width = "80%", selected = "T1")),
column(4, selectInput('endTime_NC', "Last Time Period",
c("T1", "T2", "T3", "T4", "T5"),
width = "80%", selected = "T4"))
),
fluidRow(column(12, fileInput("cond_change_trend_file", "Upload Condition file with Trend",
accept = accepted_data_types, width = file_input_width
))),
actionButton(width = "100%", "run_non_compare", "Generate Data Files", class = "btn btn-primary"),
conditionalPanel(
"false", # always hide the download button
downloadButton("downloadNC"),
)
)
)
}
</code>
no_compare_elements <- function() {
accepted_data_types <- c("text/csv", "text/comma-separated-values,text/plain", ".csv")
file_input_width <- "100%"
tags$div(
shinyjs::useShinyjs(),
class = "data-upload-container",
tags$div(
id = "introText_Non_Comparability",
tags$div(
fluidRow(
column(4, selectInput('timeComparisons_NC',
"Time Comparisons (click and add all that apply)",
c("T5-T4", "T5-T3", "T5-T2", "T5-T1", "T4-T3",
"T4-T2", "T4-T1", "T3-T2", "T3-T1", "T2-T1"),
width = "80%", multiple = TRUE, selected = c("T4-T1", "T4-T3"))),
column(4, selectInput('startTime_NC',
"First Time Period",
c("T1", "T2", "T3", "T4", "T5"),
width = "80%", selected = "T1")),
column(4, selectInput('endTime_NC', "Last Time Period",
c("T1", "T2", "T3", "T4", "T5"),
width = "80%", selected = "T4"))
),
fluidRow(column(12, fileInput("cond_change_trend_file", "Upload Condition file with Trend",
accept = accepted_data_types, width = file_input_width
))),
actionButton(width = "100%", "run_non_compare", "Generate Data Files", class = "btn btn-primary"),
conditionalPanel(
"false", # always hide the download button
downloadButton("downloadNC"),
)
)
)
}
Here is the server part:
<code>app_server <- function(input, output, session) {
final_data<-reactiveVal()
user_metadata <- reactiveVal()
user_metadata_file_name <- reactiveVal()
cond_change_trend_file_NR <- reactiveVal()
cond_change_trend_file_NR_fileName <- reactiveVal()
observeEvent(input$create_scripts, {
### code to observe events on the first tab
### this is the button on the second tab
observeEvent(input$run_non_compare, {
startTime_NC <- input$startTime_NC
endTime_NC <- input$endTime_NC
timeComparisons_NC <- input$timeComparisons_NC
fileName_NC <- input$cond_change_trend_file$name
fileName_NC <- gsub("\.csv$", "", fileName_NC)
fileName_NC <- paste0(fileName_NC, "_NR.csv")
cond_change_trend_file <- readr::read_csv(input$cond_change_trend_file$datapath)
cond_change_trend_file_NR_data <- addressNonComparabilityIssues(cond_change_trend_file, c(startTime_NC, endTime_NC), timeComparisons_NC)
cond_change_trend_file_NR(cond_change_trend_file_NR_data)
cond_change_trend_file_NR_fileName(fileName_NC)
shinyjs::click("downloadNC")
output$downloadNC <- downloadHandler(
cond_change_trend_file_NR_fileName()
content = function(file) {
write.csv(cond_change_trend_file_NR(), file, row.names = FALSE)
output$downloadData <- downloadHandler(
content = function(file) {
write.csv(final_data(), file, row.names = FALSE)
output$downloadMetaData <- downloadHandler(
user_metadata_file_name()
content = function(file) {
write.csv(user_metadata(), file, row.names = FALSE)
<code>app_server <- function(input, output, session) {
final_data<-reactiveVal()
file_name<-reactiveVal()
user_metadata <- reactiveVal()
user_metadata_file_name <- reactiveVal()
cond_change_trend_file_NR <- reactiveVal()
cond_change_trend_file_NR_fileName <- reactiveVal()
observeEvent(input$create_scripts, {
### code to observe events on the first tab
})
### this is the button on the second tab
observeEvent(input$run_non_compare, {
startTime_NC <- input$startTime_NC
endTime_NC <- input$endTime_NC
timeComparisons_NC <- input$timeComparisons_NC
fileName_NC <- input$cond_change_trend_file$name
fileName_NC <- gsub("\.csv$", "", fileName_NC)
fileName_NC <- paste0(fileName_NC, "_NR.csv")
# print(fileName_NC)
cond_change_trend_file <- readr::read_csv(input$cond_change_trend_file$datapath)
cond_change_trend_file_NR_data <- addressNonComparabilityIssues(cond_change_trend_file, c(startTime_NC, endTime_NC), timeComparisons_NC)
cond_change_trend_file_NR(cond_change_trend_file_NR_data)
cond_change_trend_file_NR_fileName(fileName_NC)
shinyjs::click("downloadNC")
})
# DOWNLOAD BUTTONS
output$downloadNC <- downloadHandler(
filename = function() {
print('here)
cond_change_trend_file_NR_fileName()
},
content = function(file) {
write.csv(cond_change_trend_file_NR(), file, row.names = FALSE)
})
output$downloadData <- downloadHandler(
filename = function() {
file_name()
},
content = function(file) {
write.csv(final_data(), file, row.names = FALSE)
})
output$downloadMetaData <- downloadHandler(
filename = function() {
user_metadata_file_name()
},
content = function(file) {
write.csv(user_metadata(), file, row.names = FALSE)
})
}
</code>
app_server <- function(input, output, session) {
final_data<-reactiveVal()
file_name<-reactiveVal()
user_metadata <- reactiveVal()
user_metadata_file_name <- reactiveVal()
cond_change_trend_file_NR <- reactiveVal()
cond_change_trend_file_NR_fileName <- reactiveVal()
observeEvent(input$create_scripts, {
### code to observe events on the first tab
})
### this is the button on the second tab
observeEvent(input$run_non_compare, {
startTime_NC <- input$startTime_NC
endTime_NC <- input$endTime_NC
timeComparisons_NC <- input$timeComparisons_NC
fileName_NC <- input$cond_change_trend_file$name
fileName_NC <- gsub("\.csv$", "", fileName_NC)
fileName_NC <- paste0(fileName_NC, "_NR.csv")
# print(fileName_NC)
cond_change_trend_file <- readr::read_csv(input$cond_change_trend_file$datapath)
cond_change_trend_file_NR_data <- addressNonComparabilityIssues(cond_change_trend_file, c(startTime_NC, endTime_NC), timeComparisons_NC)
cond_change_trend_file_NR(cond_change_trend_file_NR_data)
cond_change_trend_file_NR_fileName(fileName_NC)
shinyjs::click("downloadNC")
})
# DOWNLOAD BUTTONS
output$downloadNC <- downloadHandler(
filename = function() {
print('here)
cond_change_trend_file_NR_fileName()
},
content = function(file) {
write.csv(cond_change_trend_file_NR(), file, row.names = FALSE)
})
output$downloadData <- downloadHandler(
filename = function() {
file_name()
},
content = function(file) {
write.csv(final_data(), file, row.names = FALSE)
})
output$downloadMetaData <- downloadHandler(
filename = function() {
user_metadata_file_name()
},
content = function(file) {
write.csv(user_metadata(), file, row.names = FALSE)
})
}
The downloadData and downloadMetaData handlers (both on the first tab) work fine but the downloadNC handler has the problem. I’ve confirmed that the filename and data are fine. The print statement within the downloadNC handler doesn’t fire at all. When I inspect the buttons in devtools, the working buttons have hrefs, but this non-working button doesn’t. For some reason it’s not getting connected to the downloadHandler. Could there be an asynchronous issue? Why does it being on the second tab matter?