gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 23:23:53 28 4
gpt4 key购买 nike

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

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com