radio-button - 基于 R Shiny 条件面板中的单选按钮的切换图

标签 radio-button conditional shiny ggvis

我正在尝试使用 ggvis 绘图和单选按钮创建一个 Shiny 的应用程序。我有 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))

最佳答案

正如@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))

关于radio-button - 基于 R Shiny 条件面板中的单选按钮的切换图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33267604/

相关文章:

javascript - 无法获取要发送的 radio 值

if-statement - 模板中的 SilverStripe 条件

python-3.x - 根据条件删除 DataFrame 中的元素

c# - 如何为 WPF RadioButton 创建 bool 值到字符串转换器?

javascript - 禁用单选按钮组

MVVM 组单选按钮

wix - 根据条件在 WiX 中设置属性

r - 如何在 Shiny eventReactive 处理程序中监听多个事件表达式

r - 通过 updatePickerInput shiny R 取消选择全部

html - R Shiny : use HTML within functions (like textInput, checkboxGroupInput)