14

I'm experimenting with Shiny and I love it. I built a little application where students upload a csv file and then choose a dependent variables and independent variables and then R computes a linear regression. It works fine. I have it uploaded at:

http://carlosq.shinyapps.io/Regresion

[You can use this file to test it if you want. "beer" is the dependent variable and the rest of the variables except "id" are the independent]

Here's server.R:

# server.R
library(shiny)

shinyServer(function(input, output) {

  filedata <- reactive({
    infile <- input$file1
    if (is.null(infile)){
      return(NULL)      
    }
    read.csv(infile$datapath)
  })

  output$dependent <- renderUI({
    df <- filedata()
    if (is.null(df)) return(NULL)
    items=names(df)
    names(items)=items
    selectInput("dependent","Select ONE variable as dependent variable from:",items)
  })


  output$independents <- renderUI({
    df <- filedata()
    if (is.null(df)) return(NULL)
    items=names(df)
    names(items)=items
    selectInput("independents","Select ONE or MANY independent variables from:",items,multiple=TRUE)
  })


  output$contents <- renderPrint({
    input$action
    isolate({   
      df <- filedata()
      if (is.null(df)) return(NULL)
      fmla <- as.formula(paste(input$dependent," ~ ",paste(input$independents,collapse="+")))
      summary(lm(fmla,data=df))
    })   
  })

}) 

And here is the ui.R:

# ui.R
library(shiny)

shinyUI(fluidPage(
  titlePanel("Multiple Linear Regression"),
  sidebarLayout(
    sidebarPanel(
      fileInput('file1', 'Choose CSV File',
                accept=c('text/csv', 
                         'text/comma-separated-values,text/plain', 
                         '.csv')),

      tags$hr(),
      uiOutput("dependent"),
      uiOutput("independents"),
      tags$hr(),
      actionButton("action", "Press after reading file and selecting variables")

    ),
    mainPanel(
      verbatimTextOutput('contents')
    )
  )
))

My question is: I want to make the appearance of the button "Press After Reading File and selecting variables " conditional on a succesful uploading.

I have tried to use the suggestion contained here:

Make conditionalPanel depend on files uploaded with fileInput

But I just can't make it work.

I appreaciate any help.

Florian
  • 24,425
  • 4
  • 49
  • 80
user23438
  • 445
  • 3
  • 11

2 Answers2

15

Here's the working ShinyApp and the final version of both ui.R and server.R based on all the suggestions provided by Marat.

First the ui.R

# ui.R

library(shiny)

shinyUI(fluidPage(
  titlePanel("Multiple Linear Regression with R/Shiny"),
  sidebarLayout(
    sidebarPanel(
      p("Please upload a CSV formatted file with your data."),
      fileInput('file1', label='Click button below to select the file in your computer.',
                accept=c('text/csv', 
                         'text/comma-separated-values,text/plain', 
                         '.csv')),

      tags$hr(),
      uiOutput("dependent"),
      uiOutput("independents"),
      tags$hr(),
      uiOutput('ui.action') # instead of conditionalPanel
    ),
    mainPanel(
      p("Here's the output from your regression:"),
      verbatimTextOutput('contents')
    )
  )
))

and server.R

# server.R

library(shiny)

shinyServer(function(input, output) {

  filedata <- reactive({
    infile <- input$file1
    if (is.null(infile)){
      return(NULL)      
    }
    read.csv(infile$datapath)
  })

  output$ui.action <- renderUI({
    if (is.null(filedata())) return()
    actionButton("action", "Run regression")
  })

  output$dependent <- renderUI({
    df <- filedata()
    if (is.null(df)) return(NULL)
    items=names(df)
    names(items)=items
    selectInput("dependent","Now select ONE variable as dependent variable from:",items)
  })


  output$independents <- renderUI({
    df <- filedata()
    if (is.null(df)) return(NULL)
    items=names(df)
    names(items)=items
    selectInput("independents","Also select ONE or MANY independent variables in the box below. You can change your selection several times:",items,multiple=TRUE)
  })


  output$contents <- renderPrint({
    if (is.null(input$action)) return()
    if (input$action==0) return()
    isolate({
      df <- filedata()
      if (is.null(df)) return(NULL)
      fmla <- as.formula(paste(input$dependent," ~ ",paste(input$independents,collapse="+")))
      summary(lm(fmla,data=df))
    })
  })


}) 

Once again thanks for your help Marat.

user23438
  • 445
  • 3
  • 11
14

This code worked for me

ui.R

 # ui.R
library(shiny)

shinyUI(fluidPage(
  titlePanel("Multiple Linear Regression"),
  sidebarLayout(
    sidebarPanel(
      fileInput('file1', 'Choose CSV File',
                accept=c('text/csv', 
                         'text/comma-separated-values,text/plain', 
                         '.csv')),

      tags$hr(),
      uiOutput("dependent"),
      uiOutput("independents"),
      tags$hr(),
      uiOutput('ui.action') # instead of conditionalPanel
    ),
    mainPanel(
      verbatimTextOutput('contents')
    )
  )
))

server.R

# server.R
library(shiny)

shinyServer(function(input, output) {

  filedata <- reactive({
    infile <- input$file1
    if (is.null(infile)){
      return(NULL)      
    }
    read.csv(infile$datapath)
  })

  output$dependent <- renderUI({
    df <- filedata()
    if (is.null(df)) return(NULL)
    items=names(df)
    names(items)=items
    selectInput("dependent","Select ONE variable as dependent variable from:",items)
  })


  output$independents <- renderUI({
    df <- filedata()
    if (is.null(df)) return(NULL)
    items=names(df)
    names(items)=items
    selectInput("independents","Select ONE or MANY independent variables from:",items,multiple=TRUE)
  })


  output$contents <- renderPrint({
    input$action
    isolate({   
      df <- filedata()
      if (is.null(df)) return(NULL)
      fmla <- as.formula(paste(input$dependent," ~ ",paste(input$independents,collapse="+")))
      summary(lm(fmla,data=df))
    })   
  })


  output$ui.action <- renderUI({
    if (is.null(input$file1)) return()
    actionButton("action", "Press after reading file and selecting variables")
  })

}) 
Marat Talipov
  • 13,064
  • 5
  • 34
  • 53
  • Thank you Marat. I tried your solution. It makes the button desappear...and that's good. But it doesn't appear after uploading the file. I asumme your server.R file includes a line that checks whether the file uploaded successfully. – user23438 Jan 04 '15 at 19:40
  • @user23438, I was not able to get the solution using the `conditionPanel`, because I did not know how to properly set up `condition`. I edited the answer, which is now based on the uiOutput. – Marat Talipov Jan 04 '15 at 20:17
  • Thanks again Marat for your time. Your new solution gets me closer to the solution. Now the button appears at the right moment, but it creates a problem with my output$contents. Before I had the code in this section wrapped in "isolate({ })" that was activated by input$action. Now input$action is gone because the button is gone. I could get rid of isolate but then it prints garbage until the right variables are selected. – user23438 Jan 04 '15 at 21:38
  • If you want to prevent premature output in output$contents, you can insert couple of checks there: replace `input$action` by `if (is.null(input$action)) return()` and `if (input$action==0) return()` – Marat Talipov Jan 04 '15 at 21:59