在我闪亮的应用程序中,我有三个输入。用户想要选择文章的selectInput()
;用户想要指定股票的textInput()
;触发tableOutput()
的actionButton()
。此tableOutput()
为用户提供一个数据帧,其中数据帧中的第一个条目与筛选器匹配。
例如:用户想知道他在selectInput()
中选择的文章是在哪一周在textInput()
中选择的股票。所以他选择了文章34343
,并想知道文章34343
什么时候有100个可用的库存。输出将为他提供第1周的answere,因为有210.57的可用库存。
我在这里面临的问题是,当我指定一个大于210,57的股票时,例如211,预期的周应该是第3周,但输出显示它的第2周的股票为94.42时,这基本上意味着94.42时大于210.57。
这怎么可能呢?
library(shiny)
library(tidyverse)
df = tibble(id=as.integer(c(34343, 34343, 34343)), week=as.integer(c(1,2,3)), stock=as.double(c(210.57,94.42,412.31)))
ui <- fluidPage(
selectInput(inputId = "id", "Select", choices = df$id, multiple = F, selectize = T),
textInput(inputId = "stock", "Stock", placeholder = "i.e. 100"),
actionButton(inputId = "click", "Click"),
tableOutput(outputId = "table")
)
server <- function(input, output) {
result = eventReactive(input$click, {
df %>% filter(id %in% input$id) %>%
distinct(id, week, stock) %>%
group_by(id) %>%
slice(first(which(stock >= input$stock)))
})
output$table = renderTable({
result()
})
}
# Run the application
shinyApp(ui = ui, server = server)
发布于 2020-09-16 12:49:11
您的slice()
语句需要为:
slice(first(which(stock >= as.numeric(input$stock))))
因为input$stock
是一个不会自动转换为数字的字符串。考虑一下这两个不同的过滤器语句是如何工作的:
> filter(df, stock >= "212")
# # A tibble: 2 x 3
# id week stock
# <int> <int> <dbl>
# 1 34343 2 94.4
# 2 34343 3 412.
> filter(df, stock >= 212)
# # A tibble: 1 x 3
# id week stock
# <int> <int> <dbl>
# 1 34343 3 412.
下面的代码产生了预期的输出。
library(shiny)
library(tidyverse)
df = tibble(id=as.integer(c(34343, 34343, 34343)), week=as.integer(c(1,2,3)), stock=as.double(c(210.57,94.42,412.31)))
ui <- fluidPage(
selectInput(inputId = "id", "Select", choices = df$id, multiple = F, selectize = T),
textInput(inputId = "stock", "Stock", placeholder = "i.e. 100"),
actionButton(inputId = "click", "Click"),
tableOutput(outputId = "table")
)
server <- function(input, output) {
result = eventReactive(input$click, {
df %>% filter(id %in% input$id) %>%
distinct(id, week, stock) %>%
group_by(id) %>%
slice(first(which(stock >= as.numeric(input$stock))))
})
output$table = renderTable({
result()
})
}
# Run the application
shinyApp(ui = ui, server = server)
https://stackoverflow.com/questions/63920150
复制