【发布时间】:2016-05-01 13:40:49
【问题描述】:
我有一个简单的闪亮应用程序,它会在文本框中呈现用户选择的任何行。我希望一次只选择一行,因此我将DT 选项设置为selection = 'single',当单击不同的行时,它会正确取消选择表中的任何行。
然而,在文本框中,我通过单击不同的行而取消选择的行的名称被保留,并且新名称被附加在先前选择的行的名称之后。
我注意到事实上我可以通过双击从文本框中删除任何先前选择的行(在表格中视觉上没有影响的东西)。
在我的实际应用中,我有一个绘图函数,它只接受表格中的一个值,所以我需要找到一种方法通过input$x_rows_selected 发送一个且只有一个值。
library(shiny)
library(DT)
# Define UI for application that draws a histogram
ui <- shinyUI(fluidPage(
h1('A Server-side Table'),
fluidRow(
column(9, DT::dataTableOutput('x3')),
column(3, verbatimTextOutput('x4'))
)
))
# Define server logic required to draw a histogram
server <- shinyServer(function(input, output, session) {
# server-side processing
mtcars2 = mtcars[, 1:8]
output$x3 = DT::renderDataTable(mtcars2, server = TRUE, selection = 'single')
# print the selected indices
output$x4 = renderPrint({
s = input$x3_rows_selected
if (length(s)) {
cat('These rows were selected:\n\n')
cat(s, sep = ', ')
}
})
})
# Run the application
shinyApp(ui = ui, server = server)
【问题讨论】:
-
我知道快速修复实际上可能是
s <- s[length(s)],但我更愿意了解如何控制input$x_rows_selected