首页 > 解决方案 > selectInput 不输出数值

问题描述

我正在构建一个闪亮的应用程序,将不同的值添加到条形图中。我已经让用户输入 numericInput 函数工作,但 selectInput 函数有问题。有没有一种简单的方法可以从这些 selectInput() 函数中获取数值?必须有!

# 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)
})

您可以看到我需要在反应函数中将值相加。selectInput() 可能没有正确写出,或者也许有一种聪明的方法可以使“input$disposal_1”参数产生一个数值?

# 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)

标签: rshinyselectinput

解决方案


答案非常简单:将 input$disposal 参数包装在 as.numeric() 中

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

哇,哈哈。我认为答案会复杂得多!


推荐阅读