-
-
Save gdemin/78b510516e33a1178136 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library("shiny") | |
library("foreign") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
shinyServer(function(input, output) { | |
### Argument names: | |
ArgNames <- reactive({ | |
Names <- names(formals(input$readFunction)[-1]) | |
Names <- Names[Names!="..."] | |
return(Names) | |
}) | |
# Argument selector: | |
output$ArgSelect <- renderUI({ | |
if (length(ArgNames())==0) return(NULL) | |
selectInput("arg","Argument:",ArgNames()) | |
}) | |
## Arg text field: | |
output$ArgText <- renderUI({ | |
fun__arg <- paste0(input$readFunction,"__",input$arg) | |
if (is.null(input$arg)) return(NULL) | |
Defaults <- formals(input$readFunction) | |
if (is.null(input[[fun__arg]])) | |
{ | |
textInput(fun__arg, label = "Enter value:", value = deparse(Defaults[[input$arg]])) | |
} else { | |
textInput(fun__arg, label = "Enter value:", value = input[[fun__arg]]) | |
} | |
}) | |
### Data import: | |
Dataset <- reactive({ | |
if (is.null(input$file)) { | |
# User has not uploaded a file yet | |
return(data.frame()) | |
} | |
args <- grep(paste0("^",input$readFunction,"__"), names(input), value = TRUE) | |
argList <- list() | |
for (i in seq_along(args)) | |
{ | |
argList[[i]] <- eval(parse(text=input[[args[i]]])) | |
} | |
names(argList) <- gsub(paste0("^",input$readFunction,"__"),"",args) | |
argList <- argList[names(argList) %in% ArgNames()] | |
Dataset <- as.data.frame(do.call(input$readFunction,c(list(input$file$datapath),argList))) | |
return(Dataset) | |
}) | |
# Select variables: | |
output$varselect <- renderUI({ | |
if (identical(Dataset(), '') || identical(Dataset(),data.frame())) return(NULL) | |
# Variable selection: | |
selectInput("vars", "Variables to use:", | |
names(Dataset()), names(Dataset()), multiple =TRUE) | |
}) | |
# Show table: | |
output$table <- renderTable({ | |
if (is.null(input$vars) || length(input$vars)==0) return(NULL) | |
return(Dataset()[,input$vars,drop=FALSE]) | |
}) | |
### Download dump: | |
output$downloadDump <- downloadHandler( | |
filename = "Rdata.R", | |
content = function(con) { | |
assign(input$name, Dataset()[,input$vars,drop=FALSE]) | |
dump(input$name, con) | |
} | |
) | |
### Download save: | |
output$downloadSave <- downloadHandler( | |
filename = "Rdata.RData", | |
content = function(con) { | |
assign(input$name, Dataset()[,input$vars,drop=FALSE]) | |
save(list=input$name, file=con) | |
} | |
) | |
}) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
shinyUI(pageWithSidebar( | |
# Header: | |
headerPanel("R data reader"), | |
# Input in sidepanel: | |
sidebarPanel( | |
tags$style(type='text/css', ".well { max-width: 20em; }"), | |
# Tags: | |
tags$head( | |
tags$style(type="text/css", "select[multiple] { width: 100%; height:10em}"), | |
tags$style(type="text/css", "select { width: 100%}"), | |
tags$style(type="text/css", "input { width: 19em; max-width:100%}") | |
), | |
# Select filetype: | |
selectInput("readFunction", "Function to read data:", c( | |
# Base R: | |
"read.table", | |
"read.csv", | |
"read.csv2", | |
"read.delim", | |
"read.delim2", | |
# foreign functions: | |
"read.spss", | |
"read.arff", | |
"read.dbf", | |
"read.dta", | |
"read.epiiinfo", | |
"read.mtp", | |
"read.octave", | |
"read.ssd", | |
"read.systat", | |
"read.xport", | |
# Advanced functions: | |
"scan", | |
"readLines" | |
)), | |
# Argument selecter: | |
htmlOutput("ArgSelect"), | |
# Argument field: | |
htmlOutput("ArgText"), | |
# Upload data: | |
fileInput("file", "Upload data-file:"), | |
# Variable selection: | |
htmlOutput("varselect"), | |
br(), | |
textInput("name","Dataset name:","Data"), | |
downloadLink('downloadDump', 'Download source'), | |
downloadLink('downloadSave', 'Download binary') | |
), | |
# Main: | |
mainPanel( | |
tableOutput("table") | |
) | |
)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment