在下面的示例中,我有3 DT::datatables
。我希望用户能够从所有这些表中选择不超过一行。我可以使用dataTableProxy
和selectRow
,根据documentation中的“操纵现有DataTables实例”部分。它工作正常。
但是,在我的应用程序中,我有24个(称为值N
)表。如果我尝试将下面的代码调整到我的24个表页面,我会得到可怕数量的代码行。
更聪明的方法是什么?
特别是,我怎么能:
reactiveText()
?)编辑:我在下面的代码中复制了user5029763的答案(见下文)。
DTWrapper <- function(data, pl = 5, preselec = c()){
datatable(data,
options = list(pageLength = pl, dom='t',ordering=F),
selection = list(mode = 'single', selected= preselec),
rownames = FALSE)
}
resetRows <- function(proxies, self){
for (i in 1:length(proxies)){
if (self != i){
proxies[[i]] %>% selectRows(NULL)
}
}
}
lapply(1:3, function(id) {
observe({
rownum <- input[[paste0("tab",id,"_rows_selected")]]
if (length(rownum) > 0) { resetRows(proxyList(), id) }
})
})
server = function(input, output) {
output$tab1 <- DT::renderDataTable(DTWrapper(head(mtcars[,1:3]), input$selectTop))
output$tab2 <- DT::renderDataTable(DTWrapper(head(mtcars[,1:3]), input$selectTop))
output$tab3 <- DT::renderDataTable(DTWrapper(head(mtcars[,1:3]), input$selectTop))
proxyList <- reactive({
proxies = list()
for (i in 1:3){
tableID <- paste("tab", i, sep="")
proxies[[i]] = dataTableProxy(tableID)
}
return(proxies)
})
reactiveText <- reactive({
rownum1 <- input$tab1_rows_selected
rownum2 <- input$tab2_rows_selected
rownum3 <- input$tab3_rows_selected
if (length(rownum1) > 0){return(c(rownum1, 1))}
if (length(rownum2) > 0){return(c(rownum2, 2))}
if (length(rownum3) > 0){return(c(rownum3, 3))}
})
output$txt1 <- renderText({
paste("You selected row ", reactiveText()[1]
, " from table ", reactiveText()[2], ".", sep="")
})
}
shinyApp(
ui = fluidPage(
fluidRow(column(4,DT::dataTableOutput("tab1"))
, column(4,DT::dataTableOutput("tab2"))
, column(4, DT::dataTableOutput("tab3")))
,fluidRow(column(4,textOutput("txt1")))
),
server = server
)
textOutput
是:“您从第Y个表中选择了第X行。”
答案 0 :(得分:1)
您可以尝试modules。另一种方式是$memObj->set('sample_key', 123.0); # in PHP
print(cache.get('sample_key')) # in Django # prints 123
$memObj->set('sample_key', 123); # in PHP
print(cache.get('sample_key')) # in Django # prints None
。
lapply