我有一个在巨大函数中创建的动态界面。该函数会输出一个包含所有输入功能和其他内容的标签列表。其中有一个DT表。这一切都很好并且工作正常。现在我希望能够替换DT中的replaceData(),但是在创建标签列表时我可以将一个elementID分配给datatable(),但renderDT会覆盖它。
如果我省略 renderDT(),表格仍会显示,但 ReplaceData() 会失败并显示
DataTables 警告:表 id=DataTables_Table_0 - 无效的 JSON 响应。 有关此错误的更多信息,请参阅 http://datatables.net/tn/1
错误信息。
当前工作但非常糟糕的解决方案:如果我有renderDT(),则会创建一些以“out”开头的散列ID。这可以在observe()中捕获并用于创建datatableProxy()对象,然后该对象可用于replaceData()。这样做的一个问题是你只能有一张桌子,这很糟糕。
DT git 上已经存在问题:https://github.com/rstudio/DT/issues/567但没有解决方案。
library(shiny)
ui <- fluidPage(
uiOutput("inputs")
)
server <- function(input, output, session) {
output$inputs <- renderUI({
tagList( h1("a table has no id")
,renderDT(datatable(mtcars,elementId = "thisDoesHaveAnID"))
,actionButton("replaceDataGo","Replace data go!")
)
})
observeEvent(input$replaceDataGo,{
tableid <- gsub("_.*","",names(input)[grep("out.*",names(input))][1])
tableProxy <- dataTableProxy(tableid,session = session)
replaceData(tableProxy,mtcars[1:input$replaceDataGo,])
})
}
shinyApp(ui, server)
是否有任何方法可以将标记列表中已经存在的 ID 应用到此渲染?
只需将其包裹成
div()
:
output$inputs <- renderUI({
tagList(h1("a table has no id"),
div(id = "myID",
renderDT(datatable(mtcars,elementId = "thisDoesHaveAnID"))
),
actionButton("replaceDataGo","Replace data go!")
)
})