Skip to content

Instantly share code, notes, and snippets.

@jasdumas
Created November 19, 2015 03:00
Show Gist options
  • Save jasdumas/82bce87841179428562c to your computer and use it in GitHub Desktop.
Save jasdumas/82bce87841179428562c to your computer and use it in GitHub Desktop.
## app.R ##
server <- function(input, output) {
# open the selected data set in excel on button trigger
observeEvent(input$excel,{
cat("...in open_this_reactive\n")
data = paste(input$dataset, '.csv', sep='')
cmd = paste0("open ", data)
system(cmd)
# Original open.this function
# open.this <-function(x, file = "__tmp.csv") {
# write.table(x, file = file, sep = ",", row.names = TRUE, col.names = NA)
# cmd = paste("open ", file, sep = "")
# system(cmd)
# cat("press enter to continue...\n")
# scan()
# cmd = paste("rm ", file, sep = "")
# system(cmd)
# }
# open.this(iris)
})
datasetInput <- reactive({
switch(input$dataset,
"rock" = rock,
"pressure" = pressure,
"cars" = cars)
})
########## Outputs ##############
output$contents <- renderTable({
# input$file1 will be NULL initially. After the user selects
# and uploads a file, it will be a data frame with 'name',
# 'size', 'type', and 'datapath' columns. The 'datapath'
# column will contain the local filenames where the data can
# be found.
inFile <- input$file1
if (is.null(inFile))
return(NULL)
read.csv(inFile$datapath, header=input$header, sep=input$sep,
quote=input$quote)
})
output$table <- renderTable({
datasetInput()
})
output$downloadData <- downloadHandler(
filename = function() {
paste(input$dataset, '.csv', sep='')
},
content = function(file) {
write.csv(datasetInput(), file)
}
)
}
#####################################
library(shinythemes)
ui <- fluidPage(theme = shinytheme("cosmo"),
#titlePanel('Downloading & Uploading Data'),
sidebarLayout(
sidebarPanel(
h3("Download"),
selectInput("dataset", "Choose a dataset:",
choices = c("rock", "pressure", "cars")),
downloadButton('downloadData', 'Download'),
actionButton('excel', 'View in Excel'),
hr(),
#######
h3("Upload"),
fileInput('file1', 'Choose CSV File',
accept=c('text/csv',
'text/comma-separated-values,text/plain',
'.csv')),
checkboxInput('header', 'Header', TRUE),
radioButtons('sep', 'Separator',
c(Comma=',',
Semicolon=';',
Tab='\t'),
','),
radioButtons('quote', 'Quote',
c(None='',
'Double Quote'='"',
'Single Quote'="'"),
'"')
),
mainPanel(
fluidRow(
column(6, h4("Download"),tableOutput('table')),
column(6, h4("Upload"),tableOutput('contents'))
)
)
)
)
#####################################
shinyApp(ui = ui, server = server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment