r语言 - 闪亮的应用程序不会像我期望的那样增加变量



我不明白为什么current_row变量在按下上一个和下一个按钮之间重置为其初始值。

current_row = 0
shinyApp(
ui = fluidPage(
actionButton("next_button", "next"),
actionButton("previous_button", "previous")
),
server = function(input, output, session) {
observeEvent(input$next_button, 
{
current_row = current_row + 1 
print (current_row)
}) 
observeEvent(input$previous_button, 
{
current_row = current_row - 1 
print (current_row)
}) 
}
)

observeEvent中的current_row = current_row + 1实际上并没有更新全局环境中的current_row值。你需要<<-.但是,在shiny中使用它是非常不明智的。考虑问一个关于你到底需要什么的单独问题,以便人们可以为你指出正确的方向。

current_row = 0
shinyApp(
ui = fluidPage(
actionButton("next_button", "next"),
actionButton("previous_button", "previous")
),
server = function(input, output, session) {
observeEvent(input$next_button, 
{
current_row <<- current_row + 1 
print (current_row)
}) 
observeEvent(input$previous_button, 
{
current_row <<- current_row - 1 
print (current_row)
}) 
}
)

最新更新