本文介绍了根据R闪亮conditionalPanel中的单选按钮切换图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to create a shiny app with ggvis plots and radio buttons. I have three plots created by ggvis. Users can switch the different plot based on which radio option they select.

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

My problem is I don't know how to connect the plots with radio buttons. I've been struggling for hours. Thanks a lot for your help!I have some example code below.

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 有用!

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

这篇关于根据R闪亮conditionalPanel中的单选按钮切换图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 03:37