The gridstackR package allows users to easily create Dashboards with gridstack.js functionalities
‘gridstack.js is […] designed to help developers create beautiful draggable, resizable, responsive […] layouts with just a few lines of code’
You can install the development version from GitHub with:
# install.packages("devtools")
::install_github("petergandenberger/gridstackeR") devtools
In the example below we add gridstackR to the basic shiny application ‘Old Faithful Geyser’. The plot can now be dynamically resized and the position for both, the plot and the slider, can be changed using simple drag&drop.
library(shiny)
library(gridstackeR)
<- fluidPage(
ui grid_stack(
grid_stack_item(
h = 4, w = 4, id = "plot_container", style = "overflow:hidden",
::box(
shinydashboardtitle = "Histogram", status = "primary", solidHeader = TRUE, width = 12, height = "100%",
plotOutput("plot", height = "auto")
)
),grid_stack_item(
h = 3, w = 4, minH = 3, maxH = 3, id = "slider", style = "overflow:hidden",
::box(
shinydashboardtitle = "Inputs", status = "warning", solidHeader = TRUE, width = 12, height = "100%",
sliderInput("slider", "Slider input:", 1, 100, 50)
)
)
)
)
<- function(input, output) {
server $plot <- renderPlot({
output<- faithful$waiting
x <- seq(min(x), max(x), length.out = input$slider + 1)
bins
hist(
breaks = bins, col = "#75AADB", border = "white",
x, xlab = "Waiting time to next eruption (in mins)",
main = "Histogram of waiting times"
)
},# set the height according to the container height (minus the margins)
height = function() {
<- 150
min_height <- 80
margin max(input$plot_container_height - margin, min_height)
})
}
shinyApp(ui, server)
In the ui.R
file, create a grid using
grid_stack(...)
and place grid-stack-items inside using
grid_stack_item(...)
.
Specify options like height, width, x-, y-position as well. Check the gridstack.js documentation for a full list of options.
The ui.R
file might contain something like the
following.
grid_stack(
grid_stack_item(
h = 4, w = 4, id = "plot_container", style = "overflow:hidden",
::box(
shinydashboardtitle = "Histogram", status = "primary", solidHeader = TRUE, width = 12, height = "100%",
plotOutput("plot", height = "auto")
)
) )
Elements inside grid-stack-item
might not change their
height automatically.
The following example shows how the height of the plot can be set
dynamically using the <id>_height
callback
Note: the plot_container_height
references the height of
the id = "plot_container"
created in the ui.R
example above.
$plot <- renderPlot({
output<- faithful$waiting
x <- seq(min(x), max(x), length.out = input$slider + 1)
bins
hist(
breaks = bins, col = "#75AADB", border = "white",
x, xlab = "Waiting time to next eruption (in mins)",
main = "Histogram of waiting times"
)
},# set the height according to the container height (minus the margins)
height = function() {
<- 150
min_height <- 80
margin max(input$plot_container_height - margin, min_height)
} )
The height for a DT::dataTableOutput
can be set as in
the following example.
ui.R
grid_stack_item(
w = 4, h = 10, x = 0, y = 0, id = "c_table",
::dataTableOutput("mytable")
DT )
server.R
$mytable <- DT::renderDataTable({
output::datatable(mtcars, options = list(
DT# set the height according to the container height (minus the margins)
scrollY = max(input$c_table_height, 200) - 110, paging = FALSE
)
) })
The height for a echarts4r::echarts4rOutput
can easily
be set using the height="100%"
option.
ui.R
grid_stack_item(
w = 5, h = 5, x = 7, y = 0, id = "c_plot",
echarts4rOutput(outputId = "plot", height = "100%")
)
It is also possible to load predefined Layouts. This is demonstrated in the healthdown example above.
ui.R
server.R
# do this inside of e.g. an 'observeEvent' of an 'actionButton'
<- '[
new_layout {"id": "c_plot", "options":{"x": 0,"y": 0,"w": 2, "h": 10}},
{"id": "c_map", "options":{"x": 2,"y": 0,"w": 5, "h": 5}}
]'
$load_grid_layout(new_layout) js