R语言 DT可编辑=“色谱柱”不起作用

uqjltbpv  于 2023-02-20  发布在  其他
关注(0)|答案(1)|浏览(153)

https://yihui.shinyapps.io/DT-edit/上的例子中,当editable = "column"时,结果将不会呈现并保留在表中。实际上,它们仅在editable = TRUEeditable = "cell"时有效。
从上面的链接中挑选出以下代码,我添加了一个Click按钮来查看input元素中的内容。
在使用editable = "column"(允许输入到列单元格)选择单元格后使用Click时,input$x1_columns_selected中未显示任何变化。
有没有人能具体说明为什么以及如果这曾经正确工作?我看不出像这样的功能将如何被打破,它似乎非常有用。

library(shiny)
library(DT)

dt_output = function(title, id) {
  fluidRow(column(
    12, h1(paste0('Table ', sub('.*?([0-9]+)$', '\\1', id), ': ', title)),
    hr(),
    actionButton("click_action", "Click"),
    hr(),
    DTOutput(id)
  ))
}
render_dt = function(data, editable = 'cell', server = TRUE, ...) {
  renderDT(data, selection = 'none', server = server, editable = editable, ...)
}

shinyApp(
  ui = fluidPage(
    title = 'Double-click to edit table cells',

    dt_output('client-side processing (editable = "column")', 'x1'),
  ),

  server = function(input, output, session) {
    d1 = iris[1:5,]
    d1$Date = Sys.time() + seq_len(nrow(d1))

    # client-side processing

    output$x1 = render_dt(d1, 'column', FALSE)

    observe(str(input$x1_cell_edit))

    observeEvent(input$click_action, {
      print(input)
      print(input$x1_cells_selected)
      print(input$x1_columns_selected)
      print(input$x1_rows_all)
      print(input$x1_rows_current)
      print(input$x1_rows_selected)
      print(input$x1_search)
      #print(input$x1_state)
    })
  }
)
ih99xse1

ih99xse11#

编辑列

事实证明,这确实有效;只不过在单元格外单击并不足以注册编辑,但按Ctrl + Enter键有效。(我从this page of examples上表2的鼠标悬停文本中学到了这一点。)因此,例如,假设您编辑Sepal.Width并将第一个值设置为7:

按下Ctrl + Enter后,您的应用将显示以下输出:

NULL
'data.frame':   5 obs. of  3 variables:
 $ row  : int  1 2 3 4 5
 $ col  : int  2 2 2 2 2
 $ value: chr  "7" "3" "3.2" "3.1" ...

这意味着第2列的第1-5行被编辑,它们的值被设置为7(更改的值)、3(与之前相同)、3.2等。

选定列

存储在input$x1_columns_selected中的值与编辑列无关。相反,如果表设置了selection = list(target = 'column')选项,则当用户单击它时可以选择整个列。Click按钮将打印所选列的索引。下面是设置了该选项的应用程序示例(并且Click按钮的输出仅限于所选列):

library(shiny)
library(DT)

dt_output = function(title, id) {
  fluidRow(column(
    12, h1(paste0('Table ', sub('.*?([0-9]+)$', '\\1', id), ': ', title)),
    hr(),
    actionButton("click_action", "Click"),
    hr(),
    DTOutput(id)
  ))
}
render_dt = function(data, editable = 'cell', server = TRUE, ...) {
  renderDT(data, selection = list(target = 'column'), server = server,
           editable = editable, ...)
}

shinyApp(
  ui = fluidPage(
    title = 'Double-click to edit table cells',
    
    dt_output('client-side processing (editable = "column")', 'x1'),
  ),
  
  server = function(input, output, session) {
    d1 = iris[1:5,]
    d1$Date = Sys.time() + seq_len(nrow(d1))
    
    # client-side processing
    
    output$x1 = render_dt(d1, 'column', FALSE)
    
    observe(str(input$x1_cell_edit))
    
    observeEvent(input$click_action, {
      print(input$x1_columns_selected)
    })
  }
)

如果单击Sepal.Width列中的任意位置,将看到以下内容:

然后,如果您单击该按钮,就会得到以下输出:

NULL
[1] 2

这意味着选择了列2。

相关问题