I have a Shiny app where the server function is reading static images and prints them out in a renderPlot()
call. I can't use renderImage()
because my next step is to add dynamic features to the image before outputting it. I'm running into an issue where the image isn't taking up the full space allocated in the interface, and there's a large white margin around the image. How do I extend the image to take up the full height or width (I understand that there's aspect ratio issues that will prevent me from filling the space completely)?
I tried adding height = XXX
within the renderPlot()
call, but the output is unexpected (in example below, the image gets bigger XXX=800, then at XXX=1800 the image shifts to the side, while there's still white space). I also tried adding par(mar = c(0, 0, 0, 0))
before the plot function, no effect.
Example:
library(shiny)
library(magick)
ui <- fluidPage(
# set app background to blue, to show the white rectangle issue
tags$style('.container-fluid {background-color: #007BA7;}'),
column(6, plotOutput("p", width = "120%"))
)
server <- function(input, output, session){
output$p <- renderPlot({
image <- image_read('https://raw.githubusercontent.com/R-CoderDotCom/samples/main/bird.png')
# change background of image, to show the white rectangle issue
image <- image_background(image, color = "#cbe7ff")
plot(as.raster(image))
})
}
shinyApp(ui = ui, server = server)
I'm not entirely sure what you are trying to achieve, but you were on the right track with par(mar = c(0, 0, 0, 0)
, however there is also oma
for the outer margin area. How is this?
library(shiny)
library(magick)
ui <- fluidPage(
# set app background to blue, to show the white rectangle issue
tags$style('.container-fluid {background-color: #007BA7;}'),
column(6, plotOutput("p"))
)
server <- function(input, output, session){
output$p <- renderPlot({
image <- image_read('https://raw.githubusercontent.com/R-CoderDotCom/samples/main/bird.png')
# change background of image, to show the white rectangle issue
image <- image_background(image, color = "#cbe7ff")
par(mar = c(0, 0, 0, 0), oma = c(0, 0, 0, 0))
plot(as.raster(image))
})
}
shinyApp(ui = ui, server = server)