简体   繁体   English

在 Shiny/R 应用程序中 - 为什么 plotly 图表闪烁?

[英]In Shiny/R app - why plotly charts are flickering?

I having a trouble here, this is my code:我在这里遇到了麻烦,这是我的代码:

library(gapminder)
library(shiny)

library(plotly)
library(shinyWidgets)

df_first_mexico <- gapminder %>% filter(country == "Mexico")%>% select(year,lifeExp)
df_second_mexico <- gapminder %>% filter(country == "Mexico")%>% select(year,pop)
df_third_mexico <- gapminder %>% filter(country == "Mexico")%>% select(year,gdpPercap)

df_first_chile <- gapminder %>% filter(country == "Chile")%>% select(year,lifeExp)
df_second_chile <- gapminder %>% filter(country == "Chile")%>% select(year,pop)
df_third_chile <- gapminder %>% filter(country == "Chile")%>% select(year,gdpPercap)

ui <- fluidPage(

  mainPanel(

    tabPanel("Tab1",
             tabsetPanel(
               div(

                 pickerInput(
                   inputId = "inputs",
                   multiple = FALSE,
                   # width = "150px",
                   label = "Taxas - Sem Ajuste Sazonal",
                   choices = c("first",
                               "second", "third"),

                   selected = c("first"),

                   options = list(title = "Choice")
                 )
               ),
               tabPanel("SubPanelA",
                        div(
                          plotlyOutput("plot_A", width = "200px")

                        )
               ),

               tabPanel("SubPanelB",
                        div(
                          plotlyOutput("plot_B", width = "200px")

                        )
               )


))))




server <- function(input, output) {

  inputs_reactive <- reactive({input$inputs}) %>% bindCache(input$inputs)

  output$plot_A <- renderPlotly({

    if(inputs_reactive() == "first"){

    ggplotly(ggplot(df_first_mexico , aes(x = year, y = lifeExp)) + geom_line())

    }else{

      if(inputs_reactive() == "second"){

        ggplotly(ggplot(df_second_mexico , aes(x = year, y = pop)) + geom_line())

      }else{

        ggplotly(ggplot(df_third_mexico , aes(x = year, y = gdpPercap)) + geom_line())

      }


    }



  })

  output$plot_B <- renderPlotly({

    if(inputs_reactive() == "first"){

      ggplotly(ggplot(df_first_chile, aes(x = year, y = lifeExp)) + geom_line(color = "red"))

    }else{

      if(inputs_reactive() == "second"){

        ggplotly(ggplot(df_second_chile, aes(x = year, y = pop)) + geom_line(color = "red"))

      }else{

        ggplotly(ggplot(df_third_chile, aes(x = year, y = gdpPercap)) + geom_line(color = "red"))

      }


    }



  })




  }

# Run the application
shinyApp(ui = ui, server = server)

My question is: why when I select different tabpanels and change the inputs happens this flickering effect ( is it the right name? ) on the ggplotly() charts ?我的问题是:为什么当我选择不同的选项卡面板并更改输入时会在ggplotly()图表上发生这种闪烁效果(它是正确的名称吗? )?

For example:例如:

suppose that I choose the "first" option.假设我选择“第一个”选项。 Then I click on SubPanel B and choose "second" option.然后我点击 SubPanel B 并选择“第二个”选项。 And then I click on the SubPanel A and the flickering happens.然后我单击 SubPanel A 并发生闪烁。 Why?为什么?

I also tried to use bindcache(input$inputs) but I have this error message: Don't know how to handle object with class plotly, htmlwidget我也尝试使用bindcache(input$inputs)但我有这个错误消息: Don't know how to handle object with class plotly, htmlwidget

The "issue" is that for efficiency, Shiny by default won't update outputs that aren't visible. “问题”是为了提高效率,Shiny 默认不会更新不可见的输出。 When you're on the second tab and change the input, the plot_B updates but plot_A doesn't.当您在第二个选项卡上并更改输入时, plot_B更新,但plot_A不会。 When you click back to the first tab, you're browser still has the previous plot rendered.当您单击返回到第一个选项卡时,您的浏览器仍然呈现之前的绘图。 Now being visible, Shiny realizes this is out of date, and switches to the new plot causing the "flicker."现在可见,Shiny 意识到这已经过时,并切换到导致“闪烁”的新情节。

It's easy enough to solve, just be careful overriding the default behavior with outputs that take a long time to calculate.这很容易解决,只需小心使用需要很长时间计算的输出覆盖默认行为。 After you define your outputs in the server, add these lines:在服务器中定义输出后,添加以下行:

  outputOptions(output, "plot_A", suspendWhenHidden = FALSE)
  outputOptions(output, "plot_B", suspendWhenHidden = FALSE)

My question is: why when I select different tabpanels and change the inputs happens this flickering effect (is it the right name?)我的问题是:为什么当我选择不同的选项卡并更改输入时会出现这种闪烁效果(它的名称是否正确?)

Incase this steers you - I think searching for issues with memory leak problems in the shiny library might help you delve further (rather than describing it as a 'flickering effect').万一这会引导您-我认为在闪亮库中搜索内存泄漏问题可能会帮助您进一步研究(而不是将其描述为“闪烁效果”)。

For example:例如:
https://github.com/rstudio/shiny/issues/1591 https://github.com/rstudio/shiny/issues/1591
https://github.com/rstudio/shiny/issues/2321 https://github.com/rstudio/shiny/issues/2321

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

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