添加多个复选框到Shiny数据表格中,并获取它们的值。

huangapple go评论104阅读模式
英文:

Add multiple checkboxes in shiny datatable and retrieve it's value

问题

I added multiple checkbox columns (check1, check2) to the datatable. I have corresponding table with bool1 and bool2 that I want to update based on the reactives for the checkboxes. I am able to retrieve the data for check1 only. I need to update the callback function to take in both check1 and check2 columns but I am not too familiar with JS. Need help figuring this out.

英文:

I added multiple checkbox columns (check1, check2) to the datatable. I have corresponding table with bool1 and bool2 that I want to update based on the reactives for the checkboxes. I am able to retrieve the data for check1 only. I need to update the callback function to take in both check1 and check2 columns but I am not too familiar with JS. Need help figuring this out.

  1. library(shiny)
  2. library(DT)
  3. ui <- fluidPage(
  4. br(),
  5. fluidRow(
  6. column(
  7. 6,
  8. DTOutput("dtable")
  9. ),
  10. column(
  11. 6,
  12. verbatimTextOutput("reactiveDF")
  13. )
  14. )
  15. )
  16. shinyInput <- function(FUN, len, id, ...) {
  17. inputs <- character(len)
  18. for (i in seq_len(len)) {
  19. inputs[i] <- as.character(FUN(paste0(id, i), label = NULL, ...))
  20. }
  21. inputs
  22. }
  23. dat0 <- data.frame(
  24. fruit = c("apple", "cherry", "pineapple", "pear"),
  25. letter = c("a", "b", "c", "d")
  26. )
  27. dat1 <- cbind(dat0, bool1 = FALSE, bool2 = FALSE)
  28. dat2 <- cbind(
  29. dat0,
  30. check1 = shinyInput(checkboxInput, nrow(dat0), "checkb"),
  31. check2 = shinyInput(checkboxInput, nrow(dat0), "checkc")
  32. )
  33. js <- c(
  34. "$('[id^=checkb]').on('click', function(){",
  35. " var id = this.getAttribute('id');",
  36. " var i = parseInt(/checkb(\\d+)/.exec(id)[1]);",
  37. " var value = $(this).prop('checked');",
  38. " var info = [{row: i, col: 3, value: value}];",
  39. " Shiny.setInputValue('dtable_cell_edit:DT.cellInfo', info);",
  40. "})"
  41. )
  42. server <- function(input, output, session) {
  43. Dat <- reactiveVal(dat1)
  44. output[["dtable"]] <- renderDT({
  45. datatable(
  46. dat2,
  47. rownames = TRUE,
  48. escape = FALSE,
  49. editable = list(target = "cell", disable = list(columns = c(3,4))),
  50. selection = "none",
  51. callback = JS(js)
  52. )
  53. }, server = FALSE)
  54. observeEvent(input[["dtable_cell_edit"]], {
  55. info <- input[["dtable_cell_edit"]] # this input contains the info of the edit
  56. print(info)
  57. Dat(editData(Dat(), info))
  58. })
  59. output[["reactiveDF"]] <- renderPrint({
  60. Dat()
  61. })
  62. }
  63. shinyApp(ui, server)

答案1

得分: 1

这是您提供的代码的翻译部分:

  1. 这是您提供的代码的翻译部分:
  2. library(shiny)
  3. library(DT)
  4. ui <- fluidPage(
  5. br(),
  6. fluidRow(
  7. column(
  8. 6,
  9. DTOutput("dtable")
  10. ),
  11. column(
  12. 6,
  13. verbatimTextOutput("reactiveDF")
  14. )
  15. )
  16. )
  17. checkboxColumn <- function(len, col, ...) { # `col` is the column index
  18. inputs <- character(len)
  19. for(i in seq_len(len)) {
  20. inputs[i] <- as.character(
  21. checkboxInput(paste0("checkb_", col, "_", i), label = NULL, ...)
  22. )
  23. }
  24. inputs
  25. }
  26. dat0 <- data.frame(
  27. fruit = c("apple", "cherry", "pineapple", "pear"),
  28. letter = c("a", "b", "c", "d")
  29. )
  30. dat1 <- cbind(dat0, bool1 = FALSE, bool2 = FALSE)
  31. dat2 <- cbind(
  32. dat0,
  33. check1 = checkboxColumn(nrow(dat0), 3),
  34. check2 = checkboxColumn(nrow(dat0), 4)
  35. )
  36. js <- function(dtid, cols, ns = identity) {
  37. code <- vector("list", length(cols))
  38. for(i in seq_along(cols)) {
  39. col <- cols[i]
  40. code[[i]] <- c(
  41. sprintf(
  42. "$('body').on('click', '[id^=checkb_%d_]', function() {",
  43. col),
  44. " var id = this.getAttribute('id');",
  45. sprintf(
  46. " var i = parseInt(/checkb_%d_(\\d+)/.exec(id)[1]);",
  47. col),
  48. " var value = $(this).prop('checked');",
  49. sprintf(
  50. " var info = [{row: i, col: %d, value: value}];",
  51. col),
  52. sprintf(
  53. " Shiny.setInputValue('%s', info);",
  54. ns(sprintf("%s_cell_edit:DT.cellInfo", dtid))
  55. ),
  56. "});"
  57. )
  58. }
  59. do.call(c, code)
  60. }
  61. checkboxesColumns <- c(3, 4)
  62. server <- function(input, output, session) {
  63. Dat <- reactiveVal(dat1)
  64. output[["dtable"]] <- renderDT({
  65. datatable(
  66. dat2,
  67. rownames = TRUE,
  68. escape = FALSE,
  69. editable = list(
  70. target = "cell", disable = list(columns = checkboxesColumns)
  71. ),
  72. selection = "none",
  73. callback = JS(js("dtable", checkboxesColumns))
  74. )
  75. }, server = FALSE)
  76. observeEvent(input[["dtable_cell_edit"]], {
  77. info <- input[["dtable_cell_edit"]] # this input contains the info of the edit
  78. Dat(editData(Dat(), info))
  79. })
  80. output[["reactiveDF"]] <- renderPrint({
  81. Dat()
  82. })
  83. }
  84. shinyApp(ui, server)

希望这对您有所帮助。如果您需要更多翻译或有其他问题,请随时告诉我。

英文:

Here we go. I added an argument ns in case if you want to use this stuff in a Shiny module. If you don't use a module, keep the default value (identity).

  1. library(shiny)
  2. library(DT)
  3. ui &lt;- fluidPage(
  4. br(),
  5. fluidRow(
  6. column(
  7. 6,
  8. DTOutput(&quot;dtable&quot;)
  9. ),
  10. column(
  11. 6,
  12. verbatimTextOutput(&quot;reactiveDF&quot;)
  13. )
  14. )
  15. )
  16. checkboxColumn &lt;- function(len, col, ...) { # `col` is the column index
  17. inputs &lt;- character(len)
  18. for(i in seq_len(len)) {
  19. inputs[i] &lt;- as.character(
  20. checkboxInput(paste0(&quot;checkb_&quot;, col, &quot;_&quot;, i), label = NULL, ...)
  21. )
  22. }
  23. inputs
  24. }
  25. dat0 &lt;- data.frame(
  26. fruit = c(&quot;apple&quot;, &quot;cherry&quot;, &quot;pineapple&quot;, &quot;pear&quot;),
  27. letter = c(&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;)
  28. )
  29. dat1 &lt;- cbind(dat0, bool1 = FALSE, bool2 = FALSE)
  30. dat2 &lt;- cbind(
  31. dat0,
  32. check1 = checkboxColumn(nrow(dat0), 3),
  33. check2 = checkboxColumn(nrow(dat0), 4)
  34. )
  35. js &lt;- function(dtid, cols, ns = identity) {
  36. code &lt;- vector(&quot;list&quot;, length(cols))
  37. for(i in seq_along(cols)) {
  38. col &lt;- cols[i]
  39. code[[i]] &lt;- c(
  40. sprintf(
  41. &quot;$(&#39;body&#39;).on(&#39;click&#39;, &#39;[id^=checkb_%d_]&#39;, function() {&quot;,
  42. col),
  43. &quot; var id = this.getAttribute(&#39;id&#39;);&quot;,
  44. sprintf(
  45. &quot; var i = parseInt(/checkb_%d_(\\d+)/.exec(id)[1]);&quot;,
  46. col),
  47. &quot; var value = $(this).prop(&#39;checked&#39;);&quot;,
  48. sprintf(
  49. &quot; var info = [{row: i, col: %d, value: value}];&quot;,
  50. col),
  51. sprintf(
  52. &quot; Shiny.setInputValue(&#39;%s&#39;, info);&quot;,
  53. ns(sprintf(&quot;%s_cell_edit:DT.cellInfo&quot;, dtid))
  54. ),
  55. &quot;});&quot;
  56. )
  57. }
  58. do.call(c, code)
  59. }
  60. checkboxesColumns &lt;- c(3, 4)
  61. server &lt;- function(input, output, session) {
  62. Dat &lt;- reactiveVal(dat1)
  63. output[[&quot;dtable&quot;]] &lt;- renderDT({
  64. datatable(
  65. dat2,
  66. rownames = TRUE,
  67. escape = FALSE,
  68. editable = list(
  69. target = &quot;cell&quot;, disable = list(columns = checkboxesColumns)
  70. ),
  71. selection = &quot;none&quot;,
  72. callback = JS(js(&quot;dtable&quot;, checkboxesColumns))
  73. )
  74. }, server = FALSE)
  75. observeEvent(input[[&quot;dtable_cell_edit&quot;]], {
  76. info &lt;- input[[&quot;dtable_cell_edit&quot;]] # this input contains the info of the edit
  77. Dat(editData(Dat(), info))
  78. })
  79. output[[&quot;reactiveDF&quot;]] &lt;- renderPrint({
  80. Dat()
  81. })
  82. }
  83. shinyApp(ui, server)

huangapple
  • 本文由 发表于 2023年5月25日 22:32:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76333449.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定