Shiny app doesn't wait for file upload

1,320 views
Skip to first unread message

Bobbie Shaban

unread,
Feb 6, 2017, 7:10:32 PM2/6/17
to Shiny - Web Framework for R
Hi,

I'm new to Shiny (last 4-5 days), I've written basic R scripts before but I'm basically a PERL programmer. I am having an issue with a shiny app that I am making. I have a file input where I upload a csv file and from there, there are a couple of selectInputs which are updated according to the users choice.

The problem is that when I run the script it does't seem to wait for the file to upload before it continues on to populating the select inputs with data from the csv. I've tried incorporating pauseable.R into my app but it's giving me similar errors.

My ui.R is

Enter code here...shinyUI(fluidPage(
  
  # Application title
  withMathJax(),
  uiOutput('mj1'),
  
  # Sidebar panel
  sidebarPanel(
    #Selector for file upload
    fileInput('file1', 'Choose file to upload',
              accept = c(
                'text/csv',
                'text/comma-separated-values',
                'text/tab-separated-values',
                'text/plain',
                '.csv',
                '.tsv'
              )),
    
    #drop down for each users choice
    selectInput("gene", "Genes", choices = NULL, selected = NULL),
    selectInput("allele", "Allele", choices = NULL, selected = NULL),
    selectInput("population", "Population", choices = NULL, selected = NULL),
      
    #complete formula calculation
    actionButton("goButton", "Calculate!")
  ),
  
    mainPanel(
      textOutput("c"),
      textOutput("s" ),
      textOutput("t"),
      textOutput("f_A"),
      textOutput("Result"),
      
      plotOutput("plot1")
    )
  
))


and my server.R is
library(shiny)
source("pausable.R")

# Define server logic required to give an output
shinyServer(function(input, output, session) {
  
  #set reactive values
  values <- reactiveValues(df_data = NULL)
  
  #pause file upload
  fileupload <- pauseableReactive(input$file1)
  
  pauseFileUpload <- function(paused=TRUE){
    pause(fileupload, paused)
  }
  
  #load and read csv
  filedata <- reactive({
    if(is.null(filedata)) return (NULL)
    isolate({
      pauseFileUpload(TRUE)
      infile <- input$file1
      read.csv(infile$datapath)
      invokeLater(function(){
        pauseFileUpload(FALSE)
        }, 500)
      })
    #infile <- read.csv("KIRIMP.csv")
     # outputOptions(output, 'fileUploaded', suspendWhenHidden=FALSE)
  })
   
    #render forumula withMathJax
  output$mj1 <- renderUI({
    withMathJax('$$\\frac{N_A}{N_B}=cr^2=\\frac{c(s + t - 1) ^2}{[(s + t -1) + \\frac{ 1 - t }{f_A}]  [(s + t = 1) + \\frac{1 -s}{1-f_A}]}$$')
  })

  #observe the genes list and update genes drop down 
  observe({
    values$df_data = filedata()
    genesList = values$df_data[1]
    updateSelectInput(session, "gene", "Genes ", unique(genesList))
  })

  #observe changes in the genes dropdown and update allele drop down
  observeEvent(input$gene, {
      cvsInput = filedata()
      cvsInput.gene.sub <- as.data.frame(subset(cvsInput, Gene == input$gene, drop=FALSE, byrow=TRUE))
      updateSelectInput(session, "allele", "Allele ", cvsInput.gene.sub[, 6])
  })
  
  #observe allele drop down and populate population drop down
  observeEvent(input$allele, {
      cvsInput = filedata()
      cvsInput.allele.sub <- as.data.frame(subset(cvsInput, Allele == input$allele & Gene == input$gene, drop=FALSE, byrow=TRUE))
      updateSelectInput(session, "population", "Population ", cvsInput.allele.sub[, 12])
  })
  
  #observe population drop down and show selected rows in text output
  observeEvent(input$goButton, {
    input$goButton
    isolate({
     cvsInput = filedata()
       #perform formula calc

        output$plot1 <- renderPlot({
          plot(plotValue)
        })# end render plot
        
    }) # end isolate

  })# end observeEvent(input$goButton)
  
})# end shiny server

The error I am getting is below, which makes me think that it's continuing to the observe function without waiting for the csv upload.

Any help would be greatly appreciated, thanks!

Warning: Error in read.table: 'file' must be a character string or connection
Stack trace (innermost first):
    77: read.table
    76: read.csv
    69: isolate
    68: <reactive:filedata> [/Users/bshaban/Documents/shiny/power/power/server.R#29]
    57: filedata
    56: observerFunc [/Users/bshaban/Documents/shiny/power/power/server.R#48]
     1: runApp
Warning: Error in read.table: 'file' must be a character string or connection

Bobbie.

Bobbie Shaban

unread,
Feb 6, 2017, 8:00:50 PM2/6/17
to Shiny - Web Framework for R
All good,

I've just discovered the wonderful world of eventReactive.

changing to this seems to work

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

--
You received this message because you are subscribed to a topic in the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/shiny-discuss/mbFSfTX3_Zc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to shiny-discuss+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/e00852b2-09d9-4278-89cd-561dcd2eca89%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

omayma Said

unread,
Feb 7, 2017, 2:45:03 AM2/7/17
to Shiny - Web Framework for R
You could try req(). Here req() will check for the required value` input$file1` before proceeding to read.csv.

#load and read csv
  filedata
<- reactive({

    req
(input$file1)
    read
.csv(input$file1$datapath)
 
})

Bobbie Shaban

unread,
Feb 7, 2017, 11:24:57 PM2/7/17
to Shiny - Web Framework for R
I didn't think about req, thanks!

eventReactive also worked for me. 

Thanks heaps!
Bobbie.
Reply all
Reply to author
Forward
0 new messages