Title: | Add Forms to your 'Shiny' App |
---|---|
Description: | Allows to create modular, reusable 'HTML' forms which can be embedded in your 'shiny' app with minimal effort. Features include conditional code execution on form submission, automatic input validation and input tooltips. |
Authors: | Piotr Bajger <[email protected]> |
Maintainer: | Piotr Bajger <[email protected]> |
License: | GPL-3 |
Version: | 0.0.1 |
Built: | 2025-02-23 03:31:28 UTC |
Source: | https://github.com/piotrbajger/shinyreforms |
Internal function which adds a shinyreforms pop-up with help text to a shiny inputTag. The help text is a div which gets appended to the label for the given input.
addHelpText(tag, helpText, updated = FALSE)
addHelpText(tag, helpText, updated = FALSE)
tag |
A tag to be modified. |
helpText |
Help text to be added. |
updated |
An internal parameter which is used in recurrent calls to the function. |
A modified Shiny tag with a shinyreforms help icon.
addHelpText( shiny::textInput("text_input", label = "Label"), helpText = "Tooltip" )
addHelpText( shiny::textInput("text_input", label = "Label"), helpText = "Tooltip" )
Appends a validation suffix to a string.
addValidationSuffix(tagId)
addValidationSuffix(tagId)
tagId |
ID of the tag. |
A string '"tagId-shinyreforms-validation"'.
Creates a shinyreforms help icon and pop-up.
createHelpIcon(helpText)
createHelpIcon(helpText)
helpText |
A tooltip to be displayed. |
A shiny div with an icon and pop-up tooltip.
Returns an ID of an input tag.
getInputId(inputTag)
getInputId(inputTag)
inputTag |
A shiny tag to retrieve the ID from. |
ID of an input tag.
ShinyForm can be used to include forms in your website. Create
a ShinyForm
object anywhere in your application by
defining all the inputs (possibly adding validators) and by
specifying callback onSuccess
and onError
functions.
Parameters onSuccess
and onError
passed to the constructor
should be functions with signatures function(self, input, output)
,
where 'self' will refer to the form itself, while input
and
output
will be the usual Shiny objects.
id
Unique form id which can be used with Shiny input.
elements
A list of ShinyForm input elements.
onSuccess
A function with to be run on valid submission, see details.
onError
A function with to be run on invalid submission, see details.
submit
A submit Action button/link.
new()
Initialises a ShinyForm.
ShinyForm$new(id, submit, onSuccess, onError, ...)
id
Unique form identifier.
submit
Submit button label.
onSuccess
Function to be ran on successful validation.
onError
Function to be ran on unsuccesful validation.
...
A list of validated Shiny inputs.
ui()
Returns the form's UI. To be used inside your App's UI.
ShinyForm$ui()
server()
Form logic. To be inserted into your App's server function.
Will validate form upon hitting the "Submit" button and run the 'onSuccess' or 'onError' function depending on whether the form is valid.
ShinyForm$server(input, output)
input
Shiny input.
output
Shiny output.
getValue()
Returns value of the input element with a given ID.
ShinyForm$getValue(input, inputId)
input
Shiny input.
inputId
ID of the input whose value is to be returned.
clone()
The objects of this class are cloneable with this method.
ShinyForm$clone(deep = FALSE)
deep
Whether to make a deep clone.
Constructs a shinyreforms dependency.
shinyReformsDependency()
shinyReformsDependency()
A list containing the shinyreforms style dependency.
Adds a shinyreforms dependency to a tag object.
shinyReformsPage(htmlTag)
shinyReformsPage(htmlTag)
htmlTag |
A shiny HTML tag. |
The input htmlTag with the shinyreforms.css dependency added.
if(interactive()){ shinyReformsPage(shiny::fluidPage(...)) }
if(interactive()){ shinyReformsPage(shiny::fluidPage(...)) }
Use to create shiny input tags with validation. This should only be used in ShinyForm constructor.
validatedInput(tag, helpText = NULL, validators = c())
validatedInput(tag, helpText = NULL, validators = c())
tag |
Tag to be modified. |
helpText |
Tooltip text. If NULL, no tooltip will be added. |
validators |
A vector of 'Validator' objects. |
The Shiny tag receives an additional attribute 'validators' which is a vector of 'Validator' objects.
A modified shiny input tag with attached validators and an optional tooltip div.
shinyreforms::validatedInput( shiny::textInput("text_input", label = "Username"), helpText = "Username must have length between 4 and 12 characters.", validators = c( shinyreforms::ValidatorMinLength(4), shinyreforms::ValidatorMaxLength(12) ) )
shinyreforms::validatedInput( shiny::textInput("text_input", label = "Username"), helpText = "Username must have length between 4 and 12 characters.", validators = c( shinyreforms::ValidatorMinLength(4), shinyreforms::ValidatorMaxLength(12) ) )
Validators are used to validate input fields in a ShinyForm. Validators are to be used with the validatedInput function. A single input field can have several validators.
Package shinyreformss defines a set of commonly used pre-defined Validators. These include:
Will fail if string is shorter than minLength.
Will fail if string is longer than maxLength.
Will fail if string is empty.
test
Function returning a boolean value which will be used to validate input.
failMessage
Error message to display when validation fails.
new()
Creates a Validator object.
Validator$new(test, failMessage)
test
A function to test the input. Should take a single value as input and return a boolean.
failMessage
A fail message to be displayed.
check()
Performs a check on the input.
Validator$check(value)
value
Input value to be tested.
TRUE if the check passes, FALSE if otherwise.
clone()
The objects of this class are cloneable with this method.
Validator$clone(deep = FALSE)
deep
Whether to make a deep clone.
if(interactive()){ Validator(function(value) { ... }, "Validation failed!") }
if(interactive()){ Validator(function(value) { ... }, "Validation failed!") }
Will return TRUE for strings longer than the maximum value.
ValidatorMaxLength(maxLength)
ValidatorMaxLength(maxLength)
maxLength |
Maximum length of the input. |
A Validator
checking that the input value does not exceed
maxLength
.
Will return TRUE for strings longer than the minimum value.
ValidatorMinLength(minLength)
ValidatorMinLength(minLength)
minLength |
Minimum length of the input. |
A Validator
checking that the input value is of length at
least minLength
.
The validator will return FALSE if the input is NULL, an empty vector, or an empty string ("") and FALSE otherwise.
ValidatorNonEmpty()
ValidatorNonEmpty()
A Validator
to check if an input is non-empty.
Will return FALSE if the input value is FALSE (e.g. like for an unchecked textbox.)
ValidatorRequired()
ValidatorRequired()
A validator which checks that the value is present.