Skip to content

Instantly share code, notes, and snippets.

@kiadybezaka
Forked from SachaEpskamp/global.R
Created July 9, 2018 13:23
Show Gist options
  • Save kiadybezaka/a3fb9923eb6886ed0a4ae1d2360c26a1 to your computer and use it in GitHub Desktop.
Save kiadybezaka/a3fb9923eb6886ed0a4ae1d2360c26a1 to your computer and use it in GitHub Desktop.

Revisions

  1. @SachaEpskamp SachaEpskamp revised this gist Jun 17, 2013. 2 changed files with 1 addition and 2 deletions.
    1 change: 0 additions & 1 deletion server.R
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@

    shinyServer(function(input, output) {

    ### Argument names:
    2 changes: 1 addition & 1 deletion ui.R
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    shinyUI(pageWithSidebar(

    # Header:
    headerPanel("R data reader:"),
    headerPanel("R data reader"),

    # Input in sidepanel:
    sidebarPanel(
  2. @SachaEpskamp SachaEpskamp created this gist Jun 17, 2013.
    2 changes: 2 additions & 0 deletions global.R
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    library("shiny")
    library("foreign")
    102 changes: 102 additions & 0 deletions server.R
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,102 @@

    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)
    }
    )

    })


    70 changes: 70 additions & 0 deletions ui.R
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,70 @@

    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")

    )
    ))