-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
314 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
|
||
#' Create a patchwork design | ||
#' | ||
#' @param grid_layout Layout of the GridStack retrieved via `input$<outputId>_layout`. | ||
#' | ||
#' @return An expression that can be evaluated to create a design usable in {patchwork}. | ||
#' @export | ||
#' | ||
#' @importFrom rlang expr %||% | ||
#' | ||
#' @example examples/patchwork-design.R | ||
create_design <- function(grid_layout) { | ||
if (is.null(grid_layout$children)) | ||
stop("grid_layout must be the value stored in `input$<outputId>_layout`") | ||
design <- lapply( | ||
X = grid_layout$children, | ||
FUN = function(x) { | ||
t <- x$y + 1 | ||
l <- x$x + 1 | ||
b <- x$y + x$h %||% 1 | ||
r <- x$x + x$w %||% 1 | ||
expr(area(!!t, !!l, !!b, !!r)) | ||
} | ||
) | ||
expr(c(!!!design)) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
utils::globalVariables(c( | ||
"area" # from patchwork | ||
)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
|
||
library(shiny) | ||
library(bslib) | ||
library(ggplot2) | ||
library(gridstackr) | ||
library(patchwork) | ||
|
||
ui <- page_fluid( | ||
tags$h2("Create {patchwork} design"), | ||
fluidRow( | ||
column( | ||
width = 6, | ||
tags$b("Create your design:"), | ||
gridstackOutput("grid") | ||
), | ||
column( | ||
width = 6, | ||
tags$b("Result:"), | ||
plotOutput("design") | ||
) | ||
) | ||
) | ||
|
||
server <- function(input, output, session) { | ||
|
||
output$grid <- renderGridstack({ | ||
gridstack( | ||
resize_handles = "se,e,s", | ||
gs_item( | ||
plotOutput("plot1", height = "100%") | ||
), | ||
gs_item( | ||
plotOutput("plot2", height = "100%") | ||
), | ||
gs_item( | ||
plotOutput("plot3", height = "100%") | ||
), | ||
gs_item( | ||
plotOutput("plot4", height = "100%") | ||
) | ||
) | ||
}) | ||
|
||
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)) | ||
}) | ||
|
||
output$design <- renderPlot({ | ||
req(input$grid_layout) | ||
mydesign <- create_design(input$grid_layout) | ||
plot(eval(mydesign)) | ||
}) | ||
|
||
} | ||
|
||
if (interactive()) | ||
shinyApp(ui, server) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.