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 ?
For example:
suppose that I choose the "first" option. Then I click on SubPanel B and choose "second" option. And then I click on the SubPanel A and the flickering happens. 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
CodePudding user response:
The "issue" is that for efficiency, Shiny by default won't update outputs that aren't visible. When you're on the second tab and change the input, the plot_B
updates but plot_A
doesn't. 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."
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)
CodePudding user response:
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/2321