Q & A / Troubleshooting / Tips"

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
`%+%` <- paste0
random_color <- \() {
  # colors <- c("lightblue", "orange", "lightpink")
  colors <- c("lightblue")
  "background:" %+% sample(colors, 1)
}
tag_style <- \() "padding:3px 6px; border-radius:4px;" %+% random_color()
div_style <- "margin: 10px 0px 20px 0px;"


tags <- \(...) {
  dots <- match.call(expand.dots = FALSE)$...
  dots |>
    lapply(\(x) htmltools::tags$span("#" %+% x, style = tag_style())) |>
    htmltools::tags$div(style = div_style)
}

Q. How do I use a static scale or a fixed range for an axis?

r tags(static, scale, axis)

A.


Q. How to style the frame in which the plot is made?

r tags(CSS, style, frame)

A. The syntax to use is:

device <- animate$new(width, height, attr = list(style = MY_STYLE))

where MY_STYLE can be:


Q. I heard there is a trick for developing animated plot in a code chunk of an R Markdown Document?

r tags(trick, 'inline', 'R Markdown Document', 'RMD', 'code chunk', workflow)

A. Yes, the function below is a handy trick to set up the device and render the output in a code chunk.

animate_it <- function(..., width = 600, height = 600, options = click_to_play()) {
  # Setup
  require(animate)     # 'require' is designed for use inside functions
  device <- animate$new(width, height, virtual = TRUE, 
                        attr = list(style = "border:1px solid lightgray"))
  attach(device)       # Make methods of the device available in the namespace
  pryr::f(...)()       # Main code
  rmd_animate(device, options = options)  # Embed animated plot in R Markdown Document
}

# Usage
animate_it({
  id <- new_id(1:10)
  plot(1:10, runif(10, 0, 1), id = id)
  plot(1:10, runif(10, 0, 1), id = id, transition = TRUE)
})

Q. Are there any tips to improve the workflow developing in the R console?

r tags(trick, 'R console', workflow)

A. Yes, I sometimes use the following for development in the R console.

setup <- function(width = 600, height = 600) {
  require(animate)
  device <- animate$new(width, height, attr = list(style = "border:1px solid lightgray"))
  attach(device)
}

cleanup <- function() {
  clear()
  off()
  detach(device)
}


# Usage 
setup()
id <- new_id(1:10)
plot(1:10, runif(10, 0, 1), id = id)
plot(1:10, runif(10, 0, 1), id = id, transition = TRUE)
cleanup()

Q. What does set_max_stacksize do? Why does the device need memory?

r tags(memory, 'advanced usage')

A. set_max_stacksize sets the cap of the internal memory of the device. Memory is needed when one wants to make an animated plot and then export it to a file. This is by default switched on, since the memory usage is generally low, and it makes more sense to have a plot ready to be exported when it is complete.




Try the animate package in your browser

Any scripts or data that you put into this service are public.

animate documentation built on Feb. 16, 2023, 10:57 p.m.