我发现了一个关于更新数据输出的问题。脚本如下:
library(shiny)
library(shinyapps)
library(rhandsontable)
## CLIENT SIDE
ui <- shinyUI(pageWithSidebar(
headerPanel("Test of rHandsontable output - hot_to_r function"),
sidebarPanel(
actionButton("goButton", "test rhandsontable")
),
mainPanel(
rHandsontableOutput("all_updates")
,verbatimTextOutput('df_output')
)
))
## SERVER SIDE
server <- shinyServer(function(input, output) {
observeEvent(input$goButton,{
df <- data.frame ( x=1:10
,y=1:10
,z=factor(c("X","P"))
,merge_xy="merger"
)
df[,4] <- paste(df[,1],"+",df[,2])
output$all_updates <- renderRHandsontable({
rhandsontable(df, selectCallback = TRUE, height = 300) %>%
hot_table(highlightCol = TRUE, highlightRow = TRUE) %>%
hot_rows(rowHeights = 18)
})
})
observeEvent(input$all_updates,{
zzz <- hot_to_r(input$all_updates)
zzz[,4] <- paste(zzz[,1],'+',zzz[,2],'+',zzz[,3])
#the output of the modified dataframe
print(zzz)
output$all_updates <-renderRHandsontable({
rhandsontable(zzz, selectCallback = TRUE, height = 300) %>%
hot_table(highlightCol = TRUE, highlightRow = TRUE) %>%
hot_rows(rowHeights = 18)
})
print(hot_to_r(input$all_updates))
output$df_output <- renderPrint(
print(hot_to_r(input$all_updates))
)
})
output$all_updates <- renderRHandsontable({
df <- data.frame ( x=integer(10)
,y=integer(10)
,z=factor(c("X","P"))
,merge_xy="merger"
)
rhandsontable(df, selectCallback = TRUE, height = 300) %>%
hot_table(highlightCol = TRUE, highlightRow = TRUE) %>%
hot_rows(rowHeights = 18)
})
})
shinyApp(ui=ui,server=server)
问题是函数 'hot_to_r' 从 rHandsontable 返回数据集,有一步延迟。
您可以尝试对 Z 列进行更改:
在列发生任何更改后,表的输出已立即更新,但“hot_to_r”函数的输出会生成表的先前版本的快照(没有最新更改)。