Search code examples
rplotshinyrenderselectinput

Create graph based on selection of input and output


New to shiny. I am trying to create a plot based on chosen x and y values. Basically, whatever the user selects for the select1 and select2 selectInput function will graph it accordingly. My original data has many columns, not just two. When I try to graph very specific things, my code works great, but when I try to graph what the user "selects" it does not work.

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

data0 <- rnorm(n = 10, mean = 100, sd = 5)
data1 <- rnorm(n = 10, mean = 50, sd = 10)
data2 <- data.frame(data0, data1)


attach(data2)

ui <- fluidPage(
  
  
  selectInput(inputId = "select1",
              label = "select the x-axis",
              choices = c(" ", "data0", "data1")
  ),
  
  selectInput(inputId = "select2",
              label = "select the y-axis",
              choices = c(" ", "data0", "data1")
  ),
  
  
  submitButton(text = "Apply Changes", icon = NULL, width = NULL),
  
  plotOutput(outputId = "myplot")
  
)


server <- function(input, output) {
  
  
  output$myplot <- renderPlot({
    
    
    data2 %>%
      ggplot(aes(input$select1 ~ input$select2))+
      geom_point(alpha = 0.8)
    
  })
  
}


shinyApp(ui = ui, server = server)

Solution

  • I had to add ggplot(aes(x = get(paste(input$select1)), y = get(paste(input$select2)) to make the input selects work.

        library(shiny)
        library(readr)
        library(ggplot2)
        library(dplyr)
        
        data0 <- read_csv("DeltaX_Soil_Properties_Fall2020_Spring2021_Fall2021.csv")
        data1 <- data0[!(data0$time_marker_sampled == "-9999"),]
        attach(data1)
        
        ui <- fluidPage(
          
        
          selectInput(inputId = "select1",
                      label = "select the x-axis",
                      choices = c(" ", "elevation_navd88", "sediment_accretion", "days_between_sampling_and_deployment", "normalized_accretion", "soil_bulk_density", "soil_organic_matter_content", "soil_organic_carbon", "soil_organic_carbon_density")
          ),
          
          selectInput(inputId = "select2",
                      label = "select the y-axis",
                      choices = c(" ", "elevation_navd88", "sediment_accretion", "days_between_sampling_and_deployment", "normalized_accretion", "soil_bulk_density", "soil_organic_matter_content", "soil_organic_carbon", "soil_organic_carbon_density")
          ),
          
          
          submitButton(text = "Apply Changes", icon = NULL, width = NULL),
          
           plotOutput(outputId = "myplot")
          
        )
        
        
        server <- function(input, output) {
          
          
          output$myplot <- renderPlot({
            
        
              data1 %>%
              ggplot(aes(x = get(paste(input$select1)), y = get(paste(input$select2)), col = hydrogeomorphic_zone))+
              geom_point(alpha = 0.8)
                
            })
          
        }
        
        
        shinyApp(ui = ui, server = server)