首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >selectInput未输出数值

selectInput未输出数值
EN

Stack Overflow用户
提问于 2020-11-25 06:15:35
回答 1查看 304关注 0票数 0

我正在构建一个闪亮的应用程序,将不同的值一起添加到条形图中。我已经让user input numericInput函数正常工作,但是selectInput函数出现了错误。有没有一种简单的方法可以从这些selectInput()函数中获取数值?一定是有的!

代码语言:javascript
复制
# 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“参数生成一个数值?

代码语言:javascript
复制
# 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)
EN

回答 1

Stack Overflow用户

发布于 2020-11-25 09:28:16

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

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64995647

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档