【问题标题】:editable table with shinyTable and submitButton带有 shinyTable 和 submitButton 的可编辑表
【发布时间】:2014-10-29 16:33:53
【问题描述】:

我在一个闪亮的应用程序中有一个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)

感谢您的宝贵时间。 安迪

【问题讨论】:

  • 也许您可以使用 Actionbutton 代替?
  • 谢谢@pops。我确实尝试过 actionButton,但它弄乱了我应用中其他地方的东西。

标签: r shiny


【解决方案1】:

根据 RStudio 的 Joe Cheng 在 related question 上的建议,似乎不建议使用 submitButton 并且可能会导致痛苦。

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

下面的解决方案。

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-11
    • 2013-01-14
    • 1970-01-01
    • 2016-06-20
    相关资源
    最近更新 更多