【问题标题】:Switch plots based on radio buttons in R shiny conditionalPanel基于 R 闪亮条件面板中的单选按钮切换绘图
【发布时间】:2016-01-20 22:33:08
【问题描述】:

我正在尝试使用 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))

【问题讨论】:

  • 也许conditionalPanel 如图here?

标签: radio-button conditional shiny ggvis


【解决方案1】:
正如@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))

【讨论】:

    猜你喜欢
    • 2016-06-30
    • 2016-09-24
    • 1970-01-01
    • 2016-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-13
    • 2021-05-01
    相关资源
    最近更新 更多