【问题标题】:R Shiny update DT cell based on reactivity within same tableR Shiny 基于同一个表中的反应性更新 DT 单元
【发布时间】:2021-11-09 10:04:01
【问题描述】:

我正在尝试更新数据表对象(在 Shiny 内)中的单元格,具体取决于在同一个表的不同列中所做的选择。 Shiny 输入 ID 已注册,并且可以访问其值。但是,使用其代理替换 DT 中的数据似乎不起作用(ANIMAL 列应根据 GROUP 列更改)。 任何人都知道如何实现这一目标?

library(shiny)
library(DT)

# JS function to render selectize input in DT object
js <- c(
  "function(settings){",
  "  $('#selectNR').selectize()",
  "}"
)

ui <- fluidPage(
  h4("Reactivity within table: no response."),
  fluidRow(
    DTOutput(outputId = "tableNR"),
    verbatimTextOutput('selectedGroupNR')
  )
)

server <- function(input, output, session) {
  getData <- reactive({
    data.frame(
      GROUP = '<select id="selectNR" class="form-control">
                       <option value="A" selected>A</option>
                       <option value="B">B</option>
                       <option value="C">C</option>
                    </select>',
      ANIMAL = 'Dog',
      stringsAsFactors = FALSE, check.names = FALSE)
  })
  
  output$tableNR <- renderDT({
    datatable(data = isolate(getData()),
              selection = "none",
              escape = FALSE,
              rownames = TRUE,
              options =
                list(dom = 't',
                     initComplete = JS(js),
                     preDrawCallback = JS('function() { Shiny.unbindAll(this.api().table().node()); }'),
                     drawCallback = JS('function() { Shiny.bindAll(this.api().table().node()); } ')
                ))
  })
  # Set up proxy to update table when new selection is done.
  proxyNR <- dataTableProxy('tableNR')
  observeEvent(input$selectNR, {
    toReplace <- getData()
    toReplace$ANIMAL <- switch(input$selectNR,
                               'A' = 'Dog',
                               'B' = 'Cat',
                               'C' = 'Fish')
    DT::replaceData(proxyNR, toReplace, resetPaging = TRUE)
  })
  # As a check
  output$selectedGroupNR <- renderPrint({
    paste0('Selected group = ', input$selectNR)
  })
}
shinyApp(ui = ui, server = server)

【问题讨论】:

  • 那行不通。首先,找不到.selectize,因为您的应用程序中没有选择输入。其次,替换数据时,不替换选择器,会重置为A。

标签: r shiny dt


【解决方案1】:

这里有一个解决方案。您还必须更新&lt;select&gt;

library(shiny)
library(DT)

ui <- fluidPage(
  h4("Reactivity within table: no response."),
  fluidRow(
    DTOutput(outputId = "tableNR"),
    verbatimTextOutput('selectedGroupNR')
  )
)

group <- function(letter){
  paste0(
    '<select id="selectNR" class="form-control">',
    ifelse(letter == "A", 
           '<option value="A" selected>A</option>',
           '<option value="A">A</option>'),
    ifelse(letter == "B", 
           '<option value="B" selected>B</option>',
           '<option value="B">B</option>'),
    ifelse(letter == "C", 
           '<option value="C" selected>C</option>',
           '<option value="C">C</option>'),
    '</select>'
  )
}

server <- function(input, output, session) {
  getData <- reactive({
    data.frame(
      GROUP = '<select id="selectNR" class="form-control">
                       <option value="A">A</option>
                       <option value="B">B</option>
                       <option value="C">C</option>
                    </select>',
      ANIMAL = 'Dog',
      stringsAsFactors = FALSE, check.names = FALSE)
  })
  
  output$tableNR <- renderDT({
    datatable(data = isolate(getData()),
              selection = "none",
              escape = FALSE,
              rownames = TRUE,
              options =
                list(dom = 't',
                     preDrawCallback = JS('function() { Shiny.unbindAll(this.api().table().node()); }'),
                     drawCallback = JS('function() { Shiny.bindAll(this.api().table().node()); } ')
                ))
  })
  # Set up proxy to update table when new selection is done.
  proxyNR <- dataTableProxy('tableNR')
  observeEvent(input$selectNR, {
    toReplace <- getData()
    toReplace$GROUP <- group(input$selectNR)
    toReplace$ANIMAL <- switch(input$selectNR,
                               'A' = 'Dog',
                               'B' = 'Cat',
                               'C' = 'Fish')
    replaceData(proxyNR, toReplace, resetPaging = FALSE)
  })
  # As a check
  output$selectedGroupNR <- renderPrint({
    paste0('Selected group = ', input$selectNR)
  })
}
shinyApp(ui = ui, server = server)

【讨论】:

    猜你喜欢
    • 2023-03-28
    • 2018-11-01
    • 2018-04-13
    • 2020-06-26
    • 2020-04-23
    • 1970-01-01
    • 1970-01-01
    • 2020-12-20
    • 2020-06-24
    相关资源
    最近更新 更多