r-闪亮的缓存会导致renderUI渲染延迟

  • 本文关键字:renderUI 延迟 缓存 r shiny
  • 更新时间 :
  • 英文 :


我下面有一个Shiny应用程序,其中我使用库highchartggplotplotlyiris数据集上绘制散点图。

library(shiny)
library(shinydashboard)
library(highcharter)
library(shinyWidgets)
library(plotly)
library(ggplot2)
library(data.table)
siderbar <- dashboardSidebar(
sidebarMenu(
# Add buttons to choose the way you want to select your data
selectizeInput(inputId = "inp_species", label = "Select by:", choices = c("setosa", "versicolor", "virginica"), selected = "setosa"),
awesomeRadio(inputId = "radioTest", label = "Choose one:",
choices=c("High Charter" = "highcharter",
"Simple Plot" = "simple",
"Plotly" = "plotly"),
inline = FALSE, selected = "highcharter")
)   
)
body <- dashboardBody(
fluidRow(
tabBox(
side = "right",
selected = "Tab1",
tabPanel("Tab1", "Tab content 1", uiOutput("tabset1Selected"))
)
),
)
shinyApp(
ui = dashboardPage(
dashboardHeader(title = "tabBoxes"),
siderbar,
body
),

server = function(input, output, session) {

iris_dt <- reactive({
iris_table = data.table(copy(iris))
iris_table[Species == input$inp_species]
})

render_content <-  reactive({
req(input$radioTest)
print(input$radioTest)
if(input$radioTest=='highcharter'){
output$plot1 <-   renderHighchart({
highchart() %>%
hc_add_series(iris_dt(), type = "scatter", hcaes(x = Petal.Width, y = Sepal.Length))
})
out <- highchartOutput("plot1")
}

else if(input$radioTest=='plotly'){
output$plot2 <- renderPlotly({
plot_ly(iris_dt(), x = ~ Petal.Width, y = ~ Sepal.Length)
})
out <- plotlyOutput("plot2")
}


else if(input$radioTest=='simple'){
output$plot3 <- renderPlot({
ggplot(iris_dt(), aes(x =  Petal.Width, y = Sepal.Length)) + geom_point()
})
out <- plotOutput("plot3")

}

return(out)
})


# The currently selected tab from the first box
output$tabset1Selected <-  renderUI({
render_content()
})


}
)

我正在选择库以使用selectInput框动态绘制图表。

问题来了-

  1. 我在selectInput框中选择一个物种,highchart库绘制散点图
  2. 然后我在单选按钮部分选择plotly,然后使用plotly进行渲染
  3. 我在selectInputplotly中更改了物种,重新绘制了图
  4. 现在,当我单击highchart单选按钮时,会绘制早期物种的图(来自缓存(几秒钟,然后绘制所选物种的图表

问题是否有方法清除或禁用缓存以避免渲染延迟?

我们可以禁用动画效果。虽然这不是解决问题的办法,但在这期间它可能会有所帮助。

output$plot1 <- renderHighchart({
highchart() %>%
hc_add_series(
data = iris_dt(),
type = "scatter",
hcaes(x = Petal.Width, y = Sepal.Length)
) %>%
hc_plotOptions(
series = list(
animation = FALSE
)
)
})

最新更新