Skip to content

Commit

Permalink
example two grids + bind element
Browse files Browse the repository at this point in the history
  • Loading branch information
pvictor committed May 29, 2024
1 parent 251458a commit d30fb40
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 2 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: gridstackr
Title: Interactive grid layout with the JavaScript 'GridStack' Library
Version: 0.0.0.9000
Version: 0.0.0.9100
Authors@R: c(
person("Victor", "Perrier", email = "[email protected]", role = c("aut", "cre")),
person("Fanny", "Meyer", role = "aut"))
Expand Down
95 changes: 95 additions & 0 deletions examples/shiny-two.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@

library(shiny)
library(bslib)
library(ggplot2)
library(gridstackr)

ui <- page_fluid(
tags$h2("Two GridStack example"),
fluidRow(
column(
width = 3,
tags$b("List of items to put in dashboard:"),
gridstackOutput("grid1")
),
column(
width = 9,
tags$b("Dashboard:"),
gridstackOutput("grid2")
)
)
)

server <- function(input, output, session) {

output$grid1 <- renderGridstack({
gridstack(
margin = "10px",
cellHeight = "140px",
column = 1,
options = list(
acceptWidgets = TRUE,
dragOut = TRUE
),
gs_item(value_box(
title = "Customer lifetime value",
value = "$5,000",
showcase = icon("bank"),
theme = "text-success",
class = "mb-0"
)),
gs_item(value_box(
title = "Customer lifetime value",
value = "$5,000",
showcase = icon("bank"),
theme = value_box_theme(bg = "#e6f2fd", fg = "#0B538E"),
class = "border mb-0"
)),
gs_item(
plotOutput("plot1", height = "100%"),
class_content = "bg-white p-2 border rounded-4"
),
gs_item(
plotOutput("plot2", height = "100%"),
class_content = "bg-white p-2 border rounded-4"
),
gs_item(
plotOutput("plot3", height = "100%"),
class_content = "bg-white p-2 border rounded-4"
),
gs_item(
plotOutput("plot4", height = "100%"),
class_content = "bg-white p-2 border rounded-4"
)
)
})

output$grid2 <- renderGridstack({
gridstack(
minRow = 3,
cellHeight = "140px",
options = list(
acceptWidgets = TRUE,
dragOut = TRUE
)
)
})

output$plot1 <- renderPlot({
ggplot(mtcars) + geom_point(aes(mpg, disp))
})
outputOptions(output, "plot1", suspendWhenHidden = TRUE)
output$plot2 <- renderPlot({
ggplot(mtcars) + geom_boxplot(aes(gear, disp, group = gear))
})
output$plot3 <- renderPlot({
ggplot(mtcars) + geom_smooth(aes(disp, qsec))
})
output$plot4 <- renderPlot({
ggplot(mtcars) + geom_bar(aes(carb))
})

}

if (interactive())
shinyApp(ui, server)
2 changes: 1 addition & 1 deletion inst/htmlwidgets/gridstack.js

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions srcjs/widgets/gridstack.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ HTMLWidgets.widget({
grid.on("resizestop", function(event, el) {
window.dispatchEvent(new Event("resize"));
});
if (HTMLWidgets.shinyMode) {
var $all = $(el);
Shiny.bindAll($all);
}
grid.on("added", function(event, items) {
if (HTMLWidgets.shinyMode) {
items.forEach(function(item) {
var $item = $(item);
Shiny.bindAll($item);
});
}
});

if (HTMLWidgets.shinyMode) {
var serializedFull = grid.save(true, true);
Expand Down

0 comments on commit d30fb40

Please sign in to comment.