profvis {profvis}R Documentation

Profile an R expression and visualize profiling data

Description

This function will run an R expression with profiling, and then return an htmlwidget for interactively exploring the profiling data.

Usage

profvis(
  expr = NULL,
  interval = 0.01,
  prof_output = NULL,
  prof_input = NULL,
  width = NULL,
  height = NULL,
  split = c("h", "v"),
  torture = 0,
  simplify = TRUE,
  rerun = FALSE
)

Arguments

expr

Expression to profile. Not compatible with prof_input. The expression is repeatedly evaluated until 'Rprof()' produces an output. It can _be_ a quosure injected with [rlang::inject()] but it cannot _contain_ injected quosures.

interval

Interval for profiling samples, in seconds. Values less than 0.005 (5 ms) will probably not result in accurate timings

prof_output

Name of an Rprof output file or directory in which to save profiling data. If NULL (the default), a temporary file will be used and automatically removed when the function exits. For a directory, a random filename is used.

prof_input

The path to an Rprof data file. Not compatible with expr or prof_output.

width

Width of the htmlwidget.

height

Height of the htmlwidget

split

Direction of split. Either "v" (the default) for vertical, or "h" for horizontal. This is the orientation of the split bar.

torture

Triggers garbage collection after every torture memory allocation call.

Note that memory allocation is only approximate due to the nature of the sampling profiler and garbage collection: when garbage collection triggers, memory allocations will be attributed to different lines of code. Using torture = steps helps prevent this, by making R trigger garbage collection after every torture memory allocation step.

simplify

Whether to simplify the profiles by removing intervening frames caused by lazy evaluation. This only has an effect on R 4.0. See the filter.callframes argument of Rprof().

rerun

If 'TRUE', 'Rprof()' is run again with 'expr' until a profile is actually produced. This is useful for the cases where 'expr' returns too quickly, before R had time to sample a profile. Can also be a string containing a regexp to match profiles. In this case, 'profvis()' reruns 'expr' until the regexp matches the modal value of the profile stacks.

Details

An alternate way to use profvis is to separately capture the profiling data to a file using Rprof(), and then pass the path to the corresponding data file as the prof_input argument to profvis().

See Also

print.profvis for printing options.

Rprof for more information about how the profiling data is collected.

Examples

# Only run these examples in interactive R sessions
if (interactive()) {

# Profile some code
profvis({
  dat <- data.frame(
    x = rnorm(5e4),
    y = rnorm(5e4)
  )

  plot(x ~ y, data = dat)
  m <- lm(x ~ y, data = dat)
  abline(m, col = "red")
})


# Save a profile to an HTML file
p <- profvis({
  dat <- data.frame(
    x = rnorm(5e4),
    y = rnorm(5e4)
  )

  plot(x ~ y, data = dat)
  m <- lm(x ~ y, data = dat)
  abline(m, col = "red")
})
htmlwidgets::saveWidget(p, "profile.html")

# Can open in browser from R
browseURL("profile.html")

}

[Package profvis version 0.3.8 Index]