基于 R 闪亮条件面板中的单选按钮切换绘图

2023-12-04

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

For example, if user selects A, only plot1 is displayed on user interface. If user select B, the plot switch to 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)
code:
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 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))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

基于 R 闪亮条件面板中的单选按钮切换绘图 的相关文章

随机推荐