How can I pass inputFile to code?

I am trying to write an app that takes a csv file as inputFile from a user and then passes that dataset (a csv file) to a block of code in the file "EstimateModels.R", but I can't seem to get it to work.

I've tried to save the inputFile in an eventReactive() with return(data) and I still can't seem to get the file passed on to the next step in the processing.

The "EstimateModels.R" file contains lines that expect object named "data" which is provided by the user's inputfile, but that object will only exist after I can successfully pass the user's file on to it. Here is basically what I'm trying to do:

ui <- fluidRow(
box(title="Controls", fileInput('csvFile',"Select file")))

server <- function(input, output){
data <- read.csv(input$csvFile$databath, header=FALSE)

Your EstimateModels.R should contain a function to which you can pass the data frame produced by read.csv as an argument.

I made my own EstimateModels.R that contains the function RSQout that returns the R Squared of a linear fit

RSQout <- function (data) {
  FIT <- lm(data$y ~ data$x)

Here is a simple app.R file that allows the user to pick a file to use as the input of RSQout and then shows the R Squared. Notice that I assume the data file has a header for columns labeled x and y.


ui <- fluidRow(
  fileInput('csvFile',"Select file"),
  uiOutput(outputId = "RSQtext")

server <- function(input, output) {
  GetRSQ <- eventReactive(input$csvFile, { 
    data <- read.csv(input$csvFile$datapath, header=TRUE)
  output$RSQtext <- renderText(GetRSQ())

# Run the application 
shinyApp(ui = ui, server = server)

You probably want to do something much more complicated and eventReactive might not be the best choice for your case.

1 Like

Your data variable is dependant on user input so you would want that to be reactive.

data <- reactive({

You can look at the documentation for the req function for how this works. You could then treat data as you would any other reactive variable. data() will return a data frame for your function.

This worked great, but now is there a way to send actual objects (lists of plots) back to the rshiny environment after they are created with the EstimateModels.R code?

The basic steps of a process I'm (trying) to create are below. I was hoping it would be as simple as adding return("plots.results") to the send of the function in EstimateModels.R

  1. User uploads file to csvFile (DONE)
  2. User clicks button to initiate regression, plot generating all done within EstimateModels.R and saved to lists (DONE)
  3. User then selects from dropdown menu to select between 3 lists of plots to view in my output$plots

Alright -- so I did get the function to return a list of plots to a reactive value, but overall R shiny is almost like the same language but with a different dialect than R :wink: So, if anyone else has additional tipsfor passing objects around r shiny for analysis and returning normal r objects in the environment, I'd sure take them for future understanding. Thanks FJCC~!