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.
For example, if user selects A, only plot1 is displayed on user interface. If user select B, the plot switch to plot2.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))
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With