You can get the dimensions of the window as input
using some JavaScript
. (Related SO question)
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);
});
'))
After including this script, you can access input$dimensions
, as a 2 element vector in shiny
(containing width
and height
).
So you can introduce an observer
that uses input$dimension
, and create custom CSS
based on that:
observe( {
if (input$slt == "TRUE") {
# put the button at bottom right corner
output$ui <- renderUI( {
margin_left <- input$dimension[1] * 0.8 # 80% off left
margin_top <- input$dimension[2] * 0.8 # 80% off top
actionButton("btn", "Moving button at bottom",
style = sprintf("position: absolute; left: %spx; top: %spx;", margin_left, margin_top))
})
} else {
# put the button at middle right
output$ui <- renderUI( {
margin_left <- input$dimension[1] * 0.8 # 80% off left
margin_top <- input$dimension[2] * 0.5 # 50% off top
actionButton("btn", "Moving button at middle",
style = sprintf("position: absolute; left: %spx; top: %spx;", margin_left, margin_top))
})
}
})
Not sure how useful it is though. For example when placing an element at the far right it's better to use something like: "right: 0px;"
.