【发布时间】:2020-12-15 13:44:36
【问题描述】:
根据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
感谢您的帮助。谢谢。
【问题讨论】: