Skip to content

Commit

Permalink
Format code
Browse files Browse the repository at this point in the history
  • Loading branch information
csouchet committed Oct 27, 2022
1 parent b7ef21c commit e31f80b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 79 deletions.
84 changes: 36 additions & 48 deletions R/bpmnVisualization.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#'
#' @name display
#' @description Display BPMN diagram based on BPMN definition in XML format
#'
#'
#' @param bpmnXML A file name or xml document or string in BPMN XML format
#' @param overlays An element or a list of elements to be added to the diagram's existing elements.
#' Use overlay function to create an overlay object with content and relative position.
Expand All @@ -26,42 +26,38 @@
#' Use an explicit element ID for the widget (rather than an automatically
#' generated one). Useful if you have other JavaScript that needs to explicitly
#' discover and interact with a specific widget instance.
#'
#' @returns A \code{bpmn-visualization} Widget that will intelligently print itself into HTML in a variety of contexts
#'
#' @returns A \code{bpmn-visualization} Widget that will intelligently print itself into HTML in a variety of contexts
#' including the R console, within R Markdown documents, and within Shiny output bindings.
#'
#' @examples
#'
#' @examples
#' # Load the BPMN file
#' bpmn_file <- system.file("examples/Order_Management.bpmn", package = "bpmnVisualization")
#'
#'
#' # Display the BPMN diagram
#' display(bpmn_file, width='auto', height='auto')
#'
#'
#' # Display the BPMN diagram with overlays
#' overlays <- list(
#' create_overlay("start_event_1_1", "42"),
#' create_overlay("sequence_flow_1_1", "42"),
#' create_overlay("task_1_1", "9")
#' )
#' display(bpmn_file, overlays, width='auto', height='auto')
#'
#'
#' @seealso \code{\link{create_overlay}} to create an overlay
#'
#' @import htmlwidgets
#' @import xml2
#'
#' @export
display <- function(
bpmnXML,
overlays = NULL,
width = NULL,
height = NULL,
elementId = NULL
) {
x <- build_bpmnContent(
bpmnXML,
overlays = overlays
)
display <- function(bpmnXML,
overlays = NULL,
width = NULL,
height = NULL,
elementId = NULL) {
x <- build_bpmnContent(bpmnXML,
overlays = overlays)
# create widget
htmlwidgets::createWidget(
name = "bpmnVisualization",
Expand All @@ -76,37 +72,33 @@ display <- function(
#' @title Shiny output binding for the \code{bpmn-visualization} HTML widget
#'
#' @name bpmnVisualization-shiny-output
#' @description
#' @description
#' Helper to create output function for using the \code{bpmn-visualization} HTML widget within Shiny applications and interactive Rmd documents.
#'
#'
#' @param outputId output variable to read from
#' @param width,height Must be a valid CSS unit (like \code{'100\%'},
#' \code{'400px'}, \code{'auto'}) or a number, which will be coerced to a
#' string and have \code{'px'} appended.
#'
#'
#' @returns An output function that enables the use of the \code{bpmn-visualization} widget within Shiny applications.
#'
#' @export
bpmnVisualizationOutput <- function(
outputId,
width = "100%",
height = "400px"
) {
htmlwidgets::shinyWidgetOutput(
outputId,
"bpmnVisualization",
width,
height,
package = "bpmnVisualization"
)
bpmnVisualizationOutput <- function(outputId,
width = "100%",
height = "400px") {
htmlwidgets::shinyWidgetOutput(outputId,
"bpmnVisualization",
width,
height,
package = "bpmnVisualization")
}

#' @title Shiny render binding for the \code{bpmn-visualization} HTML widget
#'
#'
#' @rdname bpmnVisualization-shiny-render
#' @description
#' @description
#' Helper to create render function for using the \code{bpmn-visualization} HTML widget within Shiny applications and interactive Rmd documents.
#'
#'
#' @param expr An expression that generates a \code{bpmn-visualization} HTML widget
#' @param env The environment in which to evaluate \code{expr}.
#' @param quoted Is \code{expr} a quoted expression (with \code{quote()})? This
Expand All @@ -115,20 +107,16 @@ bpmnVisualizationOutput <- function(
#' @returns A render function that enables the use of the \code{bpmn-visualization} widget within Shiny applications.
#'
#' @export
renderBpmnVisualization <- function(
expr,
env = parent.frame(),
quoted = FALSE
) {
renderBpmnVisualization <- function(expr,
env = parent.frame(),
quoted = FALSE) {
# Checking that shiny is installed
rlang::check_installed("shiny")
if (!quoted) {
expr <- substitute(expr)
} # force quoted
htmlwidgets::shinyRenderWidget(
expr,
bpmnVisualizationOutput,
env,
quoted = TRUE
)
htmlwidgets::shinyRenderWidget(expr,
bpmnVisualizationOutput,
env,
quoted = TRUE)
}
48 changes: 17 additions & 31 deletions R/funs.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#' @title Create an overlay
#'
#' @name create_overlay
#' @description
#' @description
#' An overlay can be added to existing elements in the diagram.
#'
#'
#' See \code{overlays} argument in the \code{\link{display}} function.
#'
#'
#' Use this structure to create correct overlay structure.
#'
#'
#' @param elementId The bpmn element id to which the overlay will be attached
#' @param label HTML element to use as an overlay
#'
Expand All @@ -16,10 +16,8 @@
#' @export
create_overlay <- function(elementId, label) {
ret <-
.not_null_list(
elementId = elementId,
label = label
)
.not_null_list(elementId = elementId,
label = label)
}

#' @description Internal fun to build the htmlwidget content
Expand All @@ -28,27 +26,17 @@ create_overlay <- function(elementId, label) {
#' @returns A list
#'
#' @noRd
build_bpmnContent <- function(
bpmnXML,
overlays = NULL
) {
build_bpmnContent <- function(bpmnXML,
overlays = NULL) {
# load bpmn content
if (inherits(
bpmnXML,
"xml_document"
)) {
if (inherits(bpmnXML,
"xml_document")) {
bpmnContent <- as.character(bpmnXML)
} else if (
inherits(
bpmnXML,
"character"
)) {
if (
substring(
bpmnXML,
1,
38
) == "<?xml version=\"1.0\" encoding=\"UTF-8\"?>") {
} else if (inherits(bpmnXML,
"character")) {
if (substring(bpmnXML,
1,
38) == "<?xml version=\"1.0\" encoding=\"UTF-8\"?>") {
# this must be a string corresponding to the BPMN content of a file
bpmnContent <- bpmnXML
} else {
Expand All @@ -59,10 +47,8 @@ build_bpmnContent <- function(
} else {
stop("bpmnXML must be a absolute path of BPMN file or the string of the BPMN content !!")
}
x <- list(
bpmnContent = bpmnContent
)

x <- list(bpmnContent = bpmnContent)

if (length(overlays)) {
# In case the user passes a single parameter as overlays (instead of a list), we wrap it into a list so the js can work
x$overlays <- if (is.list(overlays[[1]])) {
Expand Down

0 comments on commit e31f80b

Please sign in to comment.