Skip to contents

Components represent the encapsulation of a shiny module's logic and UI into a singular object, and can be used like any other shiny tag.

For more information on the data, methods, template, and styles arguments, see related documentation.

Usage

component(
  name = NULL,
  data = NULL,
  methods = NULL,
  template = NULL,
  styles = NULL
)

Arguments

name

component name

data

a function that returns a named list of values, which are used to store the component's state

methods

named list of functions, which define the behavior of the component

template

function that returns a taglist

styles

function that returns a character vector or list of CSS styles that are scoped to the component

Examples

library(shiny)
counter <- component(
    name = "counter",
    data = function() {
        list(
            label = "Counter",
            count = reactiveVal(0L)
        )
    },
    template = function(ns) {
        tagList(
            actionButton(ns("button"), label = self$label),
            verbatimTextOutput(ns("out"))
        )
    },
    methods = list(
        setup = function(input, output, session) {
            observeEvent(input$button, {
                self$count(
                    self$count() + 1L
                )
            })
            output$out <- renderText(
                self$count()
            )
        }
    )
)
tagList(
 tags$h1("Counter"),
 counter()
)
#> <h1>Counter</h1>
#> <button id="instance-U279244dN279244k-button" type="button" class="btn btn-default action-button">Counter</button>
#> <pre class="shiny-text-output noplaceholder" id="instance-U279244dN279244k-out"></pre>