To include a chart in Rmarkdown simply place the code in a code chunk/code fence.

g2(beaver1, asp(time, temp)) %>% 
  fig_line() %>% 
  fig_point(asp(shape = "circle"))

Show/Hide

One can also include “actions” in Rmarkdown documents, these are some elements of dynamism in static documents. These “actions” are tied to inputs, similar to shiny.

For instance, create an input button.

input_button(id = "visible", label = "Toggle visibility")

Then tie the input button (above) to the action_toggle_visibility on a graph: clicking the button shows or hides the plot below.

g2(mtcars, asp(mpg, qsec)) %>%
  fig_point() %>% 
  action_toggle_visibility("visible")

Change Data

One can also create a dropdown menu with input_select. Note that a chart supports multiple actions.

We can bind select input or radio buttons to action_select_data to allow dynamically changing the underlying dataset used in the graph.

generate <- function(n){
  data.frame(
    x = 1:100,
    y = runif(100, 1, n)
  )
}

Cars <- generate(30)
Flowers <- generate(50)

g2(Cars, asp(x, y)) %>%
  fig_point() %>% 
  action_select_data(
    "dataset", 
    datasets = list(
      Cars = Cars,
      Flowers = Flowers
    )
  ) %>% 
  action_select_data(
    "radio", 
    datasets = list(
      Cars = Cars,
      Flowers = Flowers
    )
  )
input_select(
  id = "dataset", 
  label = "Select a dataset",
  choices = c("Cars", "Flowers")
)
input_radio(
  "radio", 
  "Select Dataset", 
  c("Cars", "Flowers"), 
  selected = "Cars"
)
Select Dataset

Filter

One can also dynamically filter the data with either input_slider or input_select.

input_slider(
  id = "yFilter", 
  label = "Filter Y >", 
  value = 20, 
  min = 0, 
  max = 120, 
  step = 5
)

To filter the data, bind the action_filter_data to the chart.

The operator of the filter is combined with the value from the input and the asp to form a filter statement with the following template; asp operator inputValue.

For instance, below we create a filter on input id = "yFilter" on the column dist (of the cars dataset) with the operator > (greater than) will create the following filter pseudo statement: speed > yFilterValue.

g2(cars, asp(speed, dist)) %>% 
  fig_point() %>% 
  fig_smooth() %>% 
  action_filter_data(
    input = "yFilter",
    asp = dist,
    operator = ">"
  )

The action_filter_data can also be bound to an input_select.

input_select(
  "grp", 
  "Select a group",
  c("Group A", "Group B", "Group C")
)

Note the use of the == operator in this case.

df <- data.frame(
  x = rep(1:100, each = 3),
  y = rnorm(300, rep(c(4,5,2), each = 100)),
  group = rep(c("Group A", "Group B", "Group C"), each = 100)
)

g2(df, asp(x, y)) %>% 
  fig_point() %>% 
  fig_smooth() %>% 
  gauge_x_linear(min = 0, max = 100) %>%
  gauge_y_linear(min = -3, max = 10) %>%  
  action_filter_data(
    "grp",
    group,
    operator = "=="
  )

Action

Warning

This feature is experimental and likely unstable.

One can also use g2_action in a manner akin to g2_proxy: to dynamically add or update figures and other aspects of the chart.

g2(mtcars, asp(mpg, qsec), elementId = "plotty") %>% 
  fig_point(asp(shape = "square")) 
input_button("update", "Update chart")

Like g2_proxy, g2_action takes the id of the plot it must change as first argument.

g2_action("plotty", "update", asp(mpg), data = mtcars) %>% 
  fig_point(asp(y = drat, shape = "triangle")) %>% 
  fig_point(asp(y = wt)) %>% 
  render()

JavaScript

Note that those events are always fired and can thus be used outside of Shiny: clicking a point on the chart below shows an alert.

alert <- htmlwidgets::JS(
  "(ev) => {
    alert('You clicked a point with speed:' + ev.data.data.speed)
  }"
)

g2(cars, asp(speed, dist)) %>% 
  fig_point() %>% 
  capture_event("point:click", alert)