Reputation: 43
Hello, I am working on a project using Shiny. I am extremely new to Shiny, so the fact I even made it this far is very surprising to me. I currently have something that looks like the screenshot included. It allows you to modify the original data frame by year, continent, and country, and it displays statistics of the dataframe as it changes.
I tried to incorporate a download button, however, whenever I press it (even in the browser) it tries saving as a .html instead of a .csv. I also see "Failed- server problem" each time. Is there a way to fix this? I also wanted to make sure that whatever is being downloaded is the modified dataframe the user has selected and not the original dataset.
Also, I tried to add text (similar to the number of columns) that showed the number of rows. It's super easy to do this for the original dataset, but I was not sure how to do it/display the results for when the dataframe is being manipulated by the year, country, or continent being restricted from the users view.
Any advice with either of these would be extremely helpful! I have inlcuded a file bin link belwo that contains all of the necessarry files. Thank you!
https://filebin.net/wjctohctz1sxm16y
Upvotes: 0
Views: 1800
Reputation: 9857
This will download the original data set.
Not sure what you want to name the downloaded file so I've hard-coded it to test.csv.
output$downloadData <- downloadHandler(
filename = function() {
paste("test.csv", sep = "")
},
content = function(con) {
write.csv(data, con, row.names = TRUE)
}
)
This will subset the data based on what year, country and continent is selected and download that. The filename will be made up of the selections delimited by a dash, unless no selections have been made, then the file name will be 'AllData.csv'.
output$downloadData <- downloadHandler(
filename = function() {
selected <-c()
if (input$year != "All") {
selected <-c(selected, input$year)
}
if (input$country != "All") {
selected <-c(selected, input$country)
}
if (input$continent != "All") {
selected <-c(selected, input$continent)
}
if (length(selected) == 0) {
selected <- c("AllData")
}
paste0(paste(selected, collapse="-"), ".csv")
},
content = function(con) {
if (input$year != "All") {
data <- df[df$Year == input$year,]
}
if (input$country != "All") {
data <- df[df$Country == input$country,]
}
if (input$continent != "All") {
data <- df[df$Continent == input$continent,]
}
write.csv(data, con, row.names = TRUE)
}
)
Upvotes: 1