Jana P
Jana P

Reputation: 25

Is there a R function to apply in filter option in R shiny

I have the below code. I need to put a main filter in mainpanel so that i can select the categories that in turn should change the numbers (summary)

# faithful is the dataset
# Iris is the dataset
 iris$New <- ifelse(iris$Sepal.Width>2.5,"greater than 2.5","Not Greater 
 than 2.5")
library(shiny)

sample1 <- 1:3

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

server <- function(input, output, session) {
  output$message <- renderPrint({
    if(input$x == "summary"){
      summary(iris$Petal.Width)
    } else if (input$x == "stem"){
      print(stem(faithful$eruptions))
    } else if (input$x == "typeof"){
      typeof(sample1)
    } else if (input$x == "mode"){
      mode(sample1)
    } 
  }) 
}

shinyApp(ui, server)

Can I have a main filter of "Species" from Iris dataset. When I select "setosa",the summary should change accordingly

Upvotes: 1

Views: 77

Answers (1)

akrun
akrun

Reputation: 887291

If we need to change the summary based on the values of 'Species', use renderUI with uiOutput

sample1 <- 1:3
library(shiny)
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)

-output

enter image description here

Upvotes: 1

Related Questions