A set of helper functions which serve the purpose of allowing users to assign information like sheetnames, titles, sources, metadata, and more to an object. These attributes are then used in place of the function arguments.

add_attribute(object, what, value)

# S3 method for default
add_attribute(object, what, value)

# S3 method for Content
add_attribute(object, what, value)

add_sheetname(object, value)

add_title(object, value)

add_source(object, value)

add_metadata(object, value)

add_grouplines(object, value)

add_group_names(object, value)

add_plot_size(object, value)

Arguments

object

The object to add an attribute to

value

A value

prefix

A prefix, default to NULL

collapse

Separator to collapse character vectors on, defaults to NULL

Details

add_sheetname: Adds a sheetname.

add_title: Adds a title

add_source: Adds a source. Behavior can be modified further via the prefix and collapse argument. Setting collapse to a value other than NULL will result in the input provided in value being concatenated.

add_metadata: Adds metadata. Behavior can be modified further via the prefix and collapse argument, just like with add_source.

add_grouplines: Adds group lines. Throws an error if input object is not a data.frame.

add_group_names: Adds group names. Throws an error if group lines have not been set.

add_plot_height: Adds plot height. Throws an error if input object is not a valid plot type.

add_plot_width: Adds plot width. Throws an error if input object is not a valid plot type.

add_plot_size: Assigns both height and width in one call. Expects a vector of length 2, with the first element corresponding to width, and the second to height.

Examples

library(dplyr)
#> 
#> Attaching package: ‘dplyr’
#> The following objects are masked from ‘package:stats’:
#> 
#>     filter, lag
#> The following objects are masked from ‘package:base’:
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)

# Load data and assign attributes in one pipeline
df <- mtcars %>%
  add_sheetname("Cars") %>%
  add_title("Motor Trend Car Road Tests") %>%
  add_source(
    c("Henderson and Velleman (1981),",
      "Building multiple regression models interactively.",
      "Biometrics, 37, 391–411.")) %>%
  add_metadata(
    c("The data was extracted from the 1974 Motor",
      "Trend US magazine and comprises fuel consumption",
      "and 10 aspects of automobile design and",
      "performance for 32 automobiles (1973–74 models)."))

# Create a plot and assign attributes in one pipeline
plt <- (ggplot(mtcars) +
  geom_histogram(aes(x = hp))) %>%
    add_sheetname("PS") %>%
    add_title("Histogram of horsepower") %>%
    add_plot_size(c(6, 3))

# Generate outputfile using minimal call
if (FALSE) {
datasetsXLSX(
  file = tempfile(fileext = ".xlsx"),
  datasets = list(df, plt))
}