Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[INFRA] Add goodpractice package #134

Closed
wants to merge 5 commits into from
Closed
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
9 changes: 9 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,15 @@ At this point, you're ready to make your changes! Feel free to ask for help. Eve
devtools::install()
```

### Install the `goodpractice` package
[`goodpractice`](https://github.com/mangothecat/goodpractice) is an R package used to have advice about good practices when building R packages.
It can be installed from the R console:
```r
install.packages('goodpractice')
library('goodpractice')
```
Then, follow the `goodpractice` documentation to execute the package.

### Regenerate the R documentation
The **R documentation** (.Rd) format is generated from the function comments thanks to [roxygen2](https://roxygen2.r-lib.org/). \
To regenerate this documentation after function comment updating, run this command:
Expand Down
123 changes: 62 additions & 61 deletions R/bpmnVisualization.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,52 +16,56 @@
#'
#' @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.
#' @param width Fixed width for widget (in css units). The default is \code{NULL}, which results in intelligent automatic sizing based on the widget's container.
#' @param height Fixed height for widget (in css units). The default is \code{NULL}, which results in intelligent automatic sizing based on the widget's container.
#' @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.
#' @param width Fixed width for widget (in css units).
#' The default is \code{NULL}, which results in intelligent automatic
#' sizing based on the widget's container.
#' @param height Fixed height for widget (in css units).
#' The default is \code{NULL}, which results in intelligent automatic
#' sizing based on the widget's container.
#' @param elementId The id of the HTML element to enclose the widget.
#' 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
#' including the R console, within R Markdown documents, and within Shiny output bindings.
#'
#' @examples
#' 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 including the R console,
#' within R Markdown documents, and within Shiny output bindings.
#'
#' @examples
#' # Load the BPMN file
#' bpmn_file <- system.file("examples/Order_Management.bpmn", package = "bpmnVisualization")
#'
#' 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,59 +80,56 @@ display <- function(
#' @title Shiny output binding for the \code{bpmn-visualization} HTML widget
#'
#' @name bpmnVisualization-shiny-output
#' @description
#' Helper to create output function for using the \code{bpmn-visualization} HTML widget within Shiny applications and interactive Rmd documents.
#'
#' @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.
#'
#' @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
#' 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
#' @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
#' is useful if you want to save an expression in a variable.
#'
#' @returns A render function that enables the use of the \code{bpmn-visualization} widget within Shiny applications.
#' @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)
}
54 changes: 21 additions & 33 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 @@ -57,14 +45,14 @@ build_bpmnContent <- function(
bpmnContent <- as.character(xml)
}
} else {
stop("bpmnXML must be a absolute path of BPMN file or the string of the BPMN content !!")
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
# In case the user passes a single parameter as overlay (instead of a list),
# we wrap it into a list, so the js can work.
x$overlays <- if (is.list(overlays[[1]])) {
overlays
} else {
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ It is made possible by [htmlwidgets](http://www.htmlwidgets.org/), which provide

## ♻️ Usage
### Installation
#### Install _devtools_
[Devtools](https://www.rdocumentation.org/packages/devtools) is an R package used to ease the installation and the development of other R packages.
#### Install `devtools`
[`devtools`](https://www.rdocumentation.org/packages/devtools) is an R package used to ease the installation and the development of other R packages.
It can be installed from the R console:
```r
install.packages('devtools')
library('devtools')
```

#### Install _BPMN Visualization - R Package_ from GitHub
#### Install `BPMN Visualization - R Package` from GitHub

To install a dedicated version (available versions can be found in the [GitHub releases page](https://github.com/process-analytics/bpmn-visualization-R/releases)), run:
```r
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-funs.R
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,4 @@ test_that("not_null_list works", {
res,
c("x")
)
})
})