I want to build a simple app:
- User provide some parameters that can be glued into sql query.
- Click Query to see the data.
- Click Download to download a file.
I tried with the following code. Now the Download
button doesn't work (Run the app and click Download
).
library(shiny)
library(DBI)
library(glue)
ui <- fluidPage(
titlePanel("Old Faithful Geyser Data"),
sidebarLayout(
sidebarPanel(
sliderInput("rows",
"Number of rows:",
min = 1,
max = 50,
value = 30)
),
mainPanel(
actionButton("query", "Query"),
downloadButton("downloadData"),
tableOutput("table")
)
)
)
server <- function(input, output) {
data <- eventReactive(input$query, {
req(input$rows)
# get data from database
con <- dbConnect(RSQLite::SQLite(), ":memory:")
dbWriteTable(con, "mtcars", mtcars)
sql <- glue_sql("SELECT * FROM mtcars LIMIT {input$rows}", .con = con)
df <- dbGetQuery(con, sql)
dbDisconnect(con)
df
})
output$table <- renderTable({
data()
})
output$downloadData <- downloadHandler(
filename = function() {
paste("data-", Sys.Date(), ".csv", sep="")
},
content = function(file) {
write.csv(data(), file)
}
)
}
# Run the application
shinyApp(ui = ui, server = server)