r - 带有 shinyTable 和 submitButton 的可编辑表格

标签 r shiny

我有一个 shinyTable在 Shiny 的应用程序中。它是可编辑的,但由于应用程序其他地方的 submitButton,在按下按钮之前不会保存编辑。如果进行了多次更改并且按下了按钮,则仅保存最后一次更改。

我的问题是我怎样才能让它保存所做的所有更改? 也许有一种方法可以让我在 UI 中获取整个表格的内容,这样我就可以解决问题了? 或者我会更好地使用 shinysky 或其他东西吗?

下面是一个基于包中示例的可重现示例。您会看到,如果您对上面的表格进行了 2 处更改,然后按下按钮,只有第 2 处更改会复制到下面的表格。

library(shiny)
library(shinyTable)

server <- function(input, output, session) {

  rv <- reactiveValues(cachedTbl = NULL)

  output$tbl <- renderHtable({
    if (is.null(input$tbl)){

      #fill table with 0
      tbl <- matrix(0, nrow=3, ncol=3)

      rv$cachedTbl <<- tbl
      print(tbl)
      return(tbl)
    } else{
      rv$cachedTbl <<- input$tbl
      print(input$tbl)
      return(input$tbl)
    }
  })  

  output$tblNonEdit <- renderTable({
    rv$cachedTbl
  })    
}


ui <- shinyUI(pageWithSidebar(

  headerPanel("Simple Shiny Table!"),

  sidebarPanel(
    helpText(HTML("A simple editable matrix with an update button.
                  Shows that only most recent change is saved. 
                  <p>Created using <a href = \"http://github.com/trestletech/shinyTable\">shinyTable</a>."))
  ),

  # Show the simple table
  mainPanel(
    #editable table
    htable("tbl"),
    #update button
    submitButton("apply table edits"),         
    #to show saved edits
    tableOutput("tblNonEdit")
  )
))

shinyApp(ui = ui, server = server)

感谢您的宝贵时间。 安迪

最佳答案

遵循 RStudio 的 Joe Cheng 关于 related question 的建议,似乎不建议使用 submitButton 并且会导致疼痛。

在这个简单示例和我的应用程序中,切换到 actionButton 和 isolate 相对简单。

下面的解决方案。

library(shiny)
library(shinyTable)

server <- function(input, output, session) {

  rv <- reactiveValues(cachedTbl = NULL)

  output$tbl <- renderHtable({
    if (is.null(input$tbl)){

      #fill table with 0
      tbl <- matrix(0, nrow=3, ncol=3)

      rv$cachedTbl <<- tbl
      return(tbl)
    } else{
      rv$cachedTbl <<- input$tbl
      return(input$tbl)
    }
  })  

  output$tblNonEdit <- renderTable({

    #add dependence on button
    input$actionButtonID

    #isolate the cached table so it only responds when the button is pressed
    isolate({
    rv$cachedTbl
    })
  })    
}


ui <- shinyUI(pageWithSidebar(

  headerPanel("shinyTable with actionButton to apply changes"),

  sidebarPanel(
    helpText(HTML("A simple editable matrix with a functioning update button. 
                   Using actionButton not submitButton. 
                   Make changes to the upper table, press the button and they will appear in the lower. 
                  <p>Created using <a href = \"http://github.com/trestletech/shinyTable\">shinyTable</a>."))
  ),

  # Show the simple table
  mainPanel(
    #editable table
    htable("tbl"),
    #update button
    actionButton("actionButtonID","apply table edits"),
    #to show saved edits
    tableOutput("tblNonEdit")
  )
))

shinyApp(ui = ui, server = server)

关于r - 带有 shinyTable 和 submitButton 的可编辑表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26635887/

相关文章:

authentication - 我使用 yeroon.net/ggplot2 登录 Google 电子表格有多安全

r - R 中的列表(或字典)列表

r - 获得频率表后使用 Ifelse 进行分组 (R)

r - 如何在ggplot2中制作以不同纵横比保留角度的线段?

r - 加速分位数计算

r - 无法将类型 'closure' 强制转换为类型 'character' 的向量

r - 从 SQL 中以 Shiny 的方式存储 react 数据

r - Shiny 的或只是 htmlwidgets

r - 如何在 Shiny 中选择行和列并使用 DT 进行过滤?

r - 在 Shiny 中保存传单 map