miles2know
miles2know

Reputation: 747

Rendering Shiny user inputs based on conditional logic in server

I'm trying to set up a navbar panel page in shiny where the user controls I display change based on the initial choice made in a set of radio buttons. I'm rendering the radio buttons directly in the ui and then building the conditional controls inside an "observed" logical control structure in Server.r. The error pops up because my initial if statement evaluates to false so the controls needed to filter my data don't get rendered and thus dplyr::filter() doesn't get what is expected. The thing I'm trying to figure out is that if I run this the radio buttons render with all other well panels blank. Then if I click on a few radio buttons the other ui controls appear eventually and then by clicking back on the "Regions" button the charts are called. How can I get the first if statement to see that "Regions" is selected? Or is there a way to get the radio buttons to update once the observer begins? At any rate any insights would be much appreciated.

Error:

 Listening on http://...............
  Error in eval(substitute(expr), envir, enclos) : 
  incorrect length (0), expecting: 192 

Global.R:

##### Play data
    options(stringsAsFactors = FALSE)

    exp <- data.frame(scenario=rep(c(1,2,3,4),each=48),
                region=rep(c("Western","Central","Southern","Northern","Capital","Hudson"),
                times=4,each=8),
                period=rep(c(1,2,3,4),times=48),
                price=rep(c("Hi","Lo"),times=24,each=4),
                val=rnorm(192,5,1.5))

##### Functions

    all_values <- function(x) {
    if(is.null(x)) return(NULL)
    unique(x[,1])
    }

Server.R



library(shiny)
library(ggvis)
library(plyr)
library(dplyr)

shinyServer(function(input, output, session) {

observe({

if(input$comp=="Regions") { # For comparing regions #######################################

###### Define Controls ####### 

output$regionO <- renderUI({ checkboxGroupInput(inputId="regionI", label = h4("Regions"), 
        choices = list("Western", "Central", "Southern","Northern", "Capital", "Hudson"),
        selected = "Capital")
}) 

output$scenarioO <- renderUI({ selectInput(inputId="scenarioI", label = h4("Scenario"), 
        choices = list("L1" = 1, "L2" = 2, "L3" = 3,"L4" = 4), 
        selected = 1)
})

output$fuelO < renderUI({ selectInput(inputId="fuelI", label = h4("Fuel Price"), 
        choices = list("High Price" = "Hi", "Low Price" = "Lo"), 
        selected = "Lo")
})

###### Define Data ########### 


choose <- reactive({ 

        chooseD <- exp %>% filter(scenario == input$scenarioI, region %in% input$regionI, price == input$fuelI)
        names(chooseD)<-c("scenario","NYregion","period","price","val")
        return(chooseD)
})        


sPlot1 <- reactive({

        choose %>% ggvis(~factor(period),~val,stroke=~NYregion) %>% 
        layer_lines(strokeWidth:=2.5,strokeWidth.hover:=5) %>% 
        add_axis("x",title="Analysis Period") %>% add_tooltip(all_values,"hover") 
}) 

sPlot1 %>% bind_shiny("plot1") 
sPlot1 %>% bind_shiny("plot2")  


} 

}) 

# The code below is commented out so I can test the first condition of input$comp

#else if(data==2) { # For comparing scenarios ######

###### Define Controls ####### 

###### Define Data ########### 

#} else { # For comparing fuel prices ##############

###### Define Controls ####### 

###### Define Data ########### 

#}

}) # End Shiny Server

Ui.R:


library(shiny)
library(ggvis)
library(plyr)
library(dplyr)

shinyUI(navbarPage("TBR Economic Results Viewer", # theme="bootstrapCR.css",

  tabPanel("Summary Results",
   fluidRow(
      column(2,
       wellPanel(
        radioButtons(inputId="comp", label = h4("Comparison"),
        choices = list("Regions", "Scenarios", "Prices"), 
        selected ="Regions")
        ),

       wellPanel(
        uiOutput("regionO"),
        br(),
        uiOutput("scenarioO"),
        br(),
        uiOutput("fuelO")
        )
      ),

     column(5, 
      wellPanel(
        ggvisOutput("plot1"),
        textOutput("test")
        )
      ),
     column(5,
      wellPanel(
        ggvisOutput("plot2")
        ) 
      )
    )
 ),

 tabPanel("Detailed Results",
      mainPanel(
        plotOutput("plot2")
      )
    )
 )
)

Upvotes: 3

Views: 7062

Answers (1)

jdharrison
jdharrison

Reputation: 30425

You have a typo. Apart from this you can condition on for example input$fuelI:

library(shiny)
library(ggvis)
library(plyr)
library(dplyr)
options(stringsAsFactors = FALSE)

exp <- data.frame(scenario=rep(c(1,2,3,4),each=48),
                  region=rep(c("Western","Central","Southern","Northern","Capital","Hudson"),
                             times=4,each=8),
                  period=rep(c(1,2,3,4),times=48),
                  price=rep(c("Hi","Lo"),times=24,each=4),
                  val=rnorm(192,5,1.5))
all_values <- function(x) {
  if(is.null(x)) return(NULL)
  unique(x[,1])
}

runAPP:

runApp(list(ui = navbarPage("TBR Economic Results Viewer", # theme="bootstrapCR.css",
                            tabPanel("Summary Results",
                                     fluidRow(
                                       column(2, wellPanel(
                                         radioButtons(inputId="comp", label = h4("Comparison"),
                                                      choices = list("Regions", "Scenarios", "Prices"), 
                                                      selected ="Regions")
                                       ),
                                       wellPanel(uiOutput("regionO"), br(),
                                                 uiOutput("scenarioO"), br(),
                                                 uiOutput("fuelO")
                                       )
                                       ),

                                       column(5, wellPanel(ggvisOutput("plot1"),textOutput("test"))
                                       ),
                                       column(5, wellPanel(ggvisOutput("plot2")) 
                                       )
                                     )
                            ),
                            tabPanel("Detailed Results", mainPanel(plotOutput("plot2"))
                            )
)
, server = function(input, output, session) {
  observe({
    if(input$comp=="Regions") { 
      output$regionO <- renderUI({ checkboxGroupInput(inputId="regionI" , label = h4("Regions"), 
                                                      choices = list("Western", "Central", "Southern","Northern", "Capital", "Hudson"),
                                                      selected = "Capital")
      })
      output$scenarioO <- renderUI({ selectInput(inputId="scenarioI", label = h4("Scenario"), 
                                                 choices = list("L1" = 1, "L2" = 2, "L3" = 3,"L4" = 4), 
                                                 selected = 1)
      })
      output$fuelO <- renderUI({ selectInput(inputId="fuelI", label = h4("Fuel Price"), 
                                            choices = list("High Price" = "Hi", "Low Price" = "Lo"), 
                                            selected = "Lo")
      })
      if(!is.null(input$fuelI)){
        choose <- reactive({ 
          chooseD <- exp %>% filter(scenario == input$scenarioI, region %in% input$regionI, price == input$fuelI)
          names(chooseD)<-c("scenario","NYregion","period","price","val")
          return(chooseD)
        })        
        sPlot1 <- reactive({
          choose %>% ggvis(~factor(period),~val,stroke=~NYregion) %>% 
            layer_lines(strokeWidth:=2.5,strokeWidth.hover:=5) %>% 
            add_axis("x",title="Analysis Period") %>% add_tooltip(all_values,"hover") 
        }) 

        sPlot1 %>% bind_shiny("plot1")
        sPlot1 %>% bind_shiny("plot2")  
      }
    } 
  }) 
}
))

enter image description here

Upvotes: 2

Related Questions