Title: | 'ShinyItemAnalysis' Modules Development Toolkit |
---|---|
Description: | A comprehensive suite of functions designed for constructing and managing 'ShinyItemAnalysis' modules, supplemented with detailed guides, ready-to-use templates, linters, and tests. This package allows developers to seamlessly create and integrate one or more modules into their existing packages or to start a new module project from scratch. |
Authors: | Jan Netik [cre, aut] , Patricia Martinkova [aut] |
Maintainer: | Jan Netik <[email protected]> |
License: | GPL (>= 3) |
Version: | 0.1.1 |
Built: | 2024-10-31 19:51:48 UTC |
Source: | CRAN |
This is the workhorse of {SIAtools}
package. The function checks if the
package is properly configured for SIA modules and provides immediate fixes
as needed. add_module()
automatically puts a correct entry in SIA Modules
Manifest of your package (which is created if not already present), and
prepares .R
file with the code template. Both files are automatically
opened for you by default.
add_module( name = "new_module", title = NULL, category = NULL, open = TRUE, prefix = "sm_", proj = curr_proj() )
add_module( name = "new_module", title = NULL, category = NULL, open = TRUE, prefix = "sm_", proj = curr_proj() )
name |
character, a name for the new SIA module. |
title |
character, new module's title. You can leave the default
|
category |
character, new module's category. The category dictates the
tab within the |
open |
Whether to open the manifest and module's source for interactive
editing. Defaults to |
prefix |
character, a prefix to denote SIA module. It's highly
recommended to stick with the default |
proj |
character, a path to the project. Defaults to current project. |
No return value. Called for the side effects.
Other module management functions:
get_modules()
,
preview_module()
,
remove_module()
## Not run: # add the module called "test" and edit the details later on in the YAML add_module("test") # specify the title and category at creation time add_module("test", title = "Test module", category = "Validity") ## End(Not run)
## Not run: # add the module called "test" and edit the details later on in the YAML add_module("test") # specify the title and category at creation time add_module("test", title = "Test module", category = "Validity") ## End(Not run)
The function is designed to be used primarily by RStudio "New Project Wizard". Create a new project by navigating through File > New Project > New Directory > ShinyItemAnalysis Module Project. See RStudio User Guide for the details.
create_module_project(path, ..., open = TRUE)
create_module_project(path, ..., open = TRUE)
path |
character, a path to the new module. Use |
... |
used by RStudio only |
open |
logical, whether to open the project in new RStudio session
after creation. Defaults to |
No return value. Called for the side effect.
if (interactive()) { # create a new SIA module project in the parent of your working directory create_module_project("../my_new_module") }
if (interactive()) { # create a new SIA module project in the parent of your working directory create_module_project("../my_new_module") }
This is a thin wrapper around usethis::proj_get()
that silences any messages.
curr_proj()
curr_proj()
The path to the current project.
Other helpers:
default_shiny_io_functions
,
edit_rstudio_shortcuts()
,
list_categories()
,
open_sm_manifest()
,
print.sm_manifest()
,
sia_head_tag()
## Not run: curr_proj() ## End(Not run)
## Not run: curr_proj() ## End(Not run)
{shiny}
input/output UI functions consulted by
module_namespace_linter()
A character vector of function names whose calls are inspected for ns()
omission. Name of each element denotes the original package. Please refer to
module_namespace_linter()
for more details.
default_shiny_io_functions
default_shiny_io_functions
An object of class character
of length 25.
Following functions are covered:
shiny::actionButton
shiny::actionLink
shiny::checkboxGroupInput
shiny::checkboxInput
shiny::dateInput
shiny::dateRangeInput
shiny::fileInput
shiny::numericInput
shiny::passwordInput
shiny::radioButtons
shiny::selectInput
shiny::sliderInput
shiny::textAreaInput
shiny::textInput
shiny::varSelectInput
shiny::dataTableOutput
shiny::tableOutput
shiny::uiOutput
shiny::htmlOutput
shiny::verbatimTextOutput
shiny::imageOutput
shiny::textOutput
shiny::plotOutput
plotly::plotlyOutput
DT::DTOutput
Other helpers:
curr_proj()
,
edit_rstudio_shortcuts()
,
list_categories()
,
open_sm_manifest()
,
print.sm_manifest()
,
sia_head_tag()
Shows a popup window with RStudio keyboard shortcuts. Applicable only in
RStudio and in interactive R
session.
edit_rstudio_shortcuts()
edit_rstudio_shortcuts()
You can quickly reach out solicited addin function by typing it in the
Filter...
box in the very top of the window. Then double click at the blank
space just next to the addin function name and press down desired key or key
combination. Apply the changes and from now on, just call the addin function
with one keystroke. For more details, navigate to RStudio documentation.
No return value. Called for side effect.
Other helpers:
curr_proj()
,
default_shiny_io_functions
,
list_categories()
,
open_sm_manifest()
,
print.sm_manifest()
,
sia_head_tag()
if (interactive()) { edit_rstudio_shortcuts() }
if (interactive()) { edit_rstudio_shortcuts() }
Returns a list with all modules for the current package as described in its
SIA Modules Manifest, which resides at /inst/sia/modules.yml
and is
generated with add_module()
calls. Can be formatted as a tibble
using the
respective print method.
get_modules(proj = curr_proj())
get_modules(proj = curr_proj())
proj |
character, a path to the project. Defaults to current project. |
A SIA Modules Manifest of class sm_manifest
. Inherits from a
list
.
Other module management functions:
add_module()
,
preview_module()
,
remove_module()
## Not run: get_modules() ## End(Not run)
## Not run: get_modules() ## End(Not run)
{shiny}
module UI functions for ns()
omissionThis is a simple wrapper of lintr::lint_package()
call using only the
module_namespace_linter()
from {SIAtools}
. See the
linter documentation for more details.
lint_ns(path = curr_proj(), ...)
lint_ns(path = curr_proj(), ...)
path |
character, path to the package root directory. Default is the current project’s directory. |
... |
Arguments passed on to
|
An object of class c("lints", "list"), each element of which is a "list" object.
Other linter-related functions:
module_namespace_linter()
## Not run: lint_ns() ## End(Not run)
## Not run: lint_ns() ## End(Not run)
Lists all available categories a SIA module can be placed in. SIA app will place the module with illegal category under "Modules".
list_categories()
list_categories()
A character vector.
Other helpers:
curr_proj()
,
default_shiny_io_functions
,
edit_rstudio_shortcuts()
,
open_sm_manifest()
,
print.sm_manifest()
,
sia_head_tag()
list_categories()
list_categories()
ns()
in inputId
and outputId
arguments of UI functions
in {shiny}
modulesA custom linter to be used by {lintr}
package. Checks the functions in a
module's UI part for any missing ns()
calls. These are often omitted when
working with the plain {shiny}
or SIA modules. More details follows below.
module_namespace_linter( io_funs = default_shiny_io_functions, io_args = c("inputId", "outputId"), ns_funs = c("ns", "NS") )
module_namespace_linter( io_funs = default_shiny_io_functions, io_args = c("inputId", "outputId"), ns_funs = c("ns", "NS") )
io_funs |
character, |
io_args |
character, arguments of UI functions to check. |
ns_funs |
character, function names that are considered valid in order
to "namespace" inputs' or outputs' IDs. Defaults to both |
The easiest way is to call lint_ns()
which is essentially a wrapper around:
lintr::lint_package(linters = module_namespace_linter())
Both calls use our linter for the whole package. However, note that only
module_namespace_linter
is considered. Using this custom linter with the
native ones is somewhat complicated, but not impossible. To the best of our
knowledge, the only place where the {lintr}
documentation mentions the
actual usage of external linters, is in
linters_with_tags() help page. According to that,
you can pass the following call to linters
argument in any supported
lintr::lint_*
function:
lintr::linters_with_tags( tags = NULL, packages = c("lintr", "SIAtools") )
That should select all linters available in both packages.
It is also possible to set up a configuration file that enables you to
shorten calls to {lintr}
functions, use RStudio Addins to lint an active
file, or even apply linters during continuous integration workflows, e.g., in
GitHub Actions or in Travis. To opt for that, create .lintr
file at your
package's root and fill in the following line:
linters: linters_with_tags(tags = NULL, packages = "SIAtools")
Then, you can use the provided addins or call lintr::lint_package()
to get
your modules checked.
By default, the linter looks for any inputId
or outputId
arguments of
{shiny}
's UI functions (such as numericInput or
plotOutput, respectively), and tests if the values
assigned to the arguments are all "namespaced", i.e., wrapped in ns()
function. This is crucial for inputs and outputs in the UI portion of a
module to match their counterparts in the server logic chunk.
Only {shiny}
UI calls that are inside of a tagList in a
function ("lambda" shorthand, \( )
, applies as well) are inspected. This is
because we don't want to cause false alarms for any "ordinary" {shiny}
apps
that aren't modules. All UI portions of modules are usually defined as
functions, and all input/output UI functions are inside a
tagList, so we opted for the this strategy to minimalize
false positive matches outside {shiny}
modules.
We look for any inputId
or outputId
arguments that are named as such. On
top of that, the ns()
omission is detected even if you call the function
without named arguments that would be evaluated as input or output IDs.
However, if you use partial matching (numericInput(inp = "input")
), the
actual input won't get linted, even though it should, as it is eventually
evaluated as inputId
. The same applies for arguments defined outside the
call and passed as a variable, e.g., inp <- "input"; numericInput(inputId = inp)
. That is tricky to catch in a static code analysis, which is employed
in this linter.
A linter
closure. To be used by {lintr}
only. See the first
example below.
linters for a complete list of linters available in lintr.
Other linter-related functions:
lint_ns()
# will produce lints lintr::lint( text = "module_ui <- function(id, imports, ...) { tagList( numericInput(inputId = \"input_id_without_ns\", ...) ) }", linter = module_namespace_linter() ) # is OK lintr::lint( text = "module_ui <- function(id, imports, ...) { tagList( numericInput(inputId = ns(\"input_id_with_ns\"), ...) ) }", linter = module_namespace_linter() )
# will produce lints lintr::lint( text = "module_ui <- function(id, imports, ...) { tagList( numericInput(inputId = \"input_id_without_ns\", ...) ) }", linter = module_namespace_linter() ) # is OK lintr::lint( text = "module_ui <- function(id, imports, ...) { tagList( numericInput(inputId = ns(\"input_id_with_ns\"), ...) ) }", linter = module_namespace_linter() )
Open SIA Modules Manifest for Editing
open_sm_manifest(proj = curr_proj())
open_sm_manifest(proj = curr_proj())
proj |
character, a path to the project. Defaults to current project. |
character, a path to SIA Module Manifest (invisibly).
Other helpers:
curr_proj()
,
default_shiny_io_functions
,
edit_rstudio_shortcuts()
,
list_categories()
,
print.sm_manifest()
,
sia_head_tag()
## Not run: open_sm_manifest() ## End(Not run)
## Not run: open_sm_manifest() ## End(Not run)
Previews a SIA module in a standalone development environment. See the details below.
preview_module( module_id = NULL, ui_imports = NULL, server_imports = NULL, ui_elements = sia_head_tag(), save_and_document = TRUE, load = TRUE, proj = curr_proj(), ... )
preview_module( module_id = NULL, ui_imports = NULL, server_imports = NULL, ui_elements = sia_head_tag(), save_and_document = TRUE, load = TRUE, proj = curr_proj(), ... )
module_id |
character, name of the module to preview (including the
prefix). If |
ui_imports |
list, UI objects exported from the |
server_imports |
list, reactive objects exported from the
|
ui_elements |
elements to include in |
save_and_document |
logical, whether to save all unsaved files (only available in RStudio)
and document the package. Defaults to |
load |
logical, whether to load your package
before running the module preview. Defaults to |
proj |
character, a path to the project. Defaults to current project. |
... |
Arguments passed on to
|
The function takes module's function bindings and puts (or evaluates) them
inside a bare bone shiny::shinyApp()
. By default, a customized head tag is
injected in order to mimic the "environment" of full {ShinyItemAnalysis}
app. See sia_head_tag()
for more details. Besides, a onSessionEnded
hook
is set to call shiny::stopApp()
after the client disconnects, so the
"process" is automatically quit after you close the preview in your browser
or RStudio viewer.
In order to use the function bindings, preview_module()
attempts to
load your package without the actual installation by
default.
Note that you have to install the package as usual for
{ShinyItemAnalysis}
to detect your modules.
{ShinyItemAnalysis}
appNote that this "emulated" preview environment is really meant to test the
basic UI layout and functionality and is not able to receive any object from
{ShinyItemAnalysis}
app. However, you can pass any object like
{ShinyItemAnalysis}
does to server_imports
argument manually. For further
details and examples, please refer to vignette("imports", "SIAtools")
vignette.
Shiny app object of class shiny.appobj
.
Other module management functions:
add_module()
,
get_modules()
,
remove_module()
if (interactive()) { preview_module() }
if (interactive()) { preview_module() }
Prints out the SIA Modules Manifest obtained through get_modules()
. By
default, a plain YAML content is returned, but you can also get a formatted
output in a tibble
, which is suitable for packages with large number of SIA
modules.
## S3 method for class 'sm_manifest' print(x, ..., as_tibble = FALSE)
## S3 method for class 'sm_manifest' print(x, ..., as_tibble = FALSE)
x |
sm_manifest object, i.e., an output of |
... |
Not used at the moment. |
as_tibble |
logical, print the manifest as a |
Called for side effects by default. Returns a tibble
if as_tibble
argument is set to TRUE
.
Other helpers:
curr_proj()
,
default_shiny_io_functions
,
edit_rstudio_shortcuts()
,
list_categories()
,
open_sm_manifest()
,
sia_head_tag()
Removes the given module from the SIA Module Manifest and deletes the
respective .R
file.
remove_module(module_id = NULL, proj = curr_proj())
remove_module(module_id = NULL, proj = curr_proj())
module_id |
character, name of the module to remove (including the
prefix). If |
proj |
character, a path to the project. Defaults to current project. |
No return value. Called for the side effect.
Other module management functions:
add_module()
,
get_modules()
,
preview_module()
## Not run: remove_module() ## End(Not run)
## Not run: remove_module() ## End(Not run)
This function is intended to be used for SIA module preview only, which is
done by default in preview_module()
. It provides a HTML head tag with a
math rendering facility provided by the
library, and some CSS rules to
format tables. If printed in the console, expect nothing to be shown.
sia_head_tag()
sia_head_tag()
HTML head tag of class shiny.tag
.
Other helpers:
curr_proj()
,
default_shiny_io_functions
,
edit_rstudio_shortcuts()
,
list_categories()
,
open_sm_manifest()
,
print.sm_manifest()