using conditionalPanel in Shiny ui.R and server.R: different selectInput based on a condition -



using conditionalPanel in Shiny ui.R and server.R: different selectInput based on a condition -

i have looked @ bulk of stackoverflow posts on using conditionalpanel in shiny , still have not been able find bug.

i seem able pass parameters "yr1" , "scen1" server.r ui.r, conditionalpanel not appear working.

in shiny app have 2 selections made. depending on first selection, different sec set of selections should displayed. below code 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 2100 (a2)"="a2","post-2000: 550 ppm 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 above 2 selections show 1 here)

server.r (you need fill in path kml options run this):

library(shiny) library(httpuv) library(rwbclimate) library(ggplot2) # temperature info 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 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 years ", yr1(), " , ", 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 beingness passed, dropdown menu "yr1" not comply conditionalpanel conditions. suspect in part of code, 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 next line:

yr1<-reactive({input$yearspred1})

but did not prepare problem either.

thanks help can provide!

i can see temptation utilize same id each selectinput within conditionpanel shouldn't. best have defined input id's. additionally, switch statement unnecessary returns exact same value set in. after changing selectinput id's makes much more sense have switch statement. have many unnecessary reactive statements. unless plan on using statements in other functions throughout server.r file, there not point have them there , should combined wherever used (i.e. renderplot statement). why yr made reactive because used in rendertext , renderplot. next code works appropriately.

ui.r

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 2100 (a2)"="a2","post-2000: 550 ppm 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("yearspred2", "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("yearspred3", "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") ) ) )

server.r

library(shiny) library(httpuv) library(rwbclimate) library(ggplot2) # temperature info 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 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){ yr <- reactive({switch( input$scen1, "past" = input$yearspred1, "a2" = input$yearspred2, "b1" = input$yearspred3 )}) output$add1 <- rendertext({paste("temperature prediction years ", yr(), " , ", input$scen1, " scenario") }) output$plot1<- renderplot({ dfyr1<-subset(world_dat, world_dat$scenario==input$scen1) df1 <- subset(dfyr1, dfyr1$years==yr()) climate_map(map_df=world_map_df,data_df=df1,return_map = t) + scale_fill_gradient2(limits=c(-20, 34), low="blue", mid="white", high = "red", space="rgb", guide="colourbar") }) })

r shiny

Comments

Popular posts from this blog

php - Edges appear in image after resizing -

ios8 - iOS custom keyboard - preserve state between appearances -

Delphi change the assembly code of a running process -