当UI被移动到server.R代码中时,是否可以使DT :: replaceData()工作?下面的完整代码以及错误消息。
目的是在浏览器页面源中隐藏UI(通过在server.R中放置代码),同时提供用户可以修改并使用DT :: replaceData()更新的DataTable。
这个闪亮的应用程序从using a proxy to manipulate a DT修改了两个更改:
change
按钮,用resetPaging = FALSE
(这些在下面的UI和SERVER的完整代码中标出。) 将UI移动到服务器后,除了替换产生错误的数据外,一切似乎都有效:
我已经按照建议here查看了浏览器开发人员工具,这些工具似乎没有帮助,因为没有JSON对象可见。由于代理上的其他功能起作用,当UI被移动到服务器时,它似乎是replaceData特有的。
ui.R
library(shiny)
fluidPage(
title = 'Manipulate an Existing Table',
# CHANGE 1 - ui.R
uiOutput("ui_body")
)
server.R
library(shiny)
library(DT)
shinyServer(function(input, output, session) {
# CHANGE 1 - server.R (scroll down to see change 2)
output$ui_body <- renderUI({
sidebarLayout(
sidebarPanel(
selectizeInput('rows', 'Row IDs', choices = seq_len(nrow(iris)), multiple = TRUE),
actionButton('select1', 'Select Rows'),
actionButton('clear1', 'Clear Rows'),
numericInput('col', 'Column ID', 1, min = 1, max = ncol(iris), step = 1),
actionButton('select2', 'Select Column'),
actionButton('clear2', 'Clear Columns'),
hr(),
actionButton('add', 'Add Row'),
hr(),
textInput('cap', 'Table Caption'),
hr(),
actionButton('change', 'Change Value')
),
mainPanel(
DT::dataTableOutput('foo'),
verbatimTextOutput('info')
)
)
}) ## End of Change 1, scroll down to see change 2
# using server = FALSE mainly for addRow(); server = TRUE works for
# selectRows() and selectColumns()
output$foo = DT::renderDataTable(
iris, server = FALSE, selection = list(target = 'row+column'),
caption = 'Using a proxy object to manipulate the table',
options = list(
columnDefs = list(list(className = 'dt-center', targets = 5)),
pageLength = 6,
lengthMenu = c(5, 10, 15, 20)
)
)
proxy = dataTableProxy('foo')
observeEvent(input$select1, {
proxy %>% selectRows(as.numeric(input$rows))
})
observeEvent(input$select2, {
proxy %>% selectColumns(input$col)
})
observeEvent(input$clear1, {
proxy %>% selectRows(NULL)
})
observeEvent(input$clear2, {
proxy %>% selectColumns(NULL)
})
observeEvent(input$add, {
proxy %>% addRow(iris[sample(nrow(iris), 1), , drop = FALSE])
})
observe({
if (!is.null(input$cap)){
if (input$cap != '') proxy %>% updateCaption(input$cap)
}
})
# CHANGE 2
observeEvent(input$change, {
df = iris
proxy %>% replaceData(df, resetPaging = FALSE)
}) # End of Change 2
output$info = renderPrint({
list(rows = input$foo_rows_selected, columns = input$foo_columns_selected)
})
})
更新:通过在此处设置server = TRUE找到解决方法:
output$foo = DT::renderDataTable(
iris, server = TRUE, selection = list(target = 'row+column'), ...
虽然这有效,但选择server = FALSE仍然是好的,是否可能?