1

I'm trying to scale down a plotOutput with Shiny R.

I have this plot: enter image description here

from this code:

#In ui:
fluidRow(
        column(width = 12,
           h4("Diagrama Persistencia"),
           plotOutput("Diagrama")
        )
      )

#In server 
output$Diagrama <- renderPlot({
        F_PlotDiag(Diag = isolate(values$data), tipoPlot = input$radioPlot, tamEjeNom = input$sliderTamEjeNom)
      }, height = input$sliderHeight, width = input$sliderWidth) 

Notice the height and width params. This works because all is in an observeEvent context.

As you can see, the hole plot won't fit in the screen. The problem with reducing height and width is that it looks like this:

enter image description here

But actually, if I right click and save the FIRST image, it looks fine unlike the second image: enter image description here

Is there a way to show the whole plot in the browser by scaling it down? So that I can see it as if I downloaded the image.

I really don't know much about CSS so I can't really provide any logical attempts, but this is what I've tried for my HTML:

enter image description here

tags$style(type="text/css", ".shiny-bound-output { transform: 'scale(.5)' }")
  tags$style(type="text/css", ".shiny-plot-output { transform: 'scale(.5)' }")
  tags$style(type="text/css", "#Diagrama { height: 20px }")

With no success.

AFP_555
  • 2,392
  • 4
  • 25
  • 45

1 Answers1

3

Since you didn't provide a reproducible example, see if this example helps you. Based on https://stackoverflow.com/a/8839678/4190526

The key is the following line, which finds the image under the div with the id distPlot (i.e. the plot name in ui.R), and define the CSS with a max-height but otherwise auto.

tags$style(HTML("div#distPlot img {width: auto; height: auto; max-width: auto; max-height: 400px;}")),

Full code

library(shiny)

ui <- shinyUI(fluidPage(

   tags$style(HTML("div#distPlot img {width: auto; height: auto; max-width: auto; max-height: 400px;}")),
   titlePanel("Old Faithful Geyser Data"),
   sidebarLayout(
      sidebarPanel(
         sliderInput("bins",
                     "Number of bins:",
                     min = 1,
                     max = 50,
                     value = 30)
      ),
      mainPanel(
         plotOutput("distPlot")
      )
   )
))

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

   output$distPlot <- renderPlot({
      x    <- faithful[, 2] 
      bins <- seq(min(x), max(x), length.out = input$bins + 1)

      hist(x, breaks = bins, col = 'darkgray', border = 'white')
   }, width=600, height=1200)
})

shinyApp(ui = ui, server = server)
Community
  • 1
  • 1
Xiongbing Jin
  • 11,779
  • 3
  • 47
  • 41