The par() function is not honored when creating graphics within an RShiny application.

I am having issues with a Shiny App such that the text labels are being cut off. If I were performing this plotting in R, I would adjust the margins of the plotting area using par(mar = c(10,10,10,10)), or other adjustments.

However, when I attempt to adjust the margins using par() within a shiny app no change takes effect. Here is a sample shiny app where I attempt to adjust the margins without success:

server.R

library(shiny)

shinyServer(function(input, output) {

    output$distPlot <- renderPlot({

        x    <- faithful[, 2]
        bins <- seq(min(x), max(x), length.out = input$bins + 1)
        
        par(mar = c(10,10,10,10))

        hist(x, breaks = bins, col = 'darkgray', border = 'white', main = "Obsequious people are usually not being genuine; they resort to flattery and other fawning ways to stay in the good graces of authority figures. An obsequious person can be called a bootlicker, a brownnoser or a toady.")

    })
})

ui.R

library(shiny)

# Define UI for application that draws a histogram
shinyUI(fluidPage(

    # Application title
    titlePanel("Old Faithful Geyser Data"),

    # Sidebar with a slider input for number of bins
    sidebarLayout(
        sidebarPanel(
            sliderInput("bins",
                        "Number of bins:",
                        min = 1,
                        max = 50,
                        value = 30)
        ),

        # Show a plot of the generated distribution
        mainPanel(
            plotOutput("distPlot")
        )
    )
))

Is it possible to use par() to define a plotting area for a shiny app?

When I read your post I wondered how you were judging that it didnt work, were you rerunning the app with different values of par and comparing them? do you recall what values you tried ?
Here I have a reprex that for me shows that par works...

Note. I have since edit this example to demonstrate the use of strwrap()


library(shiny)

ui <- fluidPage(
  
  # Application title
  titlePanel("Old Faithful Geyser Data"),
  
  # Sidebar with a slider input for number of bins
  sidebarLayout(
    sidebarPanel(
      shiny::checkboxInput("par",
                           "do par ?"),
      shiny::checkboxInput("strwrap",
                           "use strwrap?"),
      sliderInput("bins",
                  "Number of bins:",
                  min = 1,
                  max = 50,
                  value = 30)
    ),
    
    # Show a plot of the generated distribution
    mainPanel(
      plotOutput("distPlot")
    )
  )
)

server <- function(input, output, session) {

    output$distPlot <- renderPlot({
      
      x    <- faithful[, 2]
      bins <- seq(min(x), max(x), length.out = input$bins + 1)
      if(input$par){
      par(mar = c(10,10,10,10))
      }
      title_text <- "Obsequious people are usually not being genuine; they resort to flattery and other fawning ways to stay in the good graces of authority figures. An obsequious person can be called a bootlicker, a brownnoser or a toady."
      if(input$strwrap){
        title_text <- strwrap(title_text, width = 60)
      }
      hist(x, breaks = bins, col = 'darkgray', border = 'white', main = title_text)
      
    })
}

shinyApp(ui, server)

This topic was automatically closed 54 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.