Resizing the Google chart

So far, so simple. However, there is a problem! Google visualization charts, unlike native R visualizations, are not automatically resized when the browser window changes. We're going to fix this problem very simply, using some values that we can extract from the session argument of function(input, output, session){...}. Shiny makes lots of things available in this variable, and one of the most useful of these is session$clientData. This tells you lots of things about your user's browser, such as the pixel ratio, as well as the height and width of individual output elements within the application. For more on the uses of the session argument, see shiny.rstudio.com/reference/shiny/1.0.2/session.html. In our case, all we need to do is establish a dependency on something within this client data, which will change whenever the browser window resizes.

In this case, output_trend_width is perfect. This is the width of the line plot showing life expectancy over time. These variables are named, for example, output$clientData$output_nameOfOutput_width. When the browser is resized, this property will change. We're not really worried about height because there isn't anything to bump against the gauge below it, only to the left and right. The code to draw the gauge therefore becomes as follows:

output$gauge <- renderGvis({
# dependence on size of plots to detect a resize
session$clientData$output_trend_width

[... as before ...]
})

Changing the width of the browser window will now redraw the gauge, which will make it the right size again.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset