I have looked at the majority of the stackoverflow posts on using conditionalPanel in shiny and still have not been able to find my bug.
I seem to be able to pass my parameters for "yr1" and "scen1" from server.R to ui.R, but the conditionalPanel does not appear to be working.
In my shiny app I have two selections to be made. Depending on the first selection, a different second set of selections should be displayed. Below is the code I am currently testing:
library(shiny)
library(httpuv)
shinyUI(pageWithSidebar(
headerPanel("Global Temperature"),
sidebarPanel(
checkboxGroupInput("scen1", label = h3("Select 1 Map #1 Scenario"),
choices = list("pre-2000" = "past", "post-2000: 850 ppm by 2100 (a2)"="a2","post-2000: 550 ppm by 2100 (b1)"="b1"), selected = "past"),
conditionalPanel(condition = "input.scen1 == 'past'",
selectInput("yearspred1", "Select Map #1 Years Hindcasted",choices = c("1920-1939", "1940-1959", "1960-1979", "1980-1999"), selected="1920-1939", multiple=FALSE)),
conditionalPanel(condition = "input.scen1 == 'a2'",
selectInput("yearspred1", "Select Map #1 Years Predicted",choices = c("2020-2039", "2040-2059","2060-2079", "2080-2099")), selected="2020-2039", multiple=FALSE),
conditionalPanel(condition = "input.scen1 == 'b1'",
selectInput("yearspred1", "Select Map #1 Years Predicted",choices = c("2020-2039", "2040-2059", "2060-2079", "2080-2099"), selected="2020-2039", multiple=FALSE)),
),
mainPanel(
h3(textOutput("add1")),
imageOutput("plot1")
)
)
)
(I do the above for two selections but just show one here)
Server.R (you would need to fill in the path for the kml options to run this):
library(shiny)
library(httpuv)
library(rWBclimate)
library(ggplot2)
# get temperature data for ensembles
st=1900
en=2100
world <- c("USA")
options(kmlpath\="/Users/n....")
world_map_df <- create_map_df(world)
world_dat <- get_ensemble_temp(world, "annualavg", start=st, end=en)
world_dat$data <- as.numeric(as.character(world_dat$data))
world_dat<-subset(world_dat,world_dat$percentile==50) #subset to median percentile
world_dat$years=paste(world_dat$fromYear,world_dat$toYear, sep="-")
world_dat<-subset(world_dat, select=-c(percentile, fromYear, toYear))
shinyServer(function(input, output){
scenario1<-reactive({input$scen1})
yr1<-reactive({
switch(input$yearspred1,
"1920-1939" = "1920-1939",
"1940-1959" = "1940-1959",
"1960-1979" = "1960-1979",
"1980-1999" = "1980-1999",
"2020-2039" = "2020-2039",
"2040-2059" = "2040-2059",
"2060-2079" = "2060-2079",
"2080-2099" = "2080-2099")
})
dfyr1<-reactive({subset(world_dat, world_dat$scenario==scenario1())})
df1<-reactive({subset(dfyr1(), dfyr1()$years==yr1())})
output$add1 <- renderText({paste("Temperature prediction for years ", yr1(), " and ", scenario1(), " scenario") })
output$plot1<- renderPlot({
climate_map(world_map_df,df1(),return_map = T) + scale_fill_gradient2(limits=c(-20, 34), low="blue", mid="white", high = "red", space="rgb", guide="colourbar")
})
})
Parameters are being passed, but the dropdown menu for "yr1" does not comply with the conditionalPanel conditions. I suspect it is in this part of the code, but I am not sure:
yr1<-reactive({
switch(input$yearspred1,
"1920-1939" = "1920-1939",
"1940-1959" = "1940-1959",
"1960-1979" = "1960-1979",
"1980-1999" = "1980-1999",
"2020-2039" = "2020-2039",
"2040-2059" = "2040-2059",
"2060-2079" = "2060-2079",
"2080-2099" = "2080-2099")
})
I tried replacing it with the following line:
yr1<-reactive({input$yearspred1})
But this did not fix the problem either.
Thanks for any help you can provide!