as.treedf: Coerce to a Tree Data Frame (treedf)

Description Usage Arguments Details Examples

View source: R/treedf.R

Description

Functions to check if an object is a treedf, or coerce it if possible.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
as.treedf(x, ...)

## S3 method for class 'data.frame'
as.treedf(x, pathName = "pathString",
  pathDelimiter = "/", ...)

## S3 method for class 'Node'
as.treedf(x, ...)

is.treedf(x)

Arguments

x

An R object.

...

Passed to as.Node.data.frame when used with a data.frame that contains pathName; ignored otherwise.

pathName

The name of the column containing the path of the current node. If this column exists in x, then it defines the structure of the tree, the data.frame is converted into a tree through as.Node.data.frame, and the result is turned into a treedf. If the column does not exist, then x is supposed to already have the structure of a treedf.

pathDelimiter

The delimiter used to separate nodes in pathName.

Details

A treedf is a data.frame in which each line defines a node in a tree and is characterised by columns

id

the unique identifier of the node,

parent_id

the identifier of the parent of the node,

name

the (displayed) name of the node (optional),

selected

logical, if TRUE, the node will be selected in checkboxTreeInput (optional),

opened

logical, if TRUE, the node will be opened in checkboxTreeInput (optional).

The structure of the tree is therefore defined by the relationship between id and parent_id.

Conversion functions are provided for various tree structures available in R. If the structure of the tree is simply defined by different columns of a data.frame, one per level (e.g. organisation, department, team or family, genus, species), join them in pathString and use this argument to convert the data.frame into a treedf (see examples).

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# definition of a tree through id and parent_id
x <- data.frame(id=c(1, 2, 3, 4), parent_id=c(NA, 1, 2, 1))
ui <- fluidPage(checkboxTreeInput(inputId="tree", tree=x))
server <- function(input, output) { }
if (interactive()) shinyApp(ui, server)

# definition of a tree by levels
x <- data.frame(
       organisation=rep(c("R", "MATLAB"), each=3),
       department=c("code", "code", "doc", "code", "doc", "marketing"),
       team=c("core", "graphics", "help", "central", "help", "bosses")
     )
x$pathString <- paste(x$organisation, x$department, x$team, sep="/")
x <- as.treedf(x)
ui <- fluidPage(checkboxTreeInput(inputId="tree", tree=x))
if (interactive()) shinyApp(ui, server)

jiho/checkboxTreeInput documentation built on June 13, 2020, 10:29 a.m.