Switch plots based on radio buttons in R shiny conditionalPanel

浪子不回头ぞ 提交于 2019-11-30 16:24:57
As @aosmith say, conditionalPanel works!
   library(shiny)
    library(ggvis)

df <- data.frame(Student = c("a","a","a","a","a","b","b","b","b","b","c","c","c","c"),
                 year = c(seq(2001,2005,1),seq(2003,2007,1),seq(2002,2005,1)),
                 col1 = runif(14,min = 50,max = 100),
                 col2 = runif(14,min = 120,max = 200),
                 col3 = runif(14,min = 60,max = 200),stringsAsFactors=F)

ui = (fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput("stu","Choose Student",
                  choice = unique(df$Student)),
      radioButtons("col","Switch Plot",
                   choices = c("A", "B","C"),
                   selected = "A")
    ),
    mainPanel(
    conditionalPanel(
      condition = "input.col == 'A'", ggvisOutput("plot1")),
    conditionalPanel(
      condition = "input.col == 'B'", ggvisOutput("plot2")),
    conditionalPanel(
      condition = "input.col == 'C'", ggvisOutput("plot3"))
    )
  )
))

server = function(input,output,session){   
  dataInput = reactive({
    gg = df[which(df$Student == input$stu),]
  })

  vis1 = reactive({
    data = dataInput()
    data %>%
      ggvis(x = ~year, y = ~col1) %>%
      layer_points()
  })

  vis2 = reactive({
    data = dataInput()
    data %>%
      ggvis(x = ~year, y = ~col2) %>%
      layer_lines()
  })

  vis3 = reactive({
    data = dataInput()
    data %>%
      ggvis(x = ~year, y = ~col3) %>%
      layer_bars()
  })

  vis1 %>% bind_shiny("plot1")
  vis2 %>% bind_shiny("plot2")
  vis3 %>% bind_shiny("plot3")

}

runApp(list(ui = ui, server = server))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!