r - 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)
解决方案
答案非常简单:将 input$disposal 参数包装在 as.numeric() 中
select_price <- reactive({ c(input$price_1 + input$install_1 + as.numeric (input$disposal_1))
哇,哈哈。我认为答案会复杂得多!
推荐阅读
- c++ - 优化这种“统计巧合”查找算法
- r - 如何将字符列拆分为R中的数字级别?
- linux - 在多CPU的Linux系统上,如何获取或计算指定CPU的平均负载?
- python - 熊猫数据框到镶木地板文件的转换
- laravel - 将外部表中的 id 分配给当前表 laravel
- javascript - 谷歌调查评论通过 Puppeteer 弹出 iframe
- plsql - 分析事件发生的 SQL 时间序列
- javascript - 删除元素并更新数组后获取数组的第二个元素
- c# - 在一个 SQL Server 实例上连接到 2 个数据库
- python - 将 Scanpy 小提琴图并排绘制的问题