繁体   English   中英

如何停止在R-Shiny中重置变量?

[英]How to stop resetting a variable in R-Shiny?

我正在开发一个互动式闪亮应用程序。 要在单击鼠标时显示下一个图,我必须跟踪变量的前一个值。 但是,当我单击绘图时,所有变量都会重置。 您能否建议我一种阻止变量在每次迭代中重置的方法。

例如:

library(shiny)

ui <- shinyUI(fluidPage(
  titlePanel("Title"),
  sidebarLayout(
    sidebarPanel(
      actionButton("Reset", label="Reset Graph")
    ),
    mainPanel(
      plotOutput("graph", width = "100%", click = "plot_click"),
      verbatimTextOutput("click_info")
    )
  )
) 
)

server <- shinyServer(function(input, output, session) { 
level <- "Value1"  
observeEvent(input$Reset,{   
output$graph <- renderPlot({ plot(1, 1) }) }, ignoreNULL = F)
  print(level)
  # interaction click in graph  
  observe({
    if(is.null(input$plot_click$x)) return(NULL)
    x <- sample(20:30,1,F)
    level <- "Value2"
    isolate({
      output$graph <- renderPlot({
        draw.single.venn(x)
      }) 
    })

  })

})
shinyApp(ui=ui,server=server)

我将级别变量更改为“ Value2”。 但是在下一次迭代中,由于第一行代码,它再次变为“ Value1”。 您可以帮助我将其保留为“ Value2”吗?

您可以将其定义为反应值:

server <- shinyServer(function(input, output, session) { 
  level_init <- reactiveValues(level="Value1") 
  level_react <- reactive({
    level_init$level <- "Value2"
  })
  print(isolate(level_init$level))
  observeEvent(input$Reset,{   
    output$graph <- renderPlot({ plot(1, 1) }) }, ignoreNULL = F)
  # interaction click in graph  
  observe({
    if(is.null(input$plot_click$x)) return(NULL)
    x <- sample(20:30,1,F)
    level_react()
    print(level_init$level)
    isolate({
      output$graph <- renderPlot({
        draw.single.venn(x)
      }) 
    })

  })

})
shinyApp(ui=ui,server=server)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM