0
votes

Trying to produce a ggplot2 boxplot in shiny that plots specific values on the x-axis depending on a variable you select. I would very much appreciate your help!

I've made an example here with mtcars where the user selects number of gears and gets an output of hp for various cylinders.

How do I force shiny to display different combinations & orders of cylinders on the x axis? E.g., when the user selects 3 gears, I want the cylinder order to be 4, 8, 6. If the user selects 4 gears, I want the cylinder order to be 6, 4. If the user selects 5 gears, I want the order to be 8, 4 (omitting 6).

Please help! :)

Here's the online app: https://dwok.shinyapps.io/CarEg/

Here is my code:

server.r

library(shiny)
library(datasets)
library(ggplot2)

shinyServer(function(input, output) {

  # Use selected variables to shrink size of data frame
  selectedData <- reactive({
  mtcars[mtcars$gear == input$gear, ]
  })
  # THIS BIT IS NOT WORKING. WHY NOT???
  # Determine order of cylinders to display on x axis
  cylOrder <- reactive({
    if (input$gear == "3") {
    cylOrder <- c("4","8","6")
    } else if (input$gear == "4") {
    cylOrder <- c("6","4")
  } else if (input$gear == "5") {
    cylOrder <- c("8","4")     # yes, I don't want 6 cyl to appear
  }      
  })  
  
  # Compute the forumla text in a reactive expression
    formulaText <- reactive({
    paste("The number of gears you have selected is", input$gear)
  })

  # Return the formula text for printing as a caption
  output$caption <- renderText({
    formulaText()
  })

  # Generate a plot of the requested variable against duration of phase
    output$gearPlot <- renderPlot({
    boxplot(selectedData()$hp ~ selectedData()$cyl,
          xlab = 'cylinders', ylab = 'hp', 
          main = 'hp by cylinder for gears     selected',
#STUCK HERE!  order (cylOrder()),
          outline = FALSE
          )
  })
  output$mygear <- renderText(input$gear)
  }
)

ui.r

# using mtcars data set, select number of gears to reveal hp by cylinder 
  library(shiny)
# Define UI for Shiny app
  shinyUI(fluidPage(
  # Application title
    titlePanel(title = h2("mtcars hp by cylinders", align = "center")),
  # Sidebar layout
    sidebarLayout(position = "left",
  # Sidebar panel
    sidebarPanel  (h4("Select number of"),
                  selectInput("gear", "Gears?", c("3", "4", "5"),
                  selected =    "3")
              ),
  # Main Panel
    mainPanel     (h3("Car info:"),
                  textOutput("caption"),
                  plotOutput("gearPlot")
                  )
                )
                  )
          )

Please see https://dwok.shinyapps.io/CarEg/ for the app

1

1 Answers

0
votes

Change the reactive to

cylOrder <- reactive({
    if (input$gear == "3") {
        c("4","8","6")
    } else if (input$gear == "4") {
        c("6","4")
    } else if (input$gear == "5") {
        c("8","4")     # yes, I don't want 6 cyl to appear
    }      
})  

And change the boxplot to

    boxplot(selectedData()$hp ~ selectedData()$cyl,
            xlab = 'cylinders', ylab = 'hp', 
            main = 'hp by cylinder for gears     selected',
            subset = selectedData()$cyl %in% cylOrder(),
            outline = FALSE
    )