Skip to contents

Returns a securer::securer_tool() that evaluates R plotting code and saves the result to a file.

Usage

plot_tool(
  allowed_dirs,
  default_width = 8,
  default_height = 6,
  max_file_size = "5MB",
  max_calls = NULL,
  default_dpi = 150
)

Arguments

allowed_dirs

Character vector of directories the tool can write to.

default_width

Default plot width in inches. Default 8.

default_height

Default plot height in inches. Default 6.

max_file_size

Maximum output file size. Default "5MB".

max_calls

Maximum invocations. NULL means unlimited.

default_dpi

Default resolution in dots per inch for raster formats (png, jpg). Default 150.

Value

A securer_tool object.

Details

The plot tool evaluates R plotting code in a restricted environment. Before evaluation, an AST walk validates that only allowed functions are called, preventing arbitrary code execution. The following categories of functions are permitted:

  • Graphics: plot, lines, points, abline, hist, barplot, boxplot, curve, title, legend, axis, mtext, text, par, grid, segments, arrows, polygon, rect, symbols, pie, pairs, heatmap, image, contour, persp, stripchart, dotchart, stars, sunflowerplot, coplot, cdplot, fourfoldplot, mosaicplot, assocplot, smoothScatter, spineplot, stem

  • Helpers: mathematical functions (sqrt, log, exp, etc.), string functions (paste, sprintf, etc.), and statistical distributions (dnorm, rnorm, etc.)

  • Data manipulation: data.frame, list, matrix, lapply, sapply, subset, with, and others

  • Operators: arithmetic, comparison, and logical operators

  • Flow control: if, for, while, {, assignment

Supported output formats: png, pdf, svg, jpg/jpeg. The format is auto-detected from the file extension by default.

Examples

# \donttest{
plt <- plot_tool(allowed_dirs = tempdir())
# Basic scatter plot
plt@fn(
  path = file.path(tempdir(), "scatter.png"),
  plot_code = "plot(1:10, (1:10)^2, main = 'Example')"
)
#> $path
#> [1] "/tmp/Rtmp02mzrS/scatter.png"
#> 
#> $size
#> [1] 27235
#> 
#> $format
#> [1] "png"
#> 

# With custom dimensions and DPI
plt <- plot_tool(
  allowed_dirs = tempdir(),
  default_width = 10,
  default_height = 8,
  default_dpi = 300
)
# }