【发布时间】:2018-08-14 05:18:34
【问题描述】:
我有以下示例应用程序(下面的代码)。这个想法是,如果用户选择第二列(索引为 1),则选择转到第三列(索引为 2),因为我不希望用户能够选择第二列(如据我所知,没有内置方法可以阻止用户选择 DT 中的特定列)。
问题在于selectRows(tableProxy, c(2) 有效(行选择的一个简单示例),selectColumns(tableProxy, c(2)) 仅取消选择当前选择的列,而不选择第三列。
我的语法有问题,还是这是一个错误?如果是错误,是否有解决方法?
可重现的例子:
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
fluidRow(
tags$div(title = "Iris table",
DT::dataTableOutput("irisTable"))
)
)
# Define server logic required to draw a table
server <- function(input, output) {
output$irisTable <- DT::renderDT(datatable(head(iris, 20), options = list(paging = FALSE, searching = FALSE),
rownames = FALSE,
selection = list(target = 'row+column', mode='single', selected = list(rows = c(NULL), cols = c(2)))
) %>%
formatStyle(0, target= 'row',color = 'black',
lineHeight='70%', padding = '3px 3px', fontSize = '80%')
)
tableProxy <- dataTableProxy("irisTable")
observeEvent(input$irisTable_columns_selected, {
if (input$irisTable_columns_selected == 1) {
#tableProxy %>% selectColumns(2)
selectRows(tableProxy, c(2))
selectColumns(tableProxy, c(2))
}
})
}
# Run the application
shinyApp(ui = ui, server = server)
更新:我已经使用发布的示例代码here(转换为使用单个文件 Shiny 应用程序)尝试了上述方法,我也遇到了同样的问题。我已经重新安装了 DT 包,它并没有解决问题。
【问题讨论】: