R Shiny DT-使用电抗编辑表中的值 [英] R Shiny DT - edit values in table with reactive

查看:69
本文介绍了R Shiny DT-使用电抗编辑表中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过编辑DT :: DataTable更新反应性数据源?下面的代码基于此代码,其中将x设为无功。当尝试在observeEvent中更改x时,问题就开始了。

Is it possible to update a reactive data source by editing the DT::DataTable? Below code is based on this code with change that x is made reactive. The problem starts when trying to change x in observeEvent.

让x反应的目的是我打算从外部数据库中获取它,然后对DT进行: DataTable写回数据库,以便与用户看到的内容保持同步(这样做很好(这不是问题的一部分)。

The purpose of having x reactive is that I intend to source it from an external database, then have edits to the DT::DataTable write back to the database so that it stays in sync with what the user sees (I'm fine with doing that - it is not part of the question).

library(shiny)
library(DT)
shinyApp(
  ui = fluidPage(
    DTOutput('x1')
  ),
  server = function(input, output, session) {
    x = reactive({
      df <- iris
      df$Date = Sys.time() + seq_len(nrow(df))
      df
    })
    output$x1 = renderDT(x(), selection = 'none', editable = TRUE)

    proxy = dataTableProxy('x1')

    observeEvent(input$x1_cell_edit, {
      info = input$x1_cell_edit
      str(info)
      i = info$row
      j = info$col
      v = info$value

      # problem starts here
      x()[i, j] <<- isolate(DT::coerceValue(v, x()[i, j])) 
      replaceData(proxy, x(), resetPaging = FALSE)  # important
    })
  }
)


推荐答案

我不确定我是否理解正确,但是也许此解决方案可能会对您有所帮助。我将您的反应式更改为reactValues对象,并删除了replaceData行。

I am not sure if I understand you correctly, but maybe this solution might help you a bit. I changed your reactive into a reactiveValues object and I removed the replaceData line.

library(shiny)
library(DT)
shinyApp(
  ui = fluidPage(
    DTOutput('x1'),
    verbatimTextOutput("print")
  ),
  server = function(input, output, session) {
    x = reactiveValues(df = NULL)

    observe({
      df <- iris
      df$Date = Sys.time() + seq_len(nrow(df))
      x$df <- df
    })

    output$x1 = renderDT(x$df, selection = 'none', editable = TRUE)

    proxy = dataTableProxy('x1')

    observeEvent(input$x1_cell_edit, {
      info = input$x1_cell_edit
      str(info)
      i = info$row
      j = info$col
      v = info$value

      # problem starts here
      x$df[i, j] <- isolate(DT::coerceValue(v, x$df[i, j]))
    })

    output$print <- renderPrint({
      x$df
    })
  }
)

这篇关于R Shiny DT-使用电抗编辑表中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆