簡體   English   中英

如何在 R Shiny 中使用 selectizeGroupUI 和 DT::datatable

[英]How to use selectizeGroupUI along with DT::datatable in R Shiny

根據selectizeGroup-module官方文檔中的例子,我可以這樣做:

library(shiny)
library(shinyWidgets)
library(dplyr)
data("mpg", package = "ggplot2")

ui <- fluidPage(
    fluidRow(
        column(
            width = 10, offset = 1,
            tags$h3("Filter data with selectize group"),
            panel(
                selectizeGroupUI(
                    id = "my-filters",
                    params = list(
                        manufacturer = list(inputId = "manufacturer", title = "Manufacturer:"),
                        model = list(inputId = "model", title = "Model:"),
                        trans = list(inputId = "trans", title = "Trans:"),
                        class = list(inputId = "class", title = "Class:")
                    )
                ),
                status = "primary"
            ),
            DT::dataTableOutput(outputId = "table")
        )
    )
)

server <- function(input, output, session) {
    
    mpgView2 <- reactive({
        mpg
    })
    
    res_mod <- callModule(
        module = selectizeGroupServer,
        id = "my-filters",
        data = mpgView2,
        vars = c("manufacturer", "model", "trans", "class")
    )
    
    output$table <- DT::renderDataTable({
        req(res_mod())
        res_mod()
    })
}

shinyApp(ui, server)

過濾器工作完美。 我的要求還告訴我這個表需要可編輯,隱藏一些列,格式化圓等。我通常會做這樣的事情:

mpgView1 <- reactive({
        DT::datatable(
            mpg,
            filter = "none",
            selection = "none",
            style = "bootstrap",
            extensions = c("Scroller", "FixedColumns"),
            options = list(
                dom = 't',
                scrollY = 500,
                scrollX = 400,
                scroller = TRUE,
                defRender = TRUE,
                autoWidth = TRUE,
                targets = "no-sort",
                bSort = FALSE,
                order = c(),
                fixedColumns = list(leftColumns = 2),
                columnDefs = list(
                    list(
                        visible = FALSE,
                        targets = c(0)
                    ),
                    list(
                        width = "50px",
                        targets = "_all"
                    )
                )
            ),
            editable = list(
                target = 'cell',
                disable = list(columns = c(0,1,2))
            )
        ) %>% 
            DT::formatRound(
                columns = c(3)
            )
        
    })

    output$table <- DT::renderDataTable({ 
     mpgView1()
    })

但現在我不確定如何“結合”這兩種功能。 如果我確實嘗試將mpgView1()放在res_mod 中,則會出現錯誤:

Warning: Error in as.data.frame.default: cannot coerce class 'c("datatables", "htmlwidget")' to a data.frame

我很感激任何幫助。 謝謝。

輸出selectizeGroupServer是你過濾后的數據作為反應,所以你可以使用這個輸出在datatable被稱呼您的需求電話。 數據表可編輯的要求帶來了一些問題: selectizeGroupServer需要知道新數據。 這是可能的,但是在我的解決方案中,表格會完全刷新並且現有的過濾器丟失。 我認為您可以嘗試使用proxy獲得更好的行為,但是然后proxyselectizeGroupServer的組合有點棘手。

library(shiny)
library(shinyWidgets)
library(dplyr)
#> 
#> Attache Paket: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
data("mpg", package = "ggplot2")

ui <- fluidPage(
  fluidRow(
    column(
      width = 10, offset = 1,
      tags$h3("Filter data with selectize group"),
      panel(
        selectizeGroupUI(
          id = "my-filters",
          params = list(
            manufacturer = list(inputId = "manufacturer", title = "Manufacturer:"),
            model = list(inputId = "model", title = "Model:"),
            trans = list(inputId = "trans", title = "Trans:"),
            class = list(inputId = "class", title = "Class:")
          )
        ),
        status = "primary"
      ),
      DT::dataTableOutput(outputId = "table")
    )
  )
)

server <- function(input, output, session) {
  
  mpgView2 <- reactiveVal(mpg)
  
  observeEvent(input$table_cell_edit, {
    cell <- input$table_cell_edit
    updated_data <- mpgView2()
    updated_data[cell$row, cell$col] <- cell$value
    mpgView2(updated_data)
  })
  
  res_mod <- callModule(
    module = selectizeGroupServer,
    id = "my-filters",
    data = mpgView2,
    vars = c("manufacturer", "model", "trans", "class")
  )
  
  output$table <- DT::renderDataTable({
    req(res_mod())
    DT::datatable(
      res_mod(),
      filter = "none",
      selection = "none",
      style = "bootstrap",
      extensions = c("Scroller", "FixedColumns"),
      options = list(
        dom = 't',
        scrollY = 500,
        scrollX = 400,
        scroller = TRUE,
        defRender = TRUE,
        autoWidth = TRUE,
        targets = "no-sort",
        bSort = FALSE,
        order = c(),
        fixedColumns = list(leftColumns = 2),
        columnDefs = list(
          list(
            visible = FALSE,
            targets = c(0)
          ),
          list(
            width = "50px",
            targets = "_all"
          )
        )
      ),
      editable = list(
        target = 'cell',
        disable = list(columns = c(0,1,2))
      )
    ) %>% 
      DT::formatRound(
        columns = c(3)
      )
  })
}

shinyApp(ui, server)

reprex 包(v0.3.0) 於 2020 年 8 月 26 日創建

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM