switch statement in renderUI doesn't work as expected

82 views
Skip to first unread message

Laurent Spanu

unread,
Feb 25, 2015, 7:40:50 AM2/25/15
to shiny-...@googlegroups.com
Hi,

In order to use a more appropriate control structure, I changed a portion of my code by switching from an if statement (which works very well !) to a switch statement which doesn't run at all...
Can anyone explain me what's wrong in the code below ?
Because I think that switch and if statements work in similar way (excuse me if it's not the case) and thus, I don't understand why output$station gets the expected value or result (again,excuse me for the terminology)  with an if statement but not with a switch statement.

Many thanks for your helpful advice!

Laurent

ps: I hope that the code below will allow you to assess the situation (although you cannot access to the database)...

##SERVER.R

shinyServer(function(input,output){
 
  output$station<-renderUI({
   
    out<- reactive({
       switch(input$data,
           "Wind rose"={
             a<-c("Angleur","Herstal","Jemeppe","Liege-Aeroport","Liege-Cointe","Lodelinsart","Marcinelle","Sainte-Ode","Engis")
             i<-order(a)
             a<-a[i]
            
             selectInput("station", "Choose a station",selectize = T,
                         choices = c(Choose="",a),selected="",multiple=F)            
    
           },
           "Pollutants_meteo"={
             drv <-JDBC(driverClass="oracle.jdbc.OracleDriver", classPath="D:\\Mes Documents\\ISSEP_LSP\\DataR\\ojdbc6.jar")
             cxrdb <- dbConnect(drv, "jdbc:oracle:thin:@172.16.135.50:1521:N" , "READ", "RID$23")
             rtu<-dbGetQuery(cxrdb, paste("SELECT identifiant FROM station WHERE SUBSTR(identifiant,1,2) <> 'RM' AND flag_valid = 1 "))
             dbDisconnect(cxrdb)
            
             rtu<-rtu$IDENTIFIANT
             rtu<-paste("'",rtu,"'",sep="",collapse = ",")
             rtu<-noquote(rtu)
             #recupere la localisation ( champs libelle)
             drv <-JDBC(driverClass="oracle.jdbc.OracleDriver", classPath="D:\\Mes Documents\\ISSEP_LSP\\DataR\\ojdbc6.jar")
             cxrdb <- dbConnect(drv, "jdbc:oracle:thin:@172.16.135.50:1521:N" , "READ", "RID$23")
             sit<-dbGetQuery(cxrdb, paste("SELECT isit FROM station WHERE identifiant IN (",rtu,")"))
             dbDisconnect(cxrdb)
             sitName<-sit$ISIT
             idx<-order(sitName)
             sitName<-sitName[idx]
             a<-sitName
            
             selectInput("station", "Choose a station",
                         choices = c(Choose="",a),selectize= T,multiple=T)

            
            
           },
           "PM10 exceedance days"={
             drv <-JDBC(driverClass="oracle.jdbc.OracleDriver", classPath="D:\\Mes Documents\\ISSEP_LSP\\DataR\\ojdbc6.jar")
             cxrdb <- dbConnect(drv, "jdbc:oracle:thin:@172.16.135.50:1521:N" , "READ", "RID$23")
             #         rtu<-dbGetQuery(cxrdb, paste("Select station.ISIT from station, mesure, nom_mesure
             # WHERE station.flag_valid = 1 AND station.NSIT = mesure.NSIT AND mesure.cmhs = 1
             # AND mesure.NOPOL = nom_mesure.NOPOL AND nom_mesure.CODE_COMPLEMENTAIRE = '85101' AND SUBSTR(station.nom_court_sit,1,2) <> 'RM'
             # "))
             # autre methode       
             rtu<-dbGetQuery(cxrdb,paste("SELECT ISIT FROM STATION WHERE NSIT IN ( Select NSIT from MESURE where NOPOL IN ( SELECT NOPOL FROM NOM_MESURE WHERE CODE_COMPLEMENTAIRE = '85101' )
                                         AND CMHS = 1) AND FLAG_VALID = 1 AND SUBSTR(station.nom_court_sit,1,2) <> 'RM'"))
            
             dbDisconnect(cxrdb)
             sitName<-rtu$ISIT
             idx<-order(sitName)
             sitName<-sitName[idx]
             a<-sitName
            
             selectInput("station", "Choose a station",
                         choices = c(Choose="",a),selectize= T,multiple=T)

            
           },
          
           "Pollution rose"={
             a<-c("Angleur","Herstal","Jemeppe","Liege-Aeroport","Liege-Cointe","Lodelinsart","Marcinelle","Sainte-Ode","Engis")
             i<-order(a)
             a<-a[i]

             selectInput("station", "Choose a station",selectize = T,
                                   choices = c(Choose="",a),selected="")
                

           },
          
           "Exceedance"={
             drv <-JDBC(driverClass="oracle.jdbc.OracleDriver", classPath="D:\\Mes Documents\\ISSEP_LSP\\DataR\\ojdbc6.jar")
             cxrdb <- dbConnect(drv, "jdbc:oracle:thin:@172.16.135.50:1521:N" , "READ", "RID$23")
             rtu<-dbGetQuery(cxrdb, paste("SELECT identifiant FROM station WHERE SUBSTR(identifiant,1,2) <> 'RM' AND flag_valid = 1 "))
             dbDisconnect(cxrdb)
            
             rtu<-rtu$IDENTIFIANT
             rtu<-paste("'",rtu,"'",sep="",collapse = ",")
             rtu<-noquote(rtu)
             #recupere la localisation ( champs libelle)
             drv <-JDBC(driverClass="oracle.jdbc.OracleDriver", classPath="D:\\Mes Documents\\ISSEP_LSP\\DataR\\ojdbc6.jar")
             cxrdb <- dbConnect(drv, "jdbc:oracle:thin:@172.16.135.50:1521:N" , "READ", "RID$23")
             sit<-dbGetQuery(cxrdb, paste("SELECT isit FROM station WHERE identifiant IN (",rtu,")"))
             dbDisconnect(cxrdb)
             sitName<-sit$ISIT
             idx<-order(sitName)
             sitName<-sitName[idx]
             a<-sitName
            
             selectInput("station", "Choose a station",
                         choices = c(Choose="",a),selectize= T,multiple=F)


           }
             )
    })

out()

})

(...)

## UI
 
shinyUI(fluidPage(
 
  titlePanel(list("Data from ambient air quality monitoring network",img(src = "issep.png", height = 60, width = 60),img(src = "awac.png", height = 144, width = 144))),
  fluidRow(
    column(4,
      helpText("Data from XR server"),
selectInput(inputId = "data",label = "What do you want to display?",choices = c("Pollutants_meteo","Windrose","Pollution rose","PM10 exceedance days","Exceedance"),selected = "Pollutants_meteo"),
(...)

),
column(3,
uiOutput("station"),
uiOutput("choose")

(..)
))
Reply all
Reply to author
Forward
0 new messages