可编辑的表与shinyTable和submitButton

时间:2014-10-29 16:33:53

标签: r shiny

我在闪亮的应用中有一个shinyTable。它是可编辑的,但由于应用程序中的其他地方的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)

感谢您的时间。 安迪

1 个答案:

答案 0 :(得分:2)

根据来自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)