selectInput is not outputting numeric values

1k views Asked by At

I'm building a Shiny app that adds different values together into a bar chart. I've gotten the user input numericInput functions to work, but the selectInput function is bugging. Is there a simple way I can get numeric values from these selectInput() functions? There must be!

# values for the selectInput.
Buyback <- 10
Dump <- 30
Reuse <- 20

rec_choice <- data.frame(Buyback, Dump, Reuse)

# Define UI for application 
ui <- fluidPage(

    titlePanel("Shiny app with buggy inputSelect"),
  
    # Sidebar with input.
    pageWithSidebar(
 sidebarPanel(
        helpText("Product 1 information:"),

# Numeric inputs, works! 
        numericInput(
            inputId = "price_1",
            label = "Purchase Price",
            value = "0",
            min = 0,
            width = '50%'
        ),
        numericInput(
            inputId = "install_1",
            label = "Installation cost",
            value = "0",
            min = 0,
            width = '50%'
        ),
# here is the select input function  
        selectInput("disposal_1", "Recycling cost",
                       rec_choice),

#I though this approach may work, but no luck. 
 
# selectInput("disposal_2", "Recycling cost",
        #             choices = c("Buyback" = 10,
        #                         "Dump" = 20,
        #                         "Reuse" = 5)),


# server side.
server <- function(input, output) {

  select_price <- reactive({
# error says I've got a "non-numeric argument to binary operator."
    c(input$price_1 + input$install_1 + input$disposal_1)
})

You can see that I need to sum values together in a reactive function. The selectInput() may not be written out correctly, or perhaps there is a clever way to make the "input$disposal_1" argument produce a numeric value?

# The chart output: 
    my_bar <- output$costPlot <- renderPlot({
        barplot(select_price(), 
                beside = T,
                border=F, 
                las=2 , 
                col=c(rgb(0.3,0.9,0.4,0.6) ,  
                      rgb(0.3,0.9,0.4,0.6)) , 
                space = 3,
                main="" )
      par(mar=c(6,4,4,4))
      abline(v=c(4.9 , 6.1) , col="grey")
      
      my_bar      
         })    
}

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

There are 1 answers

0
Averysaurus On

the answer is remarkably straight forward: wrap the input$disposal argument in as.numeric()

select_price <- reactive({ c(input$price_1 + input$install_1 + as.numeric(input$disposal_1))

wow, haha. I thought an answer would be much more complicated!