Skip to content

Instantly share code, notes, and snippets.

@cecilialee
Created February 24, 2018 03:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cecilialee/9e359220da4cefe045b8158e1b6ac3b8 to your computer and use it in GitHub Desktop.
Save cecilialee/9e359220da4cefe045b8158e1b6ac3b8 to your computer and use it in GitHub Desktop.
Filter datatable columns with download option in Shiny. #r #shiny
library(shiny)
library(dplyr)
library(readr)
load(url("http://s3.amazonaws.com/assets.datacamp.com/production/course_4850/datasets/movies.Rdata"))
# UI
ui <- fluidPage(
sidebarLayout(
# Input(s)
sidebarPanel(
# Select filetype
radioButtons(inputId = "filetype",
label = "Select filetype:",
choices = c("csv", "tsv"),
selected = "csv"),
# Select variables to download
checkboxGroupInput(inputId = "selected_var",
label = "Select variables:",
choices = names(movies),
selected = c("title"))
),
# Output(s)
mainPanel(
DT::dataTableOutput(outputId = "moviestable"),
downloadButton("download_data", "Download data")
)
)
)
# Server
server <- function(input, output) {
# Create reactive data frame
movies_selected <- reactive({
req(input$selected_var)
movies %>% select(input$selected_var)
})
# Create data table
output$moviestable <- DT::renderDataTable({
DT::datatable(data = movies_selected(),
options = list(pageLength = 10),
rownames = FALSE)
})
# Download file
output$download_data <- downloadHandler(
filename = function() {
paste0("movies.", input$filetype)
},
content = function(file) {
if(input$filetype == "csv"){
write_csv(movies_selected(), file)
}
if(input$filetype == "tsv"){
write_tsv(movies_selected(), file)
}
}
)
}
# Create a Shiny app object
shinyApp(ui = ui, server = server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment