init: Create the server and UI function for the 'shiny' app

View source: R/init.R

initR Documentation

Create the server and UI function for the shiny app

Description

[Stable]

End-users: This is the most important function for you to start a teal app that is composed of teal modules.

Usage

init(
  data,
  modules,
  filter = teal_slices(),
  title = build_app_title(),
  header = tags$p(),
  footer = tags$p(),
  id = character(0)
)

Arguments

data

(teal_data or teal_data_module) For constructing the data object, refer to teal_data() and teal_data_module().

modules

(list or teal_modules or teal_module) nested list of teal_modules or teal_module objects or a single teal_modules or teal_module object. These are the specific output modules which will be displayed in the teal application. See modules() and module() for more details.

filter

(teal_slices) Specifies the initial filter using teal_slices().

title

(shiny.tag or character(1)) The browser window title. Defaults to a title "teal app" with the icon of NEST. Can be created using the build_app_title() or by passing a valid shiny.tag which is a head tag with title and link tag.

header

(shiny.tag or character(1)) The header of the app.

footer

(shiny.tag or character(1)) The footer of the app.

id

(character) Optional string specifying the shiny module id in cases it is used as a shiny module rather than a standalone shiny app. This is a legacy feature.

Details

When initializing the teal app, if datanames are not set for the teal_data object, defaults from the teal_data environment will be used.

Value

Named list with server and UI functions.

Examples

app <- init(
  data = teal_data(
    new_iris = transform(iris, id = seq_len(nrow(iris))),
    new_mtcars = transform(mtcars, id = seq_len(nrow(mtcars))),
    code = "
      new_iris <- transform(iris, id = seq_len(nrow(iris)))
      new_mtcars <- transform(mtcars, id = seq_len(nrow(mtcars)))
    "
  ),
  modules = modules(
    module(
      label = "data source",
      server = function(input, output, session, data) {},
      ui = function(id, ...) div(p("information about data source")),
      datanames = "all"
    ),
    example_module(label = "example teal module"),
    module(
      "Iris Sepal.Length histogram",
      server = function(input, output, session, data) {
        output$hist <- renderPlot(
          hist(data()[["new_iris"]]$Sepal.Length)
        )
      },
      ui = function(id, ...) {
        ns <- NS(id)
        plotOutput(ns("hist"))
      },
      datanames = "new_iris"
    )
  ),
  filter = teal_slices(
    teal_slice(dataname = "new_iris", varname = "Species"),
    teal_slice(dataname = "new_iris", varname = "Sepal.Length"),
    teal_slice(dataname = "new_mtcars", varname = "cyl"),
    exclude_varnames = list(new_iris = c("Sepal.Width", "Petal.Width")),
    module_specific = TRUE,
    mapping = list(
      `example teal module` = "new_iris Species",
      `Iris Sepal.Length histogram` = "new_iris Species",
      global_filters = "new_mtcars cyl"
    )
  ),
  title = "App title",
  header = tags$h1("Sample App"),
  footer = tags$p("Copyright 2017 - 2023")
)
if (interactive()) {
  shinyApp(app$ui, app$server)
}


teal documentation built on May 29, 2024, 10:11 a.m.