Create Plots Based on Radio Button Selection R Shiny

create plots based on radio button selection R Shiny

You can use switch to determine the behaviour based on the selection:

library(shiny)
myData <- runif(100)
plotType <- function(x, type) {
switch(type,
A = hist(x),
B = barplot(x),
C = pie(x))
}
runApp(list(
ui = bootstrapPage(
radioButtons("pType", "Choose plot type:",
list("A", "B", "C")),
plotOutput('plot')
),
server = function(input, output) {
output$plot <- renderPlot({
plotType(myData, input$pType)
})
}
))

How to display outputs based on radio button in R Shiny?

Perhaps you are looking for this

ui <- fluidPage(
sidebarLayout(
sidebarPanel(
fileInput("file1", "Choose CSV file to upload", accept = ".csv"),
radioButtons(
"dman_preview", "Display:",
c("preview", "str", "summary"),
selected = "preview",
inline = TRUE
),
),

mainPanel(
uiOutput("myoutput")
)
)
)

server <- function(input, output){
df1 <- reactive({
# req(input$file1)
# df <- read.csv(input$file1$datapath,
# header = input$header,
# sep = input$sep,
# quote = input$quote)
# df
cars ## temporary dataframe for testing purpose
})

output$contents <- renderTable({

# if(input$disp == "head") {
# return(head(df1()))
# }
# else {
# return(df1())
# }

df1()
})

output$summary <- renderPrint({
summary(df1())
})

output$str <- renderPrint({ str(df1()) })

output$myoutput <- renderUI({
switch(input$dman_preview,
"preview" = tableOutput("contents"),
"str" = verbatimTextOutput("summary"),
"summary" = verbatimTextOutput("str")
)
})
}

shinyApp(ui = ui, server = server)

How to reactively plot using radioButton and actionButton values in Shiny R

As your example is not reproducible, here's an example with mtcars and iris:

library(shiny)

ui <- fluidPage(
radioButtons(inputId = "test", "radio buttons", choices = c("iris", "mtcars")),
actionButton("run", "Run"),
plotOutput("plot")
)

server <- function(input, output, session) {

observe({
observeEvent(input$run, {
if (input$test == "iris") {
output$plot <- renderPlot({
plot(iris)
})
}

else if (input$test == "mtcars") {
output$plot <- renderPlot({
plot(mtcars)
})
}
})
})

}

shinyApp(ui, server)

Shiny update choices of selectizeInput based on radio buttons

You were almost there - added an else if statement:

library(shiny)
library(shinyWidgets)

names_vector1 = paste0("common", 1:10)
names_vector2 = paste0("scientific", 1:10)

ui = fluidPage(fluidRow(
selectizeInput(
inputId = "species_selector",
label = "Choose a species:",
selected = "common1",
choices = c("Choose" = "", names_vector1),
options = list(maxOptions = 5,
maxItems = 4)
),
awesomeRadio(
inputId = "species_selector_type",
label = NULL,
choices = c("Common name", "Scientific name"),
selected = "Common name",
inline = TRUE
)
))

server = server = server = function(input, output, session) {
observeEvent(input$species_selector_type, {
if (input$species_selector_type == "Common name") {
updateSelectizeInput(session,
inputId = "species_selector",
choices = c("Choose" = "", names_vector1))
} else if (input$species_selector_type == "Scientific name") {
updateSelectizeInput(session,
inputId = "species_selector",
choices = c("Choose" = "", names_vector2))
}
})
}

shinyApp(ui, server)

Switch plots based on radio buttons in R shiny 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))

Show slider inputs based on radio buttons in Shiny R

You can use conditionalPanel to show/hide a sliderInput when 'b' is selected from your radiobutton input.

To prevent your warning, you can require req(input$rd) before using it in your if statement.

Let me know if this is the behavior you had in mind.

library(shiny)
library(ggplot2)
options(warn=-1)

#ui.r
ui <- pageWithSidebar(
headerPanel("many plots app"),

sidebarPanel(
sliderInput("width", "Plot Width (%)", min = 0, max = 100, value = 100),
sliderInput("height", "Plot Height (px)", min = 0, max = 800, value = 800),
uiOutput("filter_plot"),
conditionalPanel(
"input.rd == 'b'",
sliderInput("sl_wt", "Divide Weight", min = 1, max = 10, value = 5)
)
),

mainPanel(
uiOutput("plot")
)
)
#server.r
server <- function(input, output, session) {

options(warn = -1) #This doesn't stop console warning to stop

output$filter_plot<-renderUI({
radioButtons("rd","Select Option",choices = c("a",
"b",
selected = "a"))
})

output$plot1<- renderPlot({
ggplot2::ggplot(mtcars, aes(mpg, cyl))+ geom_point()
})

output$plot2<- renderPlot({
ggplot2::ggplot(mtcars, aes(wt/input$sl_wt, qsec))+ geom_line()
})

output$plot <- renderUI({
req(input$rd)
if(input$rd=="a"){
plotOutput("plot1", width = paste0(input$width, "%"), height = input$height)
}
else if(input$rd=="b"){
plotOutput("plot2", width = paste0(input$width, "%"), height = input$height)
}
})
}
shinyApp(ui = ui, server = server)

different column from radio button in shiny

You can use input$typeInput to get the selected value from radiobutton.

output$PLOT <- renderPlot({
filtered <-
Data %>%
filter(week>= input$Weekinput[1],
week<= input$Weekinput[2]
)
ggplot(filtered, aes(week, .data[[input$typeInput]])) +
geom_line()
})


Related Topics



Leave a reply



Submit