I am attempting to filter rows from a data frame, such as "mtcars", using the selectInput function in shiny. So a user will select which car he wants to view and the outputTable will show only stats from the selected car. In the selectInput example, the selection works on columns:
## 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)
}
)
}
However, I am trying to use dplyr to filter out the cars based on the user input selected car:
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
})
}
)
}
Any suggestions will be much appreciated!
