combineWidgets: Combine several interactive plots

Description Usage Arguments Details Value Examples

View source: R/combine_widgets.R

Description

This function combines different htmlwidgets in a unique view.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
combineWidgets(
  ...,
  list = NULL,
  nrow = NULL,
  ncol = NULL,
  title = NULL,
  rowsize = 1,
  colsize = 1,
  byrow = TRUE,
  titleCSS = "",
  header = NULL,
  footer = NULL,
  leftCol = NULL,
  rightCol = NULL,
  width = NULL,
  height = NULL
)

Arguments

...

htmlwidgets to combine. If this list contains objects that are not htmlwidgets, the function tries to convert them into a character string which is interpreted as html content.

list

Instead of directly passing htmlwidgets to the function, one can pass a list of htmlwidgets and objects coercible to character. In particular, it can be usefull if multiple htmlwidgets have been generated using a loop function like lapply.

nrow

Number of rows of the layout. If NULL, the function will automatically take a value such that are at least as many cells in the layout as the number of htmlwidgets.

ncol

Number of columns of the layout.If NULL, the function will automatically take a value such that are at least as many cells in the layout as the number of htmlwidgets.

title

Title of the view.

rowsize

This argument controls the relative size of each row. For instance, if the layout has two rows and rowsize = c(2,1), then the width of the first row will be twice the one of the second one. This argument is recycled to fit the number of rows.

colsize

Same as rowsize but for the height of the columns of the layout.

byrow

If TRUE, then the layout is filled by row. Else it is filled by column.

titleCSS

A character containing css properties to modify the appearance of the title of the view.

header

Content to display between the title and the combined widgets. It can be a single character string or html tags.

footer

Content to display under the combined widgets. It can be a single character string or html tags.

leftCol

Content to display on the left of the combined widgets. It can be a single character string or html tags.

rightCol

Content to display on the right the combined widgets. It can be a single character string or html tags.

width

Total width of the layout (optional, defaults to automatic sizing).

height

Total height of the layout (optional, defaults to automatic sizing).

Details

The function only allows table like layout : each row has the same number of columns and reciprocally. But it is possible to create more complex layout by nesting combined htmlwidgets. (see examples)

Value

A htmlwidget object of class combineWidget. Individual widgets are stored in element widgets and can be extracted or updated. This is useful when a function returns a combineWidgets object but user wants to keep only one widget or to update one of them (see examples).

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
if (require(plotly)) {
  data(iris)

  combineWidgets(title = "The Iris dataset",
    plot_ly(iris, x = ~Sepal.Length, type = "histogram", nbinsx = 20),
    plot_ly(iris, x = ~Sepal.Width, type = "histogram", nbinsx = 20),
    plot_ly(iris, x = ~Petal.Length, type = "histogram", nbinsx = 20),
    plot_ly(iris, x = ~Petal.Width, type = "histogram", nbinsx = 20)
  )

  # Create a more complex layout by nesting combinedWidgets
  combineWidgets(title = "The iris data set: sepals", ncol = 2, colsize = c(2,1),
    plot_ly(iris, x = ~Sepal.Length, y = ~Sepal.Width, type = "scatter",
            mode = "markers", color = ~Species),
    combineWidgets(
      plot_ly(iris, x = ~Sepal.Length, type = "histogram", nbinsx = 20),
      plot_ly(iris, x = ~Sepal.Width, type = "histogram", nbinsx = 20)
    )
  )

  # combineWidgets can also be used on a single widget to easily add to it a
  # title and a footer.
  require(shiny)
  comments <- tags$div(
    "Wow this plot is so ",
    tags$span("amazing!!", style = "color:red;font-size:36px")
  )

  combineWidgets(
    plot_ly(iris, x = ~Sepal.Length, type = "histogram", nbinsx = 20),
    title = "Distribution of Sepal Length",
    footer = comments
  )

  # It is also possible to combine htmlwidgets with text or other html elements
  myComment <- tags$div(
    style="height:100%;background-color:#eee;padding:10px;box-sizing:border-box",
    tags$h2("Comment"),
    tags$hr(),
    "Here is a very clever comment about the awesome graphics you just saw."
  )
  combineWidgets(
    plot_ly(iris, x = ~Sepal.Length, type = "histogram", nbinsx = 20),
    plot_ly(iris, x = ~Sepal.Width, type = "histogram", nbinsx = 20),
    plot_ly(iris, x = ~Petal.Length, type = "histogram", nbinsx = 20),
    myComment
  )

  # Updating individual widgets.
  myWidget <- combineWidgets(
    plot_ly(iris, x = ~Sepal.Length, type = "histogram", nbinsx = 20),
    plot_ly(iris, x = ~Sepal.Width, type = "histogram", nbinsx = 20),
    ncol = 2
  )
  myWidget


  myWidget$widgets[[1]] <- myWidget$widgets[[1]] %>%
    layout(title = "Histogram of Sepal Length")

  myWidget$widgets[[2]] <- myWidget$widgets[[2]] %>%
    layout(title = "Histogram of Sepal Width")

  myWidget


  # Instead of passing directly htmlwidgets to the function, one can pass
  # a list containing htmlwidgets. This is especially useful when the widgets
  # are generated using a loop function like "lapply" or "replicate".
  #
  # The following code generates a list of 12 histograms and use combineWidgets
  # to display them.
  samples <- replicate(12, plot_ly(x = rnorm(100), type = "histogram", nbinsx = 20),
                       simplify = FALSE)
  combineWidgets(list = samples, title = "12 samples of the same distribution")
}

manipulateWidget documentation built on Oct. 5, 2021, 9:10 a.m.