0

I have a selectInput panel in shiny. I only dealt with the fixed values of choices in selectInput till now.

Now I am at a point where I want to vary these choices based on some other conditions in shiny Ui.

Example:

Ui.R

shinyUI(fluidPage(
fluidRow(column(3,
wellPanel(
                  h4("Data Upload"),
                  fileInput('file1', h5('Choose Your Model Data'), accept=c('text/csv','text/comma-separated-values,text/plain','.OUT')),
                  fileInput('file2', h5('Choose Your Observation Data'), accept=c('text/csv','text/comma-separated-values,text/plain','.xlsx'))    
                ),  
wellPanel(uiOutput("check"))))

Server.R

shinyServer(function(input, output) {
output$check <- renderUI({
   selectInput("check", label = h4("Dataset Selection"), choices = c("Model" = 1, "Observation" = 2, "Both" = 3), selected = 1, multiple = F )
  })
a <- reactive({
   fileinput1 <- input$file1
   if (is.null(fileinput1))
   return(NULL)
   read.table(fileinput1$datapath, header = TRUE, col.names = c("Ei","Mi","hours","Nphy","Cphy","CHLphy","Nhet","Chet","Ndet","Cdet","DON","DOC","DIN","DIC","AT","dCCHO","TEPC","Ncocco","Ccocco","CHLcocco","PICcocco","par","Temp","Sal","co2atm","u10","dicfl","co2ppm","co2mol","pH"))
 })

 #Upload Observation Data 

 b <- reactive({
   fileinput2 <- input$file2
   if (is.null(fileinput2))
   return(NULL)
   #xlfile <- list.files(pattern = ".xlsx")
   xlfile <- fileinput2[1]
   wb <- loadWorkbook(xl_file)
   sheet_ct <- wb$getNumberOfSheets()
   b <- rbindlist(pblapply(1:sheet_ct, function(x) {
     res <- read.xlsx(xl_file, x)
   }), fill=TRUE)
   b <- b [-c(1),]
   print (b)
   })

Now I want to make the choices in selectInput dynamic based on the file input.

cppiscute
  • 707
  • 2
  • 10
  • 33
  • See the `?updateSelectInput` and its family functions to handle these problems. Also take a look at `?conditionalPanel`. – nicola Oct 27 '14 at 10:06
  • I have tried updateselectinput and failed to update the choices list in realtime....Its like if the user uploads file1 I need to update the choice list based on that as just "Model"...If he then uploads the second file then provide all three options..if just 2nd file is uploaded then choices = just "Observation". Please let me know how this can be achieved. – cppiscute Oct 27 '14 at 14:27

1 Answers1

1

I have tried to correct some of the issues in the server.R file. Note that I followed the following algorithm

  1. if file1 is uploaded first then choice is "Model"
  2. if file2 is uploaded subsequently then choices should be "Model", "Observation", "Both"
  3. if file2 is uploaded first the choice is "Observation"
  4. if file1 is uploaded subsequently then choices should be "Model", "Observation", "Both"

library(shiny) library(xlsx)

shinyServer(function(input, output) {

  a <- reactive({
    fileinput1 <- input$file1
    if (is.null(fileinput1))
      return(NULL)
    #read.table(fileinput1$datapath, header = TRUE, col.names = c("Ei","Mi","hours","Nphy","Cphy","CHLphy","Nhet","Chet","Ndet","Cdet","DON","DOC","DIN","DIC","AT","dCCHO","TEPC","Ncocco","Ccocco","CHLcocco","PICcocco","par","Temp","Sal","co2atm","u10","dicfl","co2ppm","co2mol","pH"))

    #Please change this part back to your code as I dont have your file based on the column names above
    read.table(fileinput1$datapath, header= TRUE)
  })

  #Upload Observation Data 

  b <- reactive({
    fileinput2 <- input$file2
    if (is.null(fileinput2))
      return(NULL)
    #xlfile <- list.files(pattern = ".xlsx")
    xlfile <- fileinput2$datapath
    wb <- loadWorkbook(xlfile)
    sheet_ct <- wb$getNumberOfSheets()
    b <- rbind(list(lapply(1:sheet_ct, function(x) {
      res <- read.xlsx(xlfile, x)
    })))
    b <- b [-c(1),]
    print(b)
  })

  getModel <- reactive({
    if(!is.null(a()) & !is.null(b()))
    {
      c("Model", "Observation", "Both")
    }
    else if(!is.null(a()))
    {
      "Model"
    }
    else if(!is.null(b()))
    {
      "Observation"
    }


  })
  output$check <- renderUI({
    selectInput("check", label = h4("Dataset Selection"), choices = as.list(getModel()), multiple = F )
  })


})
  • Thanks for the answer. Just want to know is there a way I can link the progress bar of the fileInput in shiny (ui.R) with the reactive component b calculation. Because as you know it takes a little time to merge the sheets in xlsx. – cppiscute Oct 28 '14 at 15:46
  • I think you can change this `b <- rbind(list(lapply(1:sheet_ct, function(x) { res <- read.xlsx(xlfile, x) })))` to `b <- rbind(list(lapply(1:sheet_ct, function(x) { res <- read.xlsx(xlfile, x) withProgress(message = 'Calculation in progress', detail = 'This may take a while...', value = 0, { for (i in 1:sheet_ct) { incProgress(1/sheet_ct) Sys.sleep(0.25) } }) })))` – Neville Andrade Oct 28 '14 at 17:15
  • Thanks. ?withProgress --> there is no such function as such. And with above code, it throws error as :Unexpected Symbol. – cppiscute Oct 28 '14 at 17:28
  • Hi, This is available with shiny_0.10.2.1. Please see [link](http://shiny.rstudio.com/articles/progress.html) – Neville Andrade Oct 29 '14 at 11:36
  • Sorry to ask an additional question here....please check if you can, I have added to the existing question. – cppiscute Oct 31 '14 at 19:42
  • Can you put this up as another question please? – Neville Andrade Nov 03 '14 at 08:47