I posted a similar question before (Errors in if statements in R Shiny checkBoxGroupInput)
but as someone pointed out I had not included relevant data. Here is my code:
library(shiny)
library(leaflet)
library(DT)
library(ggplot2)
library(dplyr)
r_colors <- rgb(t(col2rgb(colors()) / 255))
names(r_colors) <- colors()
plotdata <- read.csv("C:/Users/Anatoly/Documents/Collatz/RenameLater/RenameLater/MapsAndSuch/RShinyCoral.csv")
colnames(plotdata) <- c("Year1", "RLIMona", "Year2", "RLICatalina", "Year3", "RLILaParguera1998", "Year4", "RLILAPARGUERA2004")
parguera <- read.csv("C:/Users/Anatoly/Documents/Collatz/RenameLater/RenameLater/MapsAndSuch/RShinyCoral.csv")
parguera <- select(parguera, 5:8)
colnames(parguera) <- c("Year", "1998 Expedition", "Year", "2004 Expedition")
monaisland <- read.csv("C:/Users/Anatoly/Documents/Collatz/RenameLater/RenameLater/MapsAndSuch/RShinyCoral.csv")
monaisland <- select(monaisland, 1:2)
colnames(monaisland) <- c("Year", "Mona Island RLI")
islacatalina <- read.csv("C:/Users/Anatoly/Documents/Collatz/RenameLater/RenameLater/MapsAndSuch/RShinyCoral.csv")
islacatalina <- select(islacatalina, 3:4)
colnames(islacatalina) <- c("Year", "Isla Catalina RLI")
ui <- fluidPage(
titlePanel("NOAA Coral Luminescence Data (RLI, 5-year Running Average)"),
leafletOutput("mymap"),
p(),
fluidRow(
column(3, actionButton("laparguera", "La Parguera Data"),
actionButton("mona", "Mona Island Data"),
actionButton("isla", "Isla Catalina Data"))),
fluidRow(
column(3, offset = 5, actionButton("visualize", "Visualize Data"))),
fluidRow(
column(7, offset = 5, checkboxGroupInput("checkbox", "Add to plot",
c("La Parguera" = "La Parguera", "Mona Island" = "Mona Island", "Isla Catalina" = "Isla Catalina"))),
fluidRow(
DT::dataTableOutput('tbl'),
plotOutput("plot1")
)
)
)
server <- function(input, output, session) {
output$mymap <- renderLeaflet({
leaflet() %>%
addTiles() %>%
addMarkers(lat = 17.95, lng = - 67.05, popup = "La Parguera ") %>%
addMarkers(lat = 18.00, lng = -67.50, popup = "Mona Island") %>%
addMarkers(lat = 18.2, lng = -69.00, popup = "Isla Catalina")
})
observeEvent(input$laparguera, {
output$tbl <- DT::renderDataTable(DT::datatable(parguera, options = list(pagelength = 25)))
})
observeEvent(input$mona, {
output$tbl <- DT::renderDataTable(DT::datatable(monaisland, options = list(pagelength = 25)))
})
observeEvent(input$isla, {
output$tbl <- DT::renderDataTable(DT::datatable(islacatalina, options = list(pagelength = 25)))
})
observeEvent(input$visualize, {
output$plot1 <- renderPlot(
{
if (input$checkbox == c("Mona Island"))
{
gplot <- ggplot(data = plotdata) +
geom_polygon(mapping = aes(x = Year1, y = RLIMona), na.rm = TRUE) +
ylab("Candelas (5-year Running Average)" )
print(gplot)
}
else if(input$checkbox == c("La Parguera", "Mona Island"))
{
gplot2 <- ggplot(data = plotdata) +
geom_polygon(mapping = aes(x = Year1, y = RLIMona), na.rm = TRUE) +
ylab("Candelas (5-year Running Average)" )
print(gplot2)
}
else if (input$checkbox == c("La Parguera", "Mona Island", "Isla Catalina"))
{
gplot3 <- ggplot(data = plotdata) +
geom_polygon(mapping = aes(x = Year1, y = RLIMona), na.rm = TRUE) +
ylab("Candelas (5-year Running Average)" )
print(gplot3)
}
else if(input$checkbox == c("Mona Island", "Isla Catalina"))
{
gplot4 <- ggplot(data = plotdata) +
geom_polygon(mapping = aes(x = Year1, y = RLIMona), na.rm = TRUE) +
ylab("Candelas (5-year Running Average)" )
print(gplot4)
}
else if(input$checkbox == c("La Parguera", "Isla Catalina"))
{
gplot5 <- ggplot(data = plotdata) +
geom_polygon(mapping = aes(x = Year1, y = RLIMona), na.rm = TRUE) +
ylab("Candelas (5-year Running Average)" )
print(gplot5)
}
else if(input$checkbox == c("Isla Catalina"))
{
gplot6 <- ggplot(data = plotdata) +
geom_polygon(mapping = aes(x = Year1, y = RLIMona), na.rm = TRUE) +
ylab("Candelas (5-year Running Average)" )
print(gplot6)
}
else if(input$checkbox == c("La Parguera"))
{
gplot7 <- ggplot(data = plotdata) +
geom_polygon(mapping = aes(x = Year1, y = RLIMona), na.rm = TRUE) +
ylab("Candelas (5-year Running Average)" )
print(gplot7)
}
else if(is.null(input$checkbox))
{
print("Check some damn boxes")
}
})
})
}
shinyApp(ui, server)
And here is my data: filedropper.com/rshinycoral
I am having a problem using if statements to determine which plot will appear in the output, depending on which boxes are checked off. Please ignore for now the fact that my plots are the same, I'll change this later.
I am aware using if statements is not optimal, but I am having trouble using Enzo's suggestions.
Thanks!