[英]In Shiny/R app - why plotly charts are flickering?
我在这里遇到了麻烦,这是我的代码:
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)
我的问题是:为什么当我选择不同的选项卡面板并更改输入时会在ggplotly()
图表上发生这种闪烁效果(它是正确的名称吗? )?
例如:
假设我选择“第一个”选项。 然后我点击 SubPanel B 并选择“第二个”选项。 然后我单击 SubPanel A 并发生闪烁。 为什么?
我也尝试使用bindcache(input$inputs)
但我有这个错误消息: Don't know how to handle object with class plotly, htmlwidget
“问题”是为了提高效率,Shiny 默认不会更新不可见的输出。 当您在第二个选项卡上并更改输入时, plot_B
更新,但plot_A
不会。 当您单击返回到第一个选项卡时,您的浏览器仍然呈现之前的绘图。 现在可见,Shiny 意识到这已经过时,并切换到导致“闪烁”的新情节。
这很容易解决,只需小心使用需要很长时间计算的输出覆盖默认行为。 在服务器中定义输出后,添加以下行:
outputOptions(output, "plot_A", suspendWhenHidden = FALSE)
outputOptions(output, "plot_B", suspendWhenHidden = FALSE)
我的问题是:为什么当我选择不同的选项卡并更改输入时会出现这种闪烁效果(它的名称是否正确?)
万一这会引导您-我认为在闪亮库中搜索内存泄漏问题可能会帮助您进一步研究(而不是将其描述为“闪烁效果”)。
例如:
https://github.com/rstudio/shiny/issues/1591
https://github.com/rstudio/shiny/issues/2321
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.