Multiple dynamic ggplots not working in shinydashboard

ggplot2
shiny
shinydashboard

#1

I am in the process of making my first shiny app but I am having great difficulty getting two ggplots to be created, at the same time, based off user input for parameters in one column and date range. The app was working when I only made one plot but now it's not working when trying to create two plots. I am using the same reactive expression as the data for the two plots... not sure if this is the reason or not. I get the following error:
Error: Aesthetics must be either length 1 or the same as the data (18): alpha

Sample Data:

roadsalt_data<-structure(list(orgid = c("31DELRBC_WQX", "BTMUA", "BTMUA", "USGS-NJ", 
"NJPC", "USGS-NJ", "BTMUA", "USGS-NJ", "11NPSWRD", "USGS-NJ"), 
    locid = c("31DELRBC_WQX-Newton Creek near mouth", "BTMUA-INTAKE", 
    "BTMUA-ND", "USGS-01466500", "NJPC-NPUHALUW", "USGS-01405340", 
    "BTMUA-NG", "USGS-01378560", "11NPSWRD-MORR_NPS_JB2", "USGS-01445160"
    ), stdate = structure(c(14054, 16526, 16019, 13389, 10751, 
    14467, 14945, 13103, 14039, 16133), class = "Date"), sttime = structure(c(38040, 
    25920, 34860, 10200, 0, 39600, 37020, 37200, 40800, 37800
    ), class = c("hms", "difftime"), units = "secs"), charnam = c("Total dissolved solids", 
    "Total dissolved solids", "Specific conductance", "Specific conductance", 
    "Specific conductance", "Total dissolved solids", "Total dissolved solids", 
    "Specific conductance", "Specific conductance", "Specific conductance"
    ), val = c(183, 170, 226, 35, 94.3, 131, 91, 735, 220, 449
    ), valunit = c("mg/L", "mg/L", "uS/cm @25C", "uS/cm @25C", 
    "uS/cm @25C", "mg/L", "mg/L", "uS/cm @25C", "uS/cm @25C", 
    "uS/cm @25C"), swqs = c("FW2-NT", "FW2-NT", "FW2-TM", "FW1", 
    "PL", "FW2-NT", "FW2-TM", "FW2-NT", "FW2-TP", "FW2-TP"), 
    WMA = c(18L, 13L, 13L, 19L, 14L, 9L, 13L, 5L, 6L, 1L), year = c(2008L, 
    2015L, 2013L, 2006L, 1999L, 2009L, 2010L, 2005L, 2008L, 2014L
    ), locid2 = c("Newton Creek near mouth", "INTAKE", "ND", 
    "01466500", "NPUHALUW", "01405340", "NG", "01378560", "NPS_JB2", 
    "01445160")), .Names = c("orgid", "locid", "stdate", "sttime", 
"charnam", "val", "valunit", "swqs", "WMA", "year", "locid2"), row.names = c(NA, 
-10L), class = c("tbl_df", "tbl", "data.frame"))

UI

library(tidyverse)
library(shiny)
library(shinydashboard)
parameters<-unique(roadsalt_data$charnam)

header<-dashboardHeader()
body<- dashboardBody(
 fluidRow(
              box(width = 6,plotOutput("plot1")),
              box(width = 6, plotOutput("plot2"))))
sidebar<-dashboardSidebar(
     selectInput("parameter_input","Select Parameter",
                  parameters),
      sliderInput("alpha","Select Shade of Point",min = 0,max = 0.8,
                  value = c(0,0.8)),
      sliderInput("date","Select Date Range",
                  min = 1997,
                  max = 2015,
                  value = c(1997,2015),
                  sep = "",
                  step = 1))

ui<- dashboardPage(header = header,
                   sidebar = sidebar,
                   body = body)

Server

server<- function(input,output,session){
  
  ### Create reactive dataframe ###
  
  parameter_selected<- reactive({
    roadsalt_data%>%
      filter(charnam == input$parameter_input)%>%
      filter(year >= input$date[1])%>%
      filter(year <= input$date[2])
  })
  
  ### Creates plots ### 
  output$plot1<- renderPlot({
    ggplot(data= parameter_selected(),aes(x=factor(year),y=val))+
      geom_boxplot(alpha = input$alpha)
  })
  
  output$plot2<- renderPlot({
    ggplot(data = parameter_selected(),aes(x=factor(year),y = val))+
      geom_point(alpha =  input$alpha)
  })
}
shinyApp(ui,server)

Any help or guidance would be greatly appreciated! Thanks!


#2

FYI, having browser() in your renderPlot really helps debugging.
You are passing a vector of length 2 as your alpha (input$alpha is a slider with 2 handles). Here ggplot is expecting a single number for alpha, so you need to subset it.


#3

Worked! Thank you so much!!!