Home > Software engineering >  Creating a histogram of a selectInput function in R shiny
Creating a histogram of a selectInput function in R shiny

Time:10-12

I am new to r shiny. I am simply trying to get my program to call on hist(Atchafalaya_data$soil_organic_carbon_density) or hist(Terrebonne_data$soil_organic_carbon_density) when a user selects Atchafalaya or Terrebonne.

I keep getting an error that says "'x' must be numeric", so I am assuming that it is actually doing hist(select).

Here is my code:

library(shiny)
library(readr)
library(tidyverse)

data <- read_csv("Data/DeltaX_Soil_Properties_Fall2020_Spring2021_Fall2021.csv")
data2 <- data[!(data$time_marker_sampled=="-9999"),]
Atchafalaya_data <- data2[!(data2$basin == "Terrebonne"),]
Terrebonne_data <- data2[!(data2$basin == "Atchafalaya"),]

ui <- fluidPage(

  selectInput( inputId = "select", 
               label = "Sections of data", 
               choices = c("Atchafalaya" = "Atchafalaya_data$soil_organic_carbon_density", "Terrebonne" = "Terrebonne_data$soil_organic_carbon_density")),
  
  plotOutput(outputId = "hist")
  
)



server <- function(input, output) {
  output$hist <- renderPlot({
    
    hist(input$select)
    })
 
}


shinyApp(ui = ui, server = server)

The values in Atchafalaya_data$soil_organic_carbon_density return something like this:

Atchafalaya_data$soil_organic_carbon_density
[1] 0.012 0.017 0.015 0.016 0.005 0.005 0.015 0.015 0.022 0.011 0.009 0.014 0.032 0.031 0.031 0.019 0.029
 [18] 0.033 0.012 0.036 0.032 0.035 0.033 0.039 0.037 0.027 0.032 0.024 0.043 0.025 0.025 0.034 0.078 0.028
 [35] 0.033 0.032 0.018 0.010 0.012 0.011 0.015 0.012 0.011 0.013 0.011 0.002 0.002 0.004 0.003 0.002 0.002
 [52] 0.003 0.020 0.020 0.017 0.021 0.021 0.018 0.022 0.021 0.023 0.011 0.013 0.007 0.007 0.010 0.009 0.006
 [69] 0.011 0.008 0.010 0.012 0.009 0.012 0.010 0.008 0.010 0.012 0.012 0.014 0.010 0.014 0.013 0.010 0.013
 [86] 0.012 0.011 0.017 0.011 0.009 0.010 0.018 0.018 0.020 0.010 0.012 0.015 0.011 0.020 0.019 0.019 0.025
[103] 0.031 0.029 0.013 0.020 0.020 0.016 0.006 0.016 0.010 0.035 0.014 0.015 0.008 0.005 0.004 0.005 0.004
[120] 0.004 0.005 0.007 0.006 0.006 0.006 0.005 0.004 0.014 0.017 0.021 0.009 0.009 0.015 0.001 0.004 0.003
[137] 0.003 0.002 0.003 0.008 0.009 0.007 0.008 0.006 0.009 0.012 0.006 0.012 0.004 0.005 0.007 0.019 0.011
[154] 0.012 0.008 0.011 0.011 0.013 0.012 0.010 0.010 0.008 0.012 0.010 0.010 0.014 0.017 0.012 0.015 0.008
[171] 0.012 0.009 0.011 0.012 0.010 0.005 0.006 0.005 0.003 0.005 0.007 0.006 0.004 0.005 0.017 0.009 0.013
[188] 0.015 0.013 0.003 0.004 0.004 0.004 0.004 0.003 0.008 0.010 0.007 0.008 0.008 0.008

Thank you for your help. It has been a frustrating day.

CodePudding user response:

Since you did'nt share the data.frames (only a column of one) i put an example with random data. In the Choices option simply put the options then select the dataframe variable get(paste0(input$select, "_data"))$soil_organic_carbon_density when rendering the plot.

library(shiny)
library(readr)
library(tidyverse)

Atchafalaya_data <- data.frame(soil_organic_carbon_density = runif(100))
Terrebonne_data <-  data.frame(soil_organic_carbon_density = runif(200))

ui <- fluidPage(
  
  selectInput( inputId = "select", 
               label = "Sections of data", 
               choices = c("Atchafalaya"  , "Terrebonne" )),
  
  plotOutput(outputId = "hist")
  
)



server <- function(input, output) {
  output$hist <- renderPlot({
    data<-get(paste0(input$select, "_data"))$soil_organic_carbon_density
    hist(data)
  })
  
}


shinyApp(ui = ui, server = server)

  • Related