Boxplots in R Shiny App showing only flat lines

Viewed 51

I have looked at similar posts and the responses don't seem to answer my question. I am trying to develop an R Shiny App (This is my first Shiny App) to draw a boxplot for some data. I am adapting it from some code that produces accurate boxplots in the console. The plots should look similar to this (I did simplify the labels for the app):

correct plot

When I run the app, I see the following:

app plot

Here is some reproducible code. Any insights would be greatly appreciated:


    library(dplyr)
    library(ggplot2)
    library(shiny)

    AlkCalcs2 <- data.frame(matrix(ncol = 6, nrow = 250))
    AlkCalcs2$climate <- sample.int(2, 250, replace = TRUE)
    AlkCalcs2$block <- sample.int(3, 250, replace = TRUE)
    AlkCalcs2$treatment <- factor(sample.int(4, 250, replace = TRUE), 
                           labels = c("Control", "Compost", "Basalt", "Basalt and Compost"))
    AlkCalcs2$pre_dilution_alk_endp <- rnorm(250, 91, 58)
    AlkCalcs2$pre_dilution_alk_infl <- rnorm(250, 65, 59)
    AlkCalcs2$pre_dilution_alk_gran <- rnorm(250, 72, 55)

    # Define server logic
    server <- function(input, output, session) {
        output$boxplot <- renderPlot({
                ggplot(data = filter(AlkCalcs2, 
                                     climate == input$climate, 
                                     block %in% input$block)) +
                geom_boxplot(mapping = aes(x = treatment, y = input$method))  +
                labs(y = "Alkalinity",
                    x = element_blank(),
                    title = paste("Climate ", input$climate, sep = ""),
                    subtitle = paste("Block(s) ", input$block, sep = "")) +
                theme(plot.title = element_text(size = 16, hjust = 0.5),
                      plot.subtitle = element_text(size = 16, hjust = 0.5),
                      axis.text = element_text(size = 12),
                      axis.title = element_text(size = 14))
        })
    }

    # Define UI for application that draws a boxplot
    ui <- fluidPage(

        # Application title
        titlePanel("Soybean Titration Analysis"),

        # Sidebar with inputs 
        sidebarLayout(
            sidebarPanel(
                radioButtons(inputId = "method",
                             label = "Alkalinity Calculation Method:",
                             choiceNames  = c('Endpoint', 'Inflection Point', 'Gran Plotting'),
                             choiceValues = c('pre_dilution_alk_endp', 
                                              'pre_dilution_alk_infl', 
                                              'pre_dilution_alk_gran')),
                radioButtons(inputId = "climate",
                             label = "Select Climate:",
                             choices = c(1, 2)),
                checkboxGroupInput(inputId = "block",
                            label = "Select Block (Choose at least one):",
                            choices = c(1, 2, 3),
                            selected = 1)
            ),

            # Show a boxplot of the data
            mainPanel("Boxplot", plotOutput('boxplot'))
        )
    )

    # Run the application 
    shinyApp(ui = ui, server = server)

1 Answers

It's more a ggplot issue than Shiny issue. Your plot data isn't right.

library(dplyr)
library(ggplot2)
library(shiny)

AlkCalcs2 <- data.frame(matrix(ncol = 6, nrow = 250))
AlkCalcs2$climate <- sample.int(2, 250, replace = TRUE)
AlkCalcs2$block <- sample.int(3, 250, replace = TRUE)
AlkCalcs2$treatment <- factor(sample.int(4, 250, replace = TRUE), 
                              labels = c("Control", "Compost", "Basalt", "Basalt and Compost"))
AlkCalcs2$pre_dilution_alk_endp <- rnorm(250, 91, 58)
AlkCalcs2$pre_dilution_alk_infl <- rnorm(250, 65, 59)
AlkCalcs2$pre_dilution_alk_gran <- rnorm(250, 72, 55)
AlkCalcs2[['pre_dilution_alk_endp']]
# Define server logic
server <- function(input, output, session) {
  output$boxplot <- renderPlot({
    plot_data <- filter(AlkCalcs2, 
                        climate == input$climate, 
                        block %in% input$block)
    ggplot(data = plot_data) +
      geom_boxplot(mapping = aes(x = treatment, y = .data[[input$method]]))  +
      labs(y = "Alkalinity",
           x = element_blank(),
           title = paste("Climate ", input$climate, sep = ""),
           subtitle = paste("Block(s) ", input$block, sep = "")) +
      theme(plot.title = element_text(size = 16, hjust = 0.5),
            plot.subtitle = element_text(size = 16, hjust = 0.5),
            axis.text = element_text(size = 12),
            axis.title = element_text(size = 14))
  })
}

# Define UI for application that draws a boxplot
ui <- fluidPage(
  
  # Application title
  titlePanel("Soybean Titration Analysis"),
  
  # Sidebar with inputs 
  sidebarLayout(
    sidebarPanel(
      radioButtons(inputId = "method",
                   label = "Alkalinity Calculation Method:",
                   choiceNames  = c('Endpoint', 'Inflection Point', 'Gran Plotting'),
                   choiceValues = c('pre_dilution_alk_endp', 
                                    'pre_dilution_alk_infl', 
                                    'pre_dilution_alk_gran')),
      radioButtons(inputId = "climate",
                   label = "Select Climate:",
                   choices = c(1, 2)),
      checkboxGroupInput(inputId = "block",
                         label = "Select Block (Choose at least one):",
                         choices = c(1, 2, 3),
                         selected = 1)
    ),
    
    # Show a boxplot of the data
    mainPanel("Boxplot", plotOutput('boxplot'))
  )
)

# Run the application 
shinyApp(ui = ui, server = server)

enter image description here

Related