transparency in shiny app not working on RStudio IDE Windows

I'm running Rstudio 2022.07.1 Build 554 on a Windows 10 machine.

I use plot(...) + polygon(..., col = rgb(0,0,0,0.2), border = NA) to plot a time series with transparent error bars as part of a shiny app.
When I run the shiny app locally, I do not see the polygon on the browser, unless I set alpha=1.
When I run the app from shinyapps it works with alpha=0.2, so that excludes a problem with the code.
Transparency works on the RStudio viewer pane, but not on the browser (run external).
I have tried Firefox and Edge.

Any idea how to fix this?

Hi!

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

Here is a code example

library(shiny)
ui <- fluidPage(
  textInput(inputId = "error", label = "St. Dev. error bars", value = "0.1"),
  actionButton(inputId = "run", label = "Make me a plot"),
  plotOutput("plot")
)
server <- function(input, output, session) {
  observeEvent(input$run, {
    output$plot <- renderPlot({
      n <- 40
      e <- abs(rnorm(n,0,as.numeric(input$error))) + 0.001
      plot(seq(n),rep(0,n), type = 'p', pch = 20, xlab = "", ylab = "", xlim = NULL, ylim = NULL)
      polygon(c(seq(n),rev(seq(n))),c(e,rev(-e)),col = rgb(0,0,0,0.2),border = NA)
    })
  })
}
shinyApp(ui, server)

After playing around with this example, I realized that when the error bars are too large, the polygon is not drawn locally, but it still does on shinyapps. Check this by setting the error bar sd at 0.5 and iterate.
The error bars size may not be the full story, because in my shiny app the user can zoom in/out the series and the polygon is never visible, whereas in shinyapps it is.

On my laptop (i.e. local) on both chrome and edge I do see a polygon, even with the alpha at 0.2
As I can't reproduce this problem I don't know how to help you solve it...

The only issue I can see is when your input$error gets large,as you mention in your second post) then the polygon cant fit within the confines of the plot, and perhaps its clipped away and not drawn ? changing the plot limits ylim, would address that

Thanks for your answer!
Indeed, when running the app from local on Windows and on the browser, the error bar size is what determines if the polygon is drawn (without clipping) or not at all.
The thing is that from shinyapps and the same browser, the polygon is always being drawn , even if it's clipped, that's perfectly OK to me.
Are you not reproducing this?
I just run the example on a different linux machine (local on Firefox) and the polygon is always drawn as well, even when setting huge error bars.
So I suspect there is something with the Windows version of RStudio Desktop.

The transparent polygon is not drawn on the Windows' RStudio viewer pane either when error bars are too large, for instance St. Dev. error bars = 1 in the example.
If alpha = 1 then the solid polygon is always drawn with any error bar size.
Can anyone reproduce this? And in that case, should this be considered a bug?

Fixed by changing the PNG device to type = cairo or type = cairo-png in the renderPlot options.
Transparency does not work for me when the PNG device is forced to type = windows, so I guess that's the default value on my RStudio IDE Windows.

1 Like

I think its great you figured it out, and fantastic that you shared back the solution to the community

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.