如何防止每次用户界面交互多次重绘闪亮的情节? [英] How to prevent shiny plot from being redrawn multiple times per UI interaction?

查看:30
本文介绍了如何防止每次用户界面交互多次重绘闪亮的情节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我闪亮的应用程序中,我有一个日期范围输入和一组复选框.复选框选项是根据输入的$ dateRange确定的.我遇到了一个问题,即每次更改日期范围时,都会重复绘制两次绘图.第一次重绘时,它将使用新的日期范围,但使用旧的复选框选择.然后更新复选框选择,并重新绘制绘图.

In my shiny app I have a date range input and a group of checkboxes. The checkbox choices are determined based on the input$dateRange. I'm running into an issue where the plot is being redrawn twice every time the date range is changed. The first time it is redrawn it will use the new date ranges, but the old checkbox choices. Then the checkbox choices are updated and the plot is redrawn a second time.

有什么方法可以防止多次重绘该图,并且只有在所有其他UI元素都更新后才绘制它吗?

Is there any way to prevent redrawing the plot multiple times and only have it drawn once all the other UI elements have updated?

server.R代码段

server.R code snippet

  # Check boxes for variants in run
  output$choose_variants <- renderUI({
    # Get the variants associated with the run
    dat <- loadVariants(input$dateRange[1], input$dateRange[2])
    if(is.null(dat))
      return()

    # Create the checkboxes and select them all by default
    checkboxGroupInput("variants", "Variants",
                        choices  = dat$variant,
                        selected = dat$variant)
  })

  # Output the data
  output$plot1 <- renderPlot({
    runLocations <- loadRunsBetweenDates(input$dateRange[1], input$dateRange[2], input$variants)
    #ggplot()
  })

ui.R代码段

  sidebarPanel(
    dateRangeInput('dateRange',
      label = 'Date range',
      start = Sys.Date(), end = Sys.Date()
    ),
    uiOutput("choose_variants")
  ),

推荐答案

由于 input $ variants 总是会更改,因此只要使日期范围滑块发生变化,您就可以使绘图依赖于 input $ variants.

Since input$variants always changes whenever the date range slider changes, you can make your plot dependent on input$variants only.

  # Output the data
  output$plot1 <- renderPlot({

    # outside of isolate statement, so this plot becomes dependent on its value.
    input$variants

    # put this in an isolate block, so it doesn´t trigger an invalidation.
    isolate(runLocations <- loadRunsBetweenDates(input$dateRange[1], input$dateRange[2], input$variants))
    #ggplot()
  })

希望这会有所帮助!

替代方法,根据评论中的条件

Alternative, based on condition in comment

您可以创建一个反应式,并使您的绘图仅依赖于此,如下所示:

You could create a reactive, and make your plot only dependent on that as follows:

  loadedVariants <- reactive({
  loadVariants(input$dateRange[1], input$dateRange[2])
  })

 # Output the data
  output$plot1 <- renderPlot({

    # outside of isolate statement, so this plot becomes dependent on its value.
    loadedVariants()

    # put this in an isolate block, so it doesn´t trigger an invalidation.
    isolate(runLocations <- loadRunsBetweenDates(input$dateRange[1], input$dateRange[2], input$variants))
    #ggplot()
  })

这篇关于如何防止每次用户界面交互多次重绘闪亮的情节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆