具有shinyTable和submitButton的可编辑表

时间:2021-12-06 17:41:23

I have a shinyTable in a shiny app. It is editable, but because of a submitButton elsewhere in the app the edits are not saved until the button is pressed. If more than one change is made and the button is pressed only the last change is saved.

我在一个闪亮的应用中有一个闪亮的桌面。它是可编辑的,但是因为在应用的其他地方有一个提交按钮,编辑直到按钮被按下才会被保存。如果进行了多个更改,并按下按钮,则只保存最后一个更改。

My question is how can I get it to save all the changes that have been made ? Perhaps there is a way that I can get at the contents of the whole table in the UI so I can workaround ? Or would I be better off using shinysky or something else ?

我的问题是,我如何让它保存所做的所有更改?也许有一种方法可以让我在UI中获取整个表的内容,这样我就可以解决这个问题了?或者我用shinysky或者别的什么更好?

Below is a reproducible example based on an example from the package. You'll see that if you make 2 changes to the upper table and then press the button only the 2nd change gets copied to the lower table.

下面是一个基于软件包示例的可复制示例。您将看到,如果您对上表做了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)

Thanks for your time. Andy

谢谢你的时间。安迪

1 个解决方案

#1


2  

Following advice from Joe Cheng at RStudio on a related question, it appears that submitButton is not advised and can cause pain.

根据RStudio的Joe Cheng关于一个相关问题的建议,似乎提交按钮是不被建议的,并且可能导致疼痛。

Switching to actionButton and isolate was relatively straightforward in this simple example and in my application.

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

Solution below.

下面的解决方案。

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)

#1


2  

Following advice from Joe Cheng at RStudio on a related question, it appears that submitButton is not advised and can cause pain.

根据RStudio的Joe Cheng关于一个相关问题的建议,似乎提交按钮是不被建议的,并且可能导致疼痛。

Switching to actionButton and isolate was relatively straightforward in this simple example and in my application.

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

Solution below.

下面的解决方案。

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)