convert_files {basepenguins} | R Documentation |
Convert files to use datasets versions of penguins and penguins_raw
Description
These functions convert files that use the
palmerpenguins package
to use the versions of penguins
and penguins_raw
included in the datasets
package in R >= 4.5.0. They removes calls to library(palmerpenguins)
and make
necessary changes to some variable names (see Details section below).
Usage
convert_files(input, output, extensions = c("R", "r", "qmd", "rmd", "Rmd"))
convert_files_inplace(input, extensions = c("R", "r", "qmd", "rmd", "Rmd"))
convert_dir(input, output, extensions = c("R", "r", "qmd", "rmd", "Rmd"))
convert_dir_inplace(input, extensions = c("R", "r", "qmd", "rmd", "Rmd"))
Arguments
input |
For |
output |
For |
extensions |
A character vector of file extensions to process, defaults to R scripts and R Markdown and Quarto documents. |
Details
Files are converted by:
Replacing
library(palmerpenguins)
with the empty string""
Replacing
data("penguins", package = "palmerpenguins")
withdata("penguins", package = "datasets")
Replacing variable names:
-
bill_length_mm
->bill_len
-
bill_depth_mm
->bill_dep
-
flipper_length_mm
->flipper_len
-
body_mass_g
->body_mass
-
Replacing
ends_with("_mm")
withstarts_with("flipper_"), starts_with"("bill_")
Non-convertible files (those without the specified extensions) are copied to
the output location if output
is provided, but are not modified.
If the output
files or directory do not (yet) exist, they will be created
(recursively if necessary).
Replacing ends_with("_mm")
with starts_with("flipper_"), starts_with("bill_")
ensures that modified R code will always run. starts_with("flipper_")
isn't
intuitively necessary, as there is only one variable starting with "flipper_",
in penguins
, but this code will not error inside dplyr::(select)
, even if
flipper_len
isn't in the data frame (trying to select flipper_len
directly will cause an error if that column isn't in the data frame).
In an educational context, we suggest manually editing the converted files to
replace starts_with("flipper_")
to flipper_len
if appropriate.
To facilitate this, the functions documented here produce a message
indicating the files and line numbers where the ends_with("_mm")
substitution was made.
Value
A list (returned invisibly) with two components:
-
changed
: A character vector of paths for files that were modified. -
not_changed
: A character vector of paths for files that were not modified. Files are not changed if they do not load the palmerpenguins package vialibrary(palmerpenguins)
,library('palmerpenguins')
orlibrary("palmerpenguins")
, or if they do not have one of the specifiedextensions
.
For both the changed
and not_changed
vectors, these will be subsets of
the output
paths, if they were provided, with the corresponding input
paths as names. If output
was not specified, then these vectors will be
subsets of the input
paths, and the vectors will not be named.
See Also
Examples
# Note that all examples below write output to a temporary directory
# and file paths are relative to that directory.
# For all examples below, use a copy of the examples provided by the package,
# copied to an "examples" directory in the working directory
example_dir("examples")
# Single file - new output
result <- convert_files("examples/penguins.R", "penguins_new.R")
cat(readLines("penguins_new.R"), sep = "\n") # view changes
# Single file - copy, then modify that in place
file.copy("examples/penguins.R", "penguins_copy.R")
convert_files_inplace("penguins_copy.R")
# Multiple files - new output locations
input_files <- c("examples/penguins.R", "examples/nested/penguins.qmd")
output_files <- output_paths(input_files, dir = "new_dir")
convert_files(input_files, output_files)
# Directory - new output location
result <- convert_dir("examples", "new_directory")
result # see `changed` and `not_changed` files
# Overwrite the files in "examples"
result <- convert_dir_inplace("examples")
result # see `changed` and `not_changed` files