根据R shiny conditionalPanel中的单选按钮切换绘图

时间:2015-10-21 19:26:34

标签: radio-button conditional shiny ggvis

我正在尝试使用ggvis图和单选按钮创建一个闪亮的应用程序。我有三个由ggvis创建的图。用户可以根据他们选择的无线电选项切换不同的图。

例如,如果用户选择A,则在用户界面上仅显示plot1。如果用户选择B,则绘图切换到plot2。

我的问题是我不知道如何使用单选按钮连接图表。我已经挣扎了几个小时。非常感谢你的帮助! 我在下面有一些示例代码。

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(ggvisOutput("plot1")
    ))
))

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))

1 个答案:

答案 0 :(得分:4)

正如@aosmith所说,conditionalPanel有效!
   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))