简体   繁体   中英

Filtering rows of data frame by checkboxGroupInput

This is shiny app where the output is the data.frame iris . The rows can be filter by checkboxes. I think my code is a bit messy and to complicated.
Does anybody have idea how to keep it simple, silly?

library(shiny)

# ui ###########################################################################
ui <- fluidPage(
  checkboxGroupInput("filterSpecies", "filterSpecies",
                     selected = c("setosa", "Sepal.WidthBiggerThen3"),
                     c("setosa", "Sepal.WidthBiggerThen3")
  ),
  tableOutput("table")
)


# global #######################################################################
load(iris)

filter <- function(dt, vars){
  # only if there is min 1 no NA element
  if(any(!is.na(vars))){
    vars <- unlist(strsplit(vars, "\\s+"))
  }

  # value of checkbox
  var1exist <- "setosa" %in% vars
  var2exist <- "Sepal.WidthBiggerThen3" %in% vars

  cond1 <- dt$Species == "setosa"
  cond2 <- dt$Sepal.Width > 3

  # negate if the checkbox is false
  if(var1exist){
    cond1 <- T
  }else{
    cond1 <- !cond1
  }

  if(var2exist){
    cond2 <- T
  }else{
    cond2 <- !cond2
  }

  condition <- cond1 & cond2
  dt[condition, ]
}


# server #######################################################################
server <- function(input, output){

  values <- reactiveValues(x = iris)

  output$table <- renderTable({(values$x)})

  # filtering
  observe({
    values$x <- filter(iris, input$filterSpecies)
  })
}


shinyApp(ui, server)

you are making it more complex than it has to be in stead of using reactValues and observe try using reactive functions.

And if you are asigning a value in both cases of an if statment try putting the if statment in the asignment

library(shiny)

# ui ###########################################################################
ui <- fluidPage(
  checkboxGroupInput("filterSpecies", "filterSpecies",
                     selected = c("setosa", "Sepal.WidthBiggerThen3"),
                     c("setosa", "Sepal.WidthBiggerThen3")
  ),
  tableOutput("table")
)


# global #######################################################################
load(iris)



# server #######################################################################
server <- function(input, output){
  filter <- reactive({
    # only if there is min 1 no NA element
    dt = values()
    vars = input$filterSpecies



    # negate if the checkbox is false
    cond1 <- if("setosa" %in% vars){
      dt$Species == "setosa"
    }else{
      dt$Species != "setosa"
    }

    cond2 <- if("Sepal.WidthBiggerThen3" %in% vars){
      dt$Sepal.Width > 3
    }else{
      dt$Sepal.Width <= 3
    }
    condition <- cond1 & cond2
    dt[condition, ]
  })

  values <- reactive({iris})

  output$table <- renderTable({filter()})

  # filtering

}


shinyApp(ui, server)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM