Title: | React Helpers |
---|---|
Description: | Make it easy to use 'React' in R with 'htmlwidget' scaffolds, helper dependency functions, an embedded 'Babel' 'transpiler', and examples. |
Authors: | Facebook Inc [aut, cph] (React library in lib, https://reactjs.org/; see AUTHORS for full list of contributors), Michel Weststrate [aut, cph] (mobx library in lib, https://github.com/mobxjs), Kent Russell [aut, cre] (R interface), Alan Dipert [aut] (R interface), Greg Lin [aut] (R interface) |
Maintainer: | Kent Russell <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.6.1 |
Built: | 2024-11-13 05:44:45 UTC |
Source: | https://github.com/react-r/reactr |
Helper function to use V8
with Babel
so we can
avoid a JSX transformer with using reactR
.
babel_transform(code = "")
babel_transform(code = "")
code |
|
transformed character
## Not run: library(reactR) babel_transform('<div>react div</div>') ## End(Not run)
## Not run: library(reactR) babel_transform('<div>react div</div>') ## End(Not run)
Create a React component
component(name, varArgs = list())
component(name, varArgs = list())
name |
Name of the React component, which must start with an upper-case character. |
varArgs |
Attributes and children of the element to pass along to
|
An htmltools tag
object
component("ParentComponent", list( x = 1, y = 2, component("ChildComponent"), component("OtherChildComponent") ) )
component("ParentComponent", list( x = 1, y = 2, component("ChildComponent"), component("OtherChildComponent") ) )
Create a React-based input
createReactShinyInput( inputId, class, dependencies, default = NULL, configuration = list(), container = htmltools::tags$div )
createReactShinyInput( inputId, class, dependencies, default = NULL, configuration = list(), container = htmltools::tags$div )
inputId |
The |
class |
Space-delimited list of CSS class names that should identify this input type in the browser. |
dependencies |
HTML dependencies to include in addition to those supporting React. Must contain at least one dependency, that of the input's implementation. |
default |
Initial value. |
configuration |
Static configuration data. |
container |
Function to generate an HTML element to contain the input. |
Shiny input suitable for inclusion in a UI.
myInput <- function(inputId, default = "") { # The value of createReactShinyInput should be returned from input constructor functions. createReactShinyInput( inputId, "myinput", # At least one htmlDependency must be provided -- the JavaScript implementation of the input. htmlDependency( name = "my-input", version = "1.0.0", src = "www/mypackage/myinput", package = "mypackage", script = "myinput.js" ), default ) }
myInput <- function(inputId, default = "") { # The value of createReactShinyInput should be returned from input constructor functions. createReactShinyInput( inputId, "myinput", # At least one htmlDependency must be provided -- the JavaScript implementation of the input. htmlDependency( name = "my-input", version = "1.0.0", src = "www/mypackage/myinput", package = "mypackage", script = "myinput.js" ), default ) }
Add this first for 'React' to work in RStudio Viewer.
html_dependency_corejs()
html_dependency_corejs()
Add JavaScript 'mobx' and 'mobx-react' dependency. When using with 'react', the order
of the dependencies is important, so please add html_dependency_react()
before
html_dependency_mobx()
.
html_dependency_mobx(react = TRUE)
html_dependency_mobx(react = TRUE)
react |
|
if(interactive()) { library(htmltools) library(reactR) browsable( tagList( html_dependency_mobx(react = FALSE), div(id="test"), tags$script(HTML( " var obs = mobx.observable({val: null}) mobx.autorun(function() { document.querySelector('#test').innerText = obs.val }) setInterval( function() {obs.val++}, 1000 ) " )) ) ) } ## Not run: # use with react library(htmltools) library(reactR) browsable( tagList( html_dependency_react(), html_dependency_mobx(), div(id="test"), tags$script(HTML(babel_transform( " var obs = mobx.observable({val: null}) var App = mobxReact.observer((props) => <div>{props.obs.val}</div>) ReactDOM.render(<App obs = {obs}/>, document.querySelector('#test')) setInterval( function() {obs.val++}, 1000 ) " ))) ) ) ## End(Not run)
if(interactive()) { library(htmltools) library(reactR) browsable( tagList( html_dependency_mobx(react = FALSE), div(id="test"), tags$script(HTML( " var obs = mobx.observable({val: null}) mobx.autorun(function() { document.querySelector('#test').innerText = obs.val }) setInterval( function() {obs.val++}, 1000 ) " )) ) ) } ## Not run: # use with react library(htmltools) library(reactR) browsable( tagList( html_dependency_react(), html_dependency_mobx(), div(id="test"), tags$script(HTML(babel_transform( " var obs = mobx.observable({val: null}) var App = mobxReact.observer((props) => <div>{props.obs.val}</div>) ReactDOM.render(<App obs = {obs}/>, document.querySelector('#test')) setInterval( function() {obs.val++}, 1000 ) " ))) ) ) ## End(Not run)
Add JavaScript 'React' dependency. For this to work in RStudio Viewer, also include
html_dependency_corejs
.
html_dependency_react(offline = TRUE)
html_dependency_react(offline = TRUE)
offline |
|
library(reactR) library(htmltools) tagList( tags$script( " ReactDOM.render( React.createElement( 'h1', null, 'Powered by React' ), document.body ) " ), #add core-js first to work in RStudio Viewer html_dependency_corejs(), html_dependency_react() #offline=FALSE for CDN )
library(reactR) library(htmltools) tagList( tags$script( " ReactDOM.render( React.createElement( 'h1', null, 'Powered by React' ), document.body ) " ), #add core-js first to work in RStudio Viewer html_dependency_corejs(), html_dependency_react() #offline=FALSE for CDN )
Adds window.reactR.exposeComponents and window.reactR.hydrate
html_dependency_reacttools()
html_dependency_reacttools()
React
is a syntactically-convenient way to create instances of React
components that can be sent to the browser for display. It is a list for
which extract methods are defined, allowing
object creation syntax like React$MyComponent(x = 1)
where
MyComponent
is a React component you have exposed to Shiny in
JavaScript.
React
React
An object of class react_component_builder
of length 0.
Internally, the component
function is used to create the
component instance.
# Create an instance of ParentComponent with two children, # ChildComponent and OtherChildComponent. React$ParentComponent( x = 1, y = 2, React$ChildComponent(), React$OtherChildComponent() )
# Create an instance of ParentComponent with two children, # ChildComponent and OtherChildComponent. React$ParentComponent( x = 1, y = 2, React$ChildComponent(), React$OtherChildComponent() )
Tag lists as returned by htmltools tagList
are not currently
supported.
reactMarkup(tag)
reactMarkup(tag)
tag |
character vector or React component or
|
A reactR markup object suitable for being passed to
createWidget
as widget instance data.
Add the minimal code required to implement a React.js-based Shiny input to an R package.
scaffoldReactShinyInput(name, npmPkgs = NULL, edit = interactive())
scaffoldReactShinyInput(name, npmPkgs = NULL, edit = interactive())
name |
Name of input |
npmPkgs |
Optional NPM packages upon which
this input is based which will be used to populate |
edit |
Automatically open the input's source files after creating the scaffolding. |
This function must be executed from the root directory of the package you wish to add the input to.
Add the minimal code required to implement a React.js-based HTML widget to an R package.
scaffoldReactWidget(name, npmPkgs = NULL, edit = interactive())
scaffoldReactWidget(name, npmPkgs = NULL, edit = interactive())
name |
Name of widget |
npmPkgs |
Optional NPM packages upon which
this widget is based which will be used to populate |
edit |
Automatically open the widget's JavaScript source file after creating the scaffolding. |
This function must be executed from the root directory of the package you wish to add the widget to.