how to prevent caching with renderCachedPlot when the plot is blank or null

thanks a lot for the renderCachedPlot() ,but I have a question, how to prevent caching when the plot is blank or null , thanks

output$plot3 <- renderCachedPlot({
a <- func()
if(a>1){
plot(...)
}else{
  return(NULL)
}
},cacheKeyExpr={list(z())})

Hi @kakaymi, your question isn't entirely clear to me. Why does your cacheKeyExpr depend on z()? It seems like it should depend on a instead?

thanks a lot,sorry, I didn't make myself clear,the entire code like following:

z <- reactive({
... 
return(list(a=a,...))
})
output$plot3 <- renderCachedPlot({
a <- z()$a
  if (a>1) {
    shinyjs::show("plot3")
  } else {
    shinyjs::hide("plot3")
  }
if(a>1){
plot(...)
}else{
  return(NULL)
}
},cacheKeyExpr={list(z()$a)})

when the a<=1,the output$plot3 is NULL,the first time run this code in the shiny-server, the plot3 has disappear rightly,but after the second time, start load the cached image, there have a blank plot(white background) at the position.
so i have a question,how to prevent caching with renderCachedPlot when the plot is blank or null?
thanks very much!!

I have a feeling this may fix your problem (let me know if it doesn't!)

a <- reactive(z()$a)

output$plot3 <- renderCachedPlot({
  if (a() > 1) {
    shinyjs::show("plot3")
  } else {
    shinyjs::hide("plot3")
  }
if(a() > 1){
  plot(...)
}else{
  return(NULL)
}
},cacheKeyExpr={a()})

thanks for your rapidly reply, I have try your method, but have no effect, the second time to load the cached image(very tiny size,just less than 500 bytes),there have a blank plot at the position.
I have try to move the shinyjs code trunk out of the renderCachedPlot, just before the output$plot3,have solved this blank plot problem, but the cached plot is still render( though very tiny size), so I still want solved this problem by the renderCachedPlot function self, not by the shinyjs. just not caching it other than hiding it, could you have any suggestion?
My solved code like following, but this is not satisfy me.

z <- reactive({
... 
return(list(a=a,...))
})
observe({
  a <- z()$a
  if (a>1) {
    shinyjs::show("plot3")
  } else {
    shinyjs::hide("plot3")
  }
})
output$plot3 <- renderCachedPlot({
a <- z()$a
if(a>1){
plot(...)
}else{
  return(NULL)
}
},cacheKeyExpr={list(z()$a)})

I have a feeling shinyjs may not be needed...would a transparent (instead of white) background achieve the effect you desire? You can do that with bg = NA

output$plot3 <- renderCachedPlot({
  a <- z()$a
  if(a>1){
    plot(...)
  } else {
    return(NULL)
  }
}, cacheKeyExpr={list(z()$a)}, bg = NA)
1 Like

sorry, I have another things to do these days, I try your method today, but have no effect. the blank plot still display at the position. I have no ideal. thank your very much!

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.