Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading in a file in R Shiny

Tags:

r

shiny

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 general code syntax is the following:

The UI file:

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

The server file:

#server.R
library(shiny)

shinyServer(function(input, output) {

text1 <- renderText({input$file1})
text2 <- renderText({input$file2})

file1 = read.csv(text1)
file2 = read.csv(text2)

output$plot <- renderPlot({

plot(file1[,1],file2[,2])

})


})

And so I would have expected text1 and text2 to hold strings containing the file path to where the files are but that does not seem to be the case. Ultimatley I just want to be able to read in two data sets and from there be able to do analysis to output based on those two data sets.

Of course using renderText might be the wrong idea as well so any suggestions on how to do this better is greatly appreciated.

like image 741
RustyStatistician Avatar asked Mar 10 '16 16:03

RustyStatistician


People also ask

How do I load a Shiny data in R?

Your title asks about importing a data frame into shiny. That can be done by storing the data frame either as a binary file using the save() function or a csv file using write. csv() and having the shiny app read it in using load() for a binary file or read. csv() for a csv file.

Is R Shiny easy?

On the other hand, R Shiny is an open-source package for building web applications with R. It provides a robust web framework for developing any sort of app, not only dashboards. Shiny is easy and intuitive to use, as you'll see in the examples below.

How do you put a picture in a Shiny dashboard in R?

Adding Image To add an image in Shiny is done by using renderImage() function.


1 Answers

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")
        )
    ))
)
like image 74
Xiongbing Jin Avatar answered Oct 04 '22 00:10

Xiongbing Jin