-1

Can I have a multiple filter. I have created a new column in Iris data set called "New". I also want "New" column filter along with "Species" filter. Below is the code for reference

sample1 <- 1:3
library(shiny)
iris$New <- ifelse(iris$Sepal.Width>2.5,"greater than 2.5","Not Greater 
than 2.5")
ui <- fluidPage(
sidebarLayout(
sidebarPanel(selectInput("x","Operations",choices = 
                           c("summary","stem","typeof","mode","birth"),
                         multiple=FALSE,selectize = TRUE)),
mainPanel(h6("Here it is"),
          verbatimTextOutput("message"),
          uiOutput("Species")
)
)
)
server <- function(input, output, session) {
r1 <- reactive({
if(input$x == "summary")
{
  summary(iris$Petal.Width[iris$Species == input$Species])
} else if (input$x == "stem")
{
  print(stem(faithful$eruptions))
} else if (input$x == "typeof")
{
  typeof(sample1)
} else if (input$x == "mode")
{
  mode(sample1)
} 
}) 
output$message <- renderPrint({r1()})
output$Species <- renderUI({
selectInput("Species", "species", 
            choices = as.character(unique(iris$Species)), multiple = FALSE)
})
}
shinyApp(ui, server)
Jana P
  • 25
  • 5
  • Welcome to StackOverflow! Please read about [how to ask a good question](https://stackoverflow.com/help/how-to-ask) and [how to create a reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). Have you tried the answers to [this question](https://stackoverflow.com/questions/7106330/how-to-filter-a-data-frame)? –  Jul 06 '19 at 05:20
  • 1
    @gersht the example works. What's not clear to me what exactly is the expected output. Perhaps Jana can talk more about this? Do I understand correctly that you want to have an additional filter next to "species" that will filter on "new"? What are the parameters of filtering on this variable? – Roman Luštrik Jul 06 '19 at 07:24
  • @RomanLuštrik I posted the links for the same reason that you start your second sentence with "what's not clear". The point of asking a good question, and creating a minimal, reproducible example, is to make sure that we understand what is needed at a glance. If we have to spend time deducing the OP's intent then it makes sense to ask them to refine their question. It's about encouraging good habits, not admonishing bad ones. I am, by the way, not the downvoter. –  Jul 06 '19 at 08:13
  • 1
    Hi Jana, it looks you've been upvoting but not *accepting* any answers on this and other posts. Accepting answers helps others with the same problem see what best solved your issue. If my answer fixes your problem consider clicking ✓ under the vote arrows to accept. You can do this in your others posts as well. –  Jul 08 '19 at 06:54

1 Answers1

1

You need to add a uiOutput, the comparison iris$New == input$New to subset of iris$Petal.Width in summary, and output$New. You'll end up with this, which I think is what you're looking for:

shiny screenshot

EDIT: I've added another option "all" to the species input as requested by the OP in the comments below this answer. There is a condition that returns a subset based only on "new" if input$Species is "all", otherwise it returns a subset based on species and new. The req function fixes a "length of 0" error issue in the if.

Here's the code. I've added comments where I've altered things:

sample1 <- 1:3
library(shiny)
iris$New <- ifelse(iris$Sepal.Width>2.5,"greater than 2.5","Not Greater than 2.5")

ui <- fluidPage(
    sidebarLayout(
        sidebarPanel(selectInput("x","Operations",choices = 
                                     c("summary","stem","typeof","mode","birth"),
                                 multiple=FALSE,selectize = TRUE)),
        mainPanel(h6("Here it is"),
                  verbatimTextOutput("message"),
                  uiOutput("Species"),
                  uiOutput("New") # <- ADD THIS

        )
    )
)
server <- function(input, output, session) {
    r1 <- reactive({
        if(input$x == "summary")
        {
            #### MODIFY ALL OF THIS ###########################################
            req(input$Species) # <- REQUIRE INPUT BEFORE CONTINUING

            if(input$Species == "all"){
                summary(iris$Petal.Width[iris$New == input$New])
            } else {
                summary(iris$Petal.Width[iris$Species == input$Species &
                                             iris$New == input$New]) # <- ADD THIS
            }
            ###################################################################
        } else if (input$x == "stem")
        {
            print(stem(faithful$eruptions))
        } else if (input$x == "typeof")
        {
            typeof(sample1)
        } else if (input$x == "mode")
        {
            mode(sample1)
        } 
    }) 
    output$message <- renderPrint({r1()})
    output$Species <- renderUI({
        selectInput("Species", "species", 
                    choices = c("all", as.character(unique(iris$Species))), multiple = FALSE)
    })
    #### ADD ALL OF THIS ######################################################
    output$New <- renderUI({
        selectInput("New", "new", 
                    choices = as.character(unique(iris$New)), multiple = FALSE)
    })
    ###########################################################################
}

shinyApp(ui, server)
  • Perfect thanks. Can we also add "All" in the filter. Currently under "Species" we have 3 categories. By default one is selected. But if I need all to be selected can we? – Jana P Jul 08 '19 at 18:08
  • Yes, you just need to add "All" to `selectInput` for species, and then add an `ifelse` somewhere inside the first `if` in `reactive` that checks for "All". –  Jul 08 '19 at 18:23
  • I tried adding All to select input. It worked but the second one i did not get – Jana P Jul 08 '19 at 18:55
  • @JanaP I've edited the code to include an "all" option for species. If the above answer solves the issue described in your original question please upvote and click ✓ to accept. –  Jul 09 '19 at 07:35
  • Perfect thanks. But I just tried replacing renderPrint by renderDatatable and verbatimTextOutput by dataTableOutput. The logic is not at all working. It should not any difference right? – Jana P Jul 09 '19 at 18:49
  • @JanaP there is a difference because `summary` does not return a dataframe, while as far as I can tell those two functions require dataframes. –  Jul 09 '19 at 19:35
  • Well thanks. However I have posted my query in another question.https://stackoverflow.com/questions/56963616/is-there-a-r-function-to-apply-filter-for-renderdatatable – Jana P Jul 10 '19 at 04:56