I think your code has several problems. Since we do not know the original data, I show an example with the iris dataset. It should be easily adaptable to your problem.
library(shiny)
library(dplyr)
myiris <- mutate(iris, Species = as.factor(Species))
shinyApp(
ui = fluidPage(
selectInput(inputId = "select",
label = "Choose subset of analysis",
choices = c("all", levels(myiris$Species)),
selected = "all"),
verbatimTextOutput("summary")
),
server = function(input, output) {
datasetInput <- reactive({
req(input$select)
filter(myiris, input$select == "all" | Species == input$select)
# alternatively `subset` works as well:
# subset(myiris, input$select == "all" | Species == input$select)
})
output$summary <- renderText(summary(datasetInput()))
})
Why does filter(myiris, input$select == "all" | Species == input$select) work?
filter and also subset take an expression as second argument. It is important that
- this can be any logical expression. The expression does not necessarily need to refer to a column name.
- the expression will be recylced if it does not match the number of rows of the
data.frame. Notice that subset and filter have different recycling rules. However, both will recycle an expresison of length == 1 to the number of rows of the data.frame.
- The expression may contain boolean operators (
&, | etc.)
With this in mind we can construct the expression:
input$select == "all" | Species == input$select
Since input$select == "all" is of length == 1 it will be recycled to match the number of elements in Species == input$select which is the number of rows of the data.frame.
Since we use an OR operator |, the expression will be TRUE for every row when all is selected. If all is not selected, the expression Species == input$select will be TRUE for all rows that match the selected input$select.