I'm trying to create a shiny app which chooses bar plots from the drop down menu and displays it.
I have two plots which are:
The first one is a total death per continent plot assigned to variable :continent_death_total_plot
The second one is a total infection per continent plot assigned to variable: continent_infection_total_plot
My code:
server <- function(input,output,session){
output$plot <- renderPlot({
if (input$choices == "Total Death Per continent"){
continental_death_total_plot
}else{
continental_infection_total_plot
}
})
}
ui <- fluidPage(
h1("R shiny Bar Plot"),
selectInput(inputId = "covid_plot",
label = "Choose category",
choices = c("Total Death Per Continent","Total Infections Per Continent")),
plotOutput("plot")
)
shinyApp(ui = ui, server = server)
When I run this code I get an error:
argument is of length zero
CodePudding user response:
One thing you have to do is change if (input$choices == "Total Death Per continent")
to if (input$covid_plot == "Total Death Per continent")
, as your selectInput
- widget is called covid_plot
.
In your example continental_death_total_plot
and continental_infection_total_plot
are not defined here, but I assume this is because you wanted to keep the example short.
CodePudding user response:
Here is an example using shinyjs()
and hide
and show
:
library(shiny)
library(shinyjs)
ui <- fluidPage(
useShinyjs(),
h1("R shiny Bar Plot"),
selectInput(inputId = "covid_plot",
label = "Choose category",
choices = c("Total Death Per Continent","Total Infections Per Continent")),
hidden(plotOutput("continental_death_total_plot")),
hidden(plotOutput("continental_infection_total_plot"))
)
server <- function(input,output,session){
output$continental_death_total_plot <- renderPlot({
plot(mtcars)
})
output$continental_infection_total_plot <- renderPlot({
plot(iris$Sepal.Length)
})
observeEvent(input$covid_plot,{
if (input$covid_plot == "Total Death Per Continent"){
hide('continental_infection_total_plot')
show('continental_death_total_plot')
}else{
hide('continental_death_total_plot')
show('continental_infection_total_plot')
}
})
}
shinyApp(ui = ui, server = server)