【发布时间】:2018-10-25 05:26:15
【问题描述】:
我正在尝试使用闪亮的 selectInput 函数从数据框中过滤行,例如“mtcars”。因此,用户将选择他想要查看的汽车,并且 outputTable 将仅显示所选汽车的统计信息。在 selectInput 示例中,选择作用于列:
## Only run examples in interactive R sessions
if (interactive()) {
# basic example
shinyApp(
ui = fluidPage(
selectInput("variable", "Variable:",
c("Cylinders" = "cyl",
"Transmission" = "am",
"Gears" = "gear")),
tableOutput("data")
),
server = function(input, output) {
output$data <- renderTable({
mtcars[, c("mpg", input$variable), drop = FALSE]
}, rownames = TRUE)
}
)
}
但是,我正在尝试使用 dplyr 根据用户输入选择的汽车来过滤掉汽车:
library(shiny)
library(dplyr)
#####Just adding a column name to mtcars for the cars column####
#cars <- mtcars
#mtcars <-setNames(cbind(rownames(cars), cars, row.names = NULL),
# c("cars", "mpg", "cyl", "disp", "hp", "drat", "wt", "qsec", "vs", "am", "gear", "carb"))
if (interactive()) {
shinyApp(
ui = fluidPage(
selectInput("variable", "Pick a Car: ",
c("All" = "All Cars",
"Ford" = "Ford",
"Volvo" = "Volvo",
"Ferrari" = "Ferrari",
"Fiat" = "Fiat",
"Merc" = "Merc")),
tableOutput("data")
),
server = function(input, output) {
output$cars <- renderText({
mtcars %>%
filter(mtcars$cars %in% as.character(input$variable))
})
output$data <- renderTable({
output$cars
})
}
)
}
任何建议将不胜感激!
【问题讨论】: