gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 01:44:11 24 4
gpt4 key购买 nike

我有一个 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com