How to automatically "ggplot size" the correct size "in" Shiny "?

I use Shiny with the likert library, which outputs ggplot2 objects, to interactively display responses. The user can select a group of questions for the plot (each group has a different number of questions), and which question should be grouped. This means that in general there is a wide variety of number of “bars”.

If I leave the height setting to auto (default), the graph becomes incredibly crowded.

enter image description here

It seems that there are two ways to adjust the height - in ui.R, I can adjust the size of the “viewport”, and in the call to renderPlot I can specify the height of the graph (if it is higher than the viewport has a scroll bar). If I specify a more spacious graph, the graph above looks great:

enter image description here

However, when I return to the question with fewer alternatives, they become stretched to look funny:

enter image description here

Is there a way to make the bars have a constant width, regardless of how many there are, and that the chart changes dynamically without me, in order to calculate a certain height depending on the number of elements?

+4
source share
1 answer

renderPlot({}) "" ggplot ( R) "". "" , . , , , , "", . , , . :

output$myPlotOutput <- renderPlot({
  myGGPlotCode
}, height = myHeightAlgorithm())

myHeightAlgorithm() (. ?renderPlot() height).

- , Shiny. , ""

set_shiny_plot_height <- function(session, output_width_name){
   function() { 
     session$clientData[[output_width_name]] 
   }
}

session - session shinyServer(function(input, output, session)), output_width_name - output$myPlotOutput. output$myPlotOutput: output_myPlotOutput_width

. set_shiny_plot_height(session, "output_myPlotOutput_width") myHeightAlgorithm()

( ). , , . -.

set_shiny_plot_height_with_respects_to_width <- function(session, output_width_name){
   width <- function() { 
     session$clientData[[output_width_name]] 
   }
   # Do something with the width
   width / 2
}
+2

All Articles