Skip to content

Instantly share code, notes, and snippets.

@kylebutts
Last active June 12, 2021 14:41
Show Gist options
  • Select an option

  • Save kylebutts/0cc3045b964f59565f37f7078777409e to your computer and use it in GitHub Desktop.

Select an option

Save kylebutts/0cc3045b964f59565f37f7078777409e to your computer and use it in GitHub Desktop.
Shiny - Window Size: gets window size and stores in input$dimension ([1] = width, [2] = height)
# From https://stackoverflow.com/questions/36995142/get-the-size-of-the-window-in-shiny
# https://stackoverflow.com/a/37060206
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);
});
'))
@kylebutts

kylebutts commented Jun 12, 2021

Copy link
Copy Markdown
Author

Include the above in UI declaration

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment