Create a gist now

Instantly share code, notes, and snippets.

@withr /server.R Secret
Created Oct 17, 2014

What would you like to do?
Flexible upload
# .libPaths("/home/tian/R/x86_64-pc-linux-gnu-library/3.0")
library(XLConnect)
shinyServer(function(input, output) {
chooseFile <- reactive({
inFile <- input$iFile
if (!is.null(inFile)) {
# Determine document format;
ptn <- "\\.[[:alnum:]]{1,5}$"
suf <- tolower(regmatches(inFile$name, regexpr(ptn, inFile$name)))
# Options for Excel documents;
if (suf %in% c('.xls', '.xlsx')) {
wb <- loadWorkbook(inFile$datapath)
sheets <- getSheets(wb)
output$ui <- renderUI({
list(
selectInput(inputId = "sheet", label = "Select a sheet:", choices = sheets),
textInput(inputId = 'arg', label = 'Additional Arguments:', value = ' '),
tags$hr()
)
})
return(list(path = inFile$datapath, suf = suf))
}
# Options for txt documents;
if (suf %in% c('.txt', '.csv')) {
output$ui <- renderUI({
list(
checkboxInput(inputId = 'header', label = 'First line as header', value = TRUE),
textInput(inputId = 'sep', label = 'Separator', value = " "),
textInput(inputId = 'quote', label = 'Quote', value = '\"'),
textInput(inputId = 'arg', label = 'Additional Arguments:', value = ' '),
tags$hr()
)
})
return(list(path = inFile$datapath, suf = suf))
}
} else {return(NULL)}
})
output$contents <- renderTable({
objFile <- chooseFile()
if (!is.null(objFile)) {
suf <- objFile$suf
# For Excel documents;
if (suf %in% c('.xls', '.xlsx')) {
Sheet <- input$sheet
if (!is.null(Sheet)){
if (input$arg %in% c(' ', '')) {
wb <- loadWorkbook(objFile$path)
dat <- readWorksheet(wb, Sheet)
return(dat)
} else {
wb <- loadWorkbook(objFile$path)
expr <- paste('readWorksheet(wb, Sheet,', input$arg, ')', sep = '')
print(expr)
dat <- eval(parse(text = expr))
return(dat)
}
} else {return(NULL)}
}
# For .txt and .csv documents;
if (suf %in% c('.txt', '.csv')) {
if (is.null(input$header)) {
dat <- read.table(objFile$path)
return(dat)
} else {
if (input$arg %in% c(' ', '')) {
dat <- read.table(objFile$path, header=input$header, sep=input$sep, quote=input$quote)
return(dat)
} else {
expr.1 <- paste('"', gsub('\\', '/', objFile$path, fixed = TRUE), '"', sep = '')
expr.2 <- paste(expr.1,
paste('header =', input$header),
paste('sep =', paste("'", input$sep, "'", sep = '')),
paste('quote =', paste("'", input$quote, "'", sep = '')), input$arg, sep = ', ')
print(expr.2)
expr <- paste('read.table(', expr.2, ')', sep = '')
print(expr)
dat <- eval(parse(text = expr))
return(dat)
}
}
}
} else {return(NULL)}
})
})
shinyUI(pageWithSidebar(
# Include css file;
tagList(
tags$head(
tags$title("Upload Data"),
tags$link(rel="stylesheet", type="text/css",href="style.css")
)
),
# Control panel;
sidebarPanel(
fileInput(inputId = "iFile", label = "", accept="application/vnd.ms-excel"),
tags$hr(),
uiOutput(outputId = "ui"),
submitButton("Upload!")
),
# Output panel;
mainPanel(tableOutput(outputId = "contents"))
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment