Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export(render_dock_view)
export(restore_dock)
export(save_dock)
export(select_panel)
export(set_panel_size)
export(update_dock_view)
import(htmlwidgets)
importFrom(shiny,getDefaultReactiveDomain)
Expand Down
35 changes: 35 additions & 0 deletions R/set_panel_size.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#' Change panel size dynamically
#' @param dock_id Dock unique id. When using modules the namespace
#' is automatically added.
#' @param id Panel id.
#' @param height New height of the panel.
#' @param session shiny session object.
#' See \url{https://dockview.dev/docs/core/panels/resizing}.
#' @export
#' @return This function is called for its side effect.
#' It sends a message to JavaScript through the current websocket connection,
#' leveraging the shiny session object.
set_panel_size <- function(
dock_id,
id,
height,
session = getDefaultReactiveDomain()
) {
id <- as.character(id)
panel_ids <- get_panels_ids(dock_id, session)
if (!(id %in% panel_ids)) {
stop(sprintf(
"<Panel (ID: %s)>: invalid value (%s) for `id`. Valid ids are: %s.",
id,
id,
paste(panel_ids, collapse = ", ")
))
}
session$sendCustomMessage(
sprintf("%s_resize-panel", session$ns(dock_id)),
list(
id = id,
options = list(height = height)
)
)
}
100 changes: 100 additions & 0 deletions inst/examples/resize_panel/app.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
library(shiny)
library(bslib)
library(visNetwork)
library(dockViewR)

nodes <- data.frame(id = 1:3)
edges <- data.frame(from = c(1, 2), to = c(1, 3))

ui <- page_fillable(
sliderInput(
"panel_height",
"Resize Panel Height:",
min = 100,
max = 800,
value = 400
),
dockViewOutput("dock")
)

server <- function(input, output, session) {
exportTestValues(
panel_ids = get_panels_ids("dock"),
active_group = get_active_group("dock"),
grid = get_grid("dock")
)

observeEvent(input$panel_height, {
req(get_panels_ids("dock"))
set_panel_size("dock", 2, input$panel_height)
})

output$dock <- renderDockView({
dock_view(
panels = list(
panel(
id = "1",
title = "Panel 1",
content = tagList(
sliderInput(
"obs",
"Number of observations:",
min = 0,
max = 1000,
value = 500
),
plotOutput("distPlot")
)
),
panel(
id = "2",
title = "Panel 2",
content = tagList(
visNetworkOutput("network")
),
position = list(
referencePanel = "1",
direction = "right"
),
minimumWidth = 500
),
panel(
id = "3",
title = "Panel 3",
content = tagList(
selectInput(
"variable",
"Variable:",
c("Cylinders" = "cyl", "Transmission" = "am", "Gears" = "gear")
),
tableOutput("data")
),
position = list(
referencePanel = "2",
direction = "below"
),
minimumHeight = 50
)
),
theme = "replit"
)
})

output$distPlot <- renderPlot({
req(input$obs)
hist(rnorm(input$obs))
})

output$network <- renderVisNetwork({
visNetwork(nodes, edges, width = "100%")
})

output$data <- renderTable(
{
mtcars[, c("mpg", input$variable), drop = FALSE]
},
rownames = TRUE
)
}

shinyApp(ui, server)
14,547 changes: 14,546 additions & 1 deletion inst/htmlwidgets/dockview.js

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions man/set_panel_size.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion srcjs/modules/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,13 @@ const addPanel = (panel, api) => {
}
}
let props = { ...panel, ...internals }
return (api.addPanel(props))
api.addPanel(props);
// Resize panel content on panel resize.
// This only works if the nested content
// can resize correctly.
api.getPanel(panel.id).api.onDidDimensionsChange((e) => {
window.dispatchEvent(new Event('resize'));
})
}

const movePanel = (m, api) => {
Expand Down
8 changes: 8 additions & 0 deletions srcjs/widgets/dockview.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ HTMLWidgets.widget({
}
api.updateOptions(m);
})

// Panel resize
Shiny.addCustomMessageHandler(el.id + '_resize-panel', (m) => {
let panel = api.getPanel(m.id);
if (panel) {
panel.api.setSize(m.options);
}
})
}

},
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/_snaps/set_panel_size.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# set_panel_size works

Code
set_panel_size("dock", "blabla", height = 400, session = session)
Condition
Error in `set_panel_size()`:
! <Panel (ID: blabla)>: invalid value (blabla) for `id`. Valid ids are: 2, 3, test.

64 changes: 64 additions & 0 deletions tests/testthat/_snaps/set_panel_size/set_panel_size-001.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"input": {
"obs": 500,
"variable": "cyl"
},
"export": {
"active_group": "3",
"grid": {
"root": {
"type": "branch",
"data": [
{
"type": "leaf",
"data": {
"views": [
"1"
],
"activeView": "1",
"id": "1"
},
"size": 95
},
{
"type": "branch",
"data": [
{
"type": "leaf",
"data": {
"views": [
"2"
],
"activeView": "2",
"id": "2"
},
"size": 95
},
{
"type": "leaf",
"data": {
"views": [
"3"
],
"activeView": "3",
"id": "3"
},
"size": 95
}
],
"size": -5
}
],
"size": 0
},
"width": 0,
"height": 0,
"orientation": "HORIZONTAL"
},
"panel_ids": [
"1",
"2",
"3"
]
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
64 changes: 64 additions & 0 deletions tests/testthat/_snaps/set_panel_size/set_panel_size-002.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"input": {
"obs": 500,
"variable": "cyl"
},
"export": {
"active_group": "3",
"grid": {
"root": {
"type": "branch",
"data": [
{
"type": "leaf",
"data": {
"views": [
"1"
],
"activeView": "1",
"id": "1"
},
"size": 638
},
{
"type": "branch",
"data": [
{
"type": "leaf",
"data": {
"views": [
"2"
],
"activeView": "2",
"id": "2"
},
"size": 95
},
{
"type": "leaf",
"data": {
"views": [
"3"
],
"activeView": "3",
"id": "3"
},
"size": 460
}
],
"size": 495
}
],
"size": 565
},
"width": 1143,
"height": 565,
"orientation": "HORIZONTAL"
},
"panel_ids": [
"1",
"2",
"3"
]
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/testthat/_snaps/update-dock_view/update-theme-001_.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/testthat/_snaps/update-dock_view/update-theme-002_.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading