2014-10-29 22 views
3

我有一個閃亮的應用程序shinyTable。它是可編輯的,但由於應用程序中的其他位置的submitButton,只有按下按鈕後纔會保存編輯。如果進行了多個更改並按下該按鈕,則只保存最後的更改。與shinyTable和submitButton編輯表

我的問題是我怎樣才能保存所有已做的更改? 也許有一種方法可以獲得UI中整個表格的內容,這樣我就可以解決問題了嗎? 或者我會更好使用shinysky或其他東西?

下面是一個基於軟件包示例的可重複的示例。您會看到如果您對上表進行了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) 

謝謝你的時間。 Andy

+0

也許你可以使用Actionbutton來代替? – 2014-10-29 17:38:14

+0

謝謝@pops。我確實嘗試了actionButton,但它在我的應用程序中的其他地方搞砸了。 – Andy 2014-10-29 18:50:02

回答

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)