Shiny server does not respect calls to par(bg). local IDE does


#1

When using the base R graphics in a shiny app I tried to make the background of my plots dark so I invoked

par(mar = rep(0, 4), bg = "black", fg = "white")

before calls to the plotting functions. On my desktop in RStudio it all works as expected but when pushed to an instance of shiny server the bg parameter is not respected while the fg parameter is! The plot background remains white. You can see this in action (at least until you show me my errror!) here, on the opening screen. This problem doesn’t exist with ggplot themes, FWIW.

Thanks for ideas how to fix this.


#2

I can’t see your example as the link is blocked from my workplace, but have you tried setting the background colour in the call to renderPlot rather than in par(bg = …)?

e.g.

ui <- fluidPage(
  titlePanel("Hello Shiny!"),
  sidebarLayout(
    sidebarPanel(
      sliderInput(inputId = "bins",
                  label = "Number of bins:",
                  min = 1,
                  max = 50,
                  value = 30)
    ),
    mainPanel(
      plotOutput(outputId = "distPlot")
    )
  )
)

server <- function(input, output) {
  output$distPlot <- renderPlot({
    x    <- faithful$waiting
    bins <- seq(min(x), max(x), length.out = input$bins + 1)
    par(fg = "white")
    hist(x, breaks = bins, col = "#75AADB", border = "white",
         xlab = "Waiting time to next eruption (in mins)",
         main = "Histogram of waiting times", 
         col.lab = "white", col.main = "white", col.axis = "white"
    )
  }, bg = "black")
}

shinyApp(ui, server)

#3

That is the answer! Thanks. I erred in trying to use outputArgs=list(bg="black"), which is just for markdown docs and is described in the docs, instead of just bg="black". When that didn’t work I was stumped.

It is still worth noting that the behavior of the desktop version of Shiny and the server versions are inconsistent. What works on the desktop breaks in the server. Further, the behavior is inconsistent. Sometimes I have to force a refresh of the panel by changing the input parameters before the change in bg is reflected…and sometimes not.