R Shiny: How to dynamically append arbitrary number of input widgets

后端 未结 4 446
没有蜡笔的小新
没有蜡笔的小新 2021-02-02 01:06

The goal

I am working on a Shiny app that allows the user to upload their own data and focus on the entire data or a subset by providing data filtering widgets describ

4条回答
  •  醉话见心
    2021-02-02 01:32

    If you are looking for a data subsetting/filtering in Shiny Module :

    filterData from package shinytools can do the work. It returns an expression as a call but it can also return the data (if your dataset is not too big).

    library(shiny)
    # remotes::install_github("ardata-fr/shinytools")
    library(shinytools)
    
    ui <- fluidPage(
      fluidRow(
        column(
          3,
          filterDataUI(id = "ex"),
          actionButton("AB", label = "Apply filters")
        ),
        column(
          3,
          tags$strong("Expression"),
          verbatimTextOutput("expression"),
          tags$br(),
          DT::dataTableOutput("DT")
        )
      )
    )
    
    server <- function(input, output) {
    
      x <- reactive({iris})
    
      res <- callModule(module = filterDataServer, id = "ex", x = x, return_data = FALSE)
    
      output$expression <- renderPrint({
        print(res$expr)
      })
    
      output$DT <- DT::renderDataTable({
        datatable(data_filtered())
      })
    
      data_filtered <- eventReactive(input$AB, {
        filters <- eval(expr = res$expr, envir = x())
        x()[filters,]
    
      })
    }
    
    shinyApp(ui, server)
    

    You can also use lazyeval or rlang to evaluate the expression :

    filters <- lazyeval::lazy_eval(res$expr, data = x())
    filters <- rlang::eval_tidy(res$expr, data = x())
    

提交回复
热议问题