Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A general shiny app to import and export data to R. Note that this can be used as a starting point for any app that requires data to be loaded into Shiny.
library("shiny")
library("foreign")
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)
}
)
})
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")
)
))
@MingChen0919

This comment has been minimized.

Copy link

@MingChen0919 MingChen0919 commented Jun 29, 2016

This is great! Thanks!

@markrazmandi

This comment has been minimized.

Copy link

@markrazmandi markrazmandi commented Dec 31, 2016

Very comprehensive indeed. Thank you kindly for the reference!

@gu-stat

This comment has been minimized.

Copy link

@gu-stat gu-stat commented Jun 24, 2017

Hello, very good work! However, I can't seem to make the "variables to use" feature to work. I can only select one at a time and it doesn't seem to affect the import. Any thoughts on that?

Thanks!

@easbi

This comment has been minimized.

Copy link

@easbi easbi commented Jul 20, 2018

This is Good but when i tried to use readxl package as function reader, can we make some parameter to be default? because it i left that with empty its begin to error. i dont find the way for fix this problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment