Reading in a file in R Shiny

前端 未结 1 1881
梦毁少年i
梦毁少年i 2020-12-03 23:13

So I am building an app in R shiny that requires the user to upload a .csv file. Once read in by R shiny, I am unsure how to actually manipulate that object to use. The gene

相关标签:
1条回答
  • 2020-12-03 23:21

    There is a good example here http://shiny.rstudio.com/gallery/file-upload.html. But for completeness, I've included the working answer below. The key point is that you should reference the file using file$datapath, and also to check if input is NULL (when user hasn't uploaded a file yet).

    server.R

    #server.R
    library(shiny)
    
    shinyServer(function(input, output) {
    
        observe({
            file1 = input$file1
            file2 = input$file2
            if (is.null(file1) || is.null(file2)) {
                return(NULL)
            }
            data1 = read.csv(file1$datapath)
            data2 = read.csv(file2$datapath)
            output$plot <- renderPlot({
                plot(data1[,1],data2[,2])
            })
        })
    
    })
    

    ui.R

    library(shiny)
    
    #ui.R
    # Define UI for random distribution application 
    shinyUI(fluidPage(
    
        # Application title
        titlePanel("ORR Simulator"),
    
        # Sidebar with controls to select the random distribution type
        # and number of observations to generate. Note the use of the
        # br() element to introduce extra vertical spacing
        sidebarLayout(
            sidebarPanel(
                fileInput('file1', 'Select the XXX.csv file',
                          accept=c('text/csv','text/comma-separated-values,text/plain','.csv')),
                tags$hr(),
                fileInput('file2', 'Select the YYY.csv file',
                          accept=c('text/csv','text/comma-separated-values,text/plain','.csv')),
                tags$hr(),
                numericInput("S", "Number of simulations to run:", 100)
            ),
            mainPanel(
                plotOutput("plot")
            )
        ))
    )
    
    0 讨论(0)
提交回复
热议问题