Dynamically adjust the height and / or width of brilliant output based on window size

I would like to have the height and width of the output, the size of which depends on the current window size. I tried using below, but to no avail.

ShinyUi <- fluidPage( # Application title titlePanel("title"), sidebarLayout( sidebarPanel( ... inputs ... ), mainPanel( plotlyOutput("distPlot", height = 'auto', width = 'auto') ) )) ShinyServer <- function(input, output, session) { output$distPlot <- renderPlotly({ p <- ggplot(dataShow, aes(x=dataShow$X, y=dataShow$Y)) + geom_point(shape=1, alpha = 0.5, color = "grey50") ggplotly(p) }) } # Run the application shinyApp(ui = ShinyUi, server = ShinyServer) 

Do you know of any other use cases, possibly in the server function, instead of using the above user interface?

Smaller window: enter image description here

Extended window: enter image description here

+5
source share
1 answer

It does not answer your question, but according to my comments, you can add the height and width of the plot to the ggplotly function using the js link from this .

I have prepared a minimal example of what you want.

 library(shiny) library(plotly) ShinyUi <- fluidPage( tags$head(tags$script(' var dimension = [0, 0]; $(document).on("shiny:connected", function(e) { dimension[0] = window.innerWidth; dimension[1] = window.innerHeight; Shiny.onInputChange("dimension", dimension); }); $(window).resize(function(e) { dimension[0] = window.innerWidth; dimension[1] = window.innerHeight; Shiny.onInputChange("dimension", dimension); }); ')), plotlyOutput("distPlot", width = "auto") ) ShinyServer <- function(input, output, session) { #To make the responsive to the change in UI size observeEvent(input$dimension,{ output$distPlot <- renderPlotly({ p <- ggplot(iris, aes(x = Sepal.Length, y=Sepal.Width)) + geom_point(shape=1, alpha = 0.5, color = "grey50") ggplotly(p, width = (0.95*as.numeric(input$dimension[1])), height = as.numeric(input$dimension[2])) }) }) } # Run the application shinyApp(ui = ShinyUi, server = ShinyServer) 

The output you get is as follows: enter image description here

Now, when you make the window even smaller, you still get a plot that takes up the entire screen (without scrollbars!) As follows: enter image description here

+6
source

Source: https://habr.com/ru/post/1268465/


All Articles