Home > Mobile >  Include multiple different graphics in shiny app
Include multiple different graphics in shiny app

Time:06-26

I am attempting to build my first shiny app. I need to include multiple graphics (about 50) and I am having problems selecting them based on their label from the dropdown control. I am able to show the first one but I don't know how to display the other ones on the main panel. I currently have 3 on the dropdown control but only the first one works. How do I make lambda2, lambda3 and so on show on the main panel? I also would like to dynamically plot the number of years selected on the slider. Here is the code:

    library(shiny)
library(tidyverse)
library(shinythemes)
library(plotly)
library(scales)
library(shinyWidgets)
library(shinydashboard)

# Define input choices
type <- c("lambda","lambda2","lambda3")

table <- structure(list(year = 1991:2010, 
lambda = c(0.68854, 0.75545, 
1.63359, 1.22282, 1.70744, 1.09692, 0.51159, 1.3904, 1.09132, 
 0.59846, 0.43055, 0.80135, 0.69027, 0.65646, 0.95485, 1.04818, 
0.67859, 1.00461, 1.16665, 1.28203)), row.names = c(NA, -20L), class = "data.frame")

# Define UI 
ui <- fluidPage(
  navbarPage("Fish",
            windowTitle = "Fish Graphs",
            sidebarPanel(
            h3("Select Graphics to Visualize"), 
            selectInput(inputId = "graphtype",
                          label = "Graphic",
                          choices = type,
                          selected = "lambda"),
            sliderInput(inputId = "Yearslider",
                      label="Years to plot",
                      sep="",
                      min=1991,
                      max=2011,
                      value=c(1991,2011))),
                      mainPanel(plotOutput("plot"))))
                                            
####################################
server<- function (input, output, session) {
  session$onSessionEnded(function() {
    stopApp()
  })  
output$plot <- renderPlot({
   xlabels <- 1991:2011
       ggplot(table,aes(year,lamda))   geom_line(size=1.5,colour="blue")   geom_point(colour="orange",size=4)   
       scale_x_continuous("",breaks = xlabels)    
       theme(axis.text.x = element_text(angle = 45, vjust = 0.5))   
       labs(x="",y=expression("Lambda ("~lambda *")"),title="Population growth rate - fraction per year- \nof Delta Smelt")
      
if (input$lambda2 == TRUE) { 
  xlabels <- 1991:2011
       ggplot(table,aes(year,lamda))   geom_line(size=1.5,colour="green")   geom_point(colour="orange",size=4)   
         scale_x_continuous("",breaks = xlabels)  
         theme(axis.text.x = element_text(angle = 45, vjust = 0.5))   
         labs(x="",y=expression("Lambda ("~lambda *")"),title="Population growth rate - fraction per year- \nof Delta Smelt")
       
}

 if (input$lambda3 == TRUE) { 
     xlabels <- 1991:2011
    ggplot(table,aes(year,lamda))   geom_line(size=1.5,colour="red")   geom_point(colour="orange",size=4)   
    scale_x_continuous("",breaks = xlabels)    
    theme(axis.text.x = element_text(angle = 45, vjust = 0.5))   
    labs(x="",y=expression("Lambda ("~lambda *")"),title="Population growth rate - fraction per year- \nof Delta Smelt")
  }                     
})
 }
 shinyApp(ui = ui, server = server)

enter image description here

CodePudding user response:

The main issue with your code is that the element of the input list containing the lambda choice is called graphtype. Using input$lambda2 returns NULL. Do e.g. input$graphtype == "lambda2" instead. Also, if you want to switch between different choices you have to use an if-else with a branch for "each" choice or perhaps use switch as I do below. To make your plot react to the year slider I use an reactive which filters the data for years in the selected range. Also, instead of duplicating the ggplot code I would suggest to move it in a separate function outside of the server which also makes it easier to debug the code.

plot_fun <- function(.data, point.color = "black") {
  breaks <- unique(.data$year)
  ggplot(.data, aes(year, lambda))  
    geom_line(size = 1.5, colour = "blue")  
    geom_point(colour = point.color, size = 4)  
    scale_x_continuous("", breaks = breaks)  
    theme(axis.text.x = element_text(angle = 45, vjust = 0.5))  
    labs(x = "", y = expression("Lambda (" ~ lambda * ")"), title = "Population growth rate - fraction per year- \nof Delta Smelt")
}

server <- function(input, output, session) {
  session$onSessionEnded(function() {
    stopApp()
  })
  plot_data <- reactive({
    table[table$year >= input$Yearslider[1] & table$year <= input$Yearslider[2], ]
  })
  
  output$plot <- renderPlot({
    switch(input$graphtype,
           "lambda" = plot_fun(plot_data(), point.color = "orange"),
           "lambda2" = plot_fun(plot_data(), point.color = "purple"),
           "lambda3" = plot_fun(plot_data(), point.color = "green")
           )
  })
}
shinyApp(ui = ui, server = server)

enter image description here

  • Related