Shiny Dynamic Filter variable selection and display of variable values for selection

后端 未结 1 404
旧时难觅i
旧时难觅i 2020-12-04 03:34

I am still learning Shiny and R and feel it is a sea where I still need to learn quite a lot. Please excuse me if my method of coding is not ideal and do suggest where the c

相关标签:
1条回答
  • 2020-12-04 04:25

    Here is a way to subset your data frame in function of selected values for the desired column.

    I didn't really understand what you wanted to do with the row and column select input though.

    ui <- navbarPage("My Shiny App",
                     tabPanel("Insights",
                              sidebarPanel(
                                fileInput("file1", "Choose input data"),
                                selectInput("filtervar", "Select Filter Variable", NULL),
                                checkboxGroupInput("filteroptions", "Filter Options", NULL)
                                ),
                              mainPanel(
                                tabsetPanel(id = "mytabs",
                                            tabPanel("Data", tableOutput("table.output"))
                                )
                              )
                     )
    )
    
    server <- function(input, output,session) {
    
      values <- reactiveValues()
    
      observe({
        file <- input$file1
    
        if (is.null(file))
          return()
    
        values$data <- fread(file$datapath)
    
        vars <- names(values$data)
    
        updateSelectInput(session, "filtervar", choices = vars)
      })
    
      observe({
    
        data <- isolate(values$data)
    
        filter.var <- input$filtervar
    
        if (is.null(filter.var) || filter.var == "")
          return()
    
        values <- data[[filter.var]]
    
        if (is.factor(values)) {
          options <- levels(values)
        } else {
          options <- unique(values[order(values)])
        }
    
        updateCheckboxGroupInput(session, "filteroptions", 
                                 choices = options, 
                                 selected = as.character(options))
    
      })
    
      output$table.output <- renderTable({
    
        isolate({
          data <- values$data
          var <- input$filtervar
        })
    
        values <- input$filteroptions
    
        if(is.null(data)) {
          return()
        } else if (is.null(var) || var == "") {
          return(data)
        } else if (is.null(values)) {
          return(data[FALSE])
        } else {
    
          if (is.numeric(data[[var]]))
            values <- as.numeric(values)
    
          setkeyv(data, var)
          return(data[.(values)])
        }
    
      })
    
    
    }
    
    shinyApp(ui = ui, server = server)
    
    0 讨论(0)
提交回复
热议问题