我希望删除所有/无复选框,从一个可实现的表使用在一个闪亮的应用程序。对于一个普通的R脚本,建议给出一个这里的答案。
但是,此解决方案在以下方面失败:
renderWidget(实例)中的警告:忽略预先添加的内容;prependContent不能用于闪亮的呈现调用
下面的代码无法删除上面提到的错误复选框。那么,如何在一个闪亮的应用程序中删除Reactable表的all/none复选框。
library(reactable)
library(htmlwidgets)
javascript <- JS('
document.querySelector(\'.rt-select-input[aria-label="Select all rows"]\').parentElement.parentElement.style.display="none";
')
ui <- fluidPage(reactableOutput("table")
)
server <- function(input, output, session) {
output$table <- renderReactable({
e <- reactable(iris,
onClick = "select",
selection = "multiple")
(p <- prependContent(e, onStaticRenderComplete(javascript)))
})
}
shinyApp(ui, server) 发布于 2020-09-21 02:01:12
这可以通过使用闪亮的javascript事件和shinyjs包进行巧妙的变通来实现。
js.code定义将向shiny:visualchange事件添加事件处理程序的js函数。useShinyjs()使用shinyjs包。extendShinyjs定义js函数以便使用。js$hideSelectAll("table")将事件处理程序添加到表中。delay(100, runjs('$( "#table" ).trigger( "shiny:visualchange" );'))延迟对事件处理程序的调用,以便允许表刷新。注:
我尝试过shiny:value事件,但它没有工作,应该在呈现输出组件时执行它,但遗憾的是,它没有。
library(reactable)
library(shiny)
library(shinyjs)
# you'll need to pass the id that you provided to reactableOutput(id)
js.code <- '
shinyjs.hideSelectAll = function(id){
$("#"+id).on("shiny:visualchange", function({currentTarget}) {
currentTarget = currentTarget.querySelector(".rt-select-input[aria-label=\'Select all rows\']")
if(currentTarget) currentTarget.parentElement.parentElement.style.display="none";
});
}
'
ui <- fluidPage(reactableOutput("table"),
useShinyjs(debug=TRUE),
extendShinyjs(text = js.code, functions = c("hideSelectAll"))
)
server <- function(input, output, session) {
output$table <- renderReactable({
reactable(iris,
onClick = "select",
selection = "multiple")
})
js$hideSelectAll("table")
delay(100, runjs('$( "#table" ).trigger( "shiny:visualchange" );'))
#runjs('$( "#table" ).trigger( "shiny:visualchange" );')
}
shinyApp(ui, server) 如果你想用一次
library(reactable)
library(shiny)
library(shinyjs)
js.code <- '
document.querySelector(\'.rt-select-input[aria-label="Select all rows"]\').parentElement.parentElement.style.display="none";
'
ui <- fluidPage(reactableOutput("table"),
useShinyjs(debug=TRUE)
)
server <- function(input, output, session) {
output$table <- renderReactable({
reactable(iris,
onClick = "select",
selection = "multiple")
})
delay(100, runjs(js.code))
}
shinyApp(ui, server) 只使用没有外部依赖的闪亮的:
即使刷新数据,也不会显示select all按钮。看来,我的第一段代码并不是最优的:(但我把它作为参考。
ui <- fluidPage(reactableOutput("table"),
actionButton("button", "refresh"),
tags$head(tags$script(HTML('
setTimeout(()=>{
document.querySelector(\'#table .rt-select-input[aria-label="Select all rows"]\').parentElement.parentElement.style.display="none";
}, 200)
')))
)
server <- function(input, output, session) {
output$table <- renderReactable({
reactable(iris,
onClick = "select",
selection = "multiple")
})
observeEvent(input$button, {
output$table <- renderReactable({
reactable(mtcars,
onClick = "select",
selection = "multiple")
})
})
}
shinyApp(ui, server) https://stackoverflow.com/questions/63984876
复制相似问题