gpt4 book ai didi

r - 是否可以在 R Shiny 中动态设置小部件选项?

转载 作者:行者123 更新时间:2023-12-04 11:25:26 25 4
gpt4 key购买 nike

作为一个最小的工作示例,

# An app with a datatable whose pagination option we can toggle with a button
library(shiny)
runApp(list(
ui = basicPage(
actionButton("button", label = "Toggle paginate"),
dataTableOutput("table")
),
server = function(input, output) {
output$table <- renderDataTable(
data.frame(x = 1:100, y = input$button), # Some dummy data
options = list(
bPaginate = as.logical(input$button %% 2)
)
)
}
))

对应的浏览器输出:

enter image description here

我应该能够通过单击“切换分页”按钮动态地打开和关闭选项 bPaginate

但是,这不起作用 - 表格从不分页。出了什么问题?

最佳答案

我还为数据表小部件而苦恼。 renderDataTable 也不是 dt::RenderDataTable 在选项作为简单列表中给出时似乎可以识别并重新渲染输出。

Yihui 的答案适用于基本的 Shiny renderDataTable,但我永远不会猜测这是一个解决方案,而且我不明白为什么一个函数会起作用而一个普通列表不会。我认为这是底层 Javascript 的一些内部实现问题,但这似乎是一个不应泄漏到 R api 中的实现细节。

但是,我无法让易辉的答案适用于 DataTable 包的 DT 版本。也许这只是我的错误,但我最终想出了另一种方法来让用户输入影响表选项而不使用选项函数。这样做需要一个额外的(响应式(Reactive))变量层并使用 datatable() 函数。 (请注意,这全部基于使用不同选项名称的最新 DataTable 版本,并且我使用的 ui 与上述问题略有不同。这些都不应该改 rebase 本问题或其解决方案。)

# An app with a datatable whose pagination option we can toggle with a button
library(shiny)
runApp(list(
ui = fluidPage(
sidebarLayout(
sidebarPanel(
checkboxInput("paging", "Paginate table")
),
mainPanel(
DT::dataTableOutput("table")
)
)
),
server = function(input, output) {
# Create a new table when input$paging changes
makeTable <- reactive({
DT::datatable(
{ data.frame(x= 1:100, y = input$paging)}, # Some dummy data
options= list(
paging = input$paging
)
)
})
# Render the datatable returned by the reactive function
output$table <- DT::renderDataTable( makeTable() )
}
))

关于r - 是否可以在 R Shiny 中动态设置小部件选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23334607/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com