Title: | Tagging and Organizing Objects in R |
---|---|
Description: | Provides functions for attaching tags to R objects, searching for objects based on tags, and removing tags from objects. It also includes a function for removing all tags from an object, as well as a function for deleting all objects with a specific tag from the R environment. The package is useful for organizing and managing large collections of objects in R. |
Authors: | Joachim Zuckarelli [aut, cre] |
Maintainer: | Joachim Zuckarelli <[email protected]> |
License: | GPL-3 |
Version: | 1.0.1 |
Built: | 2025-02-19 02:42:05 UTC |
Source: | https://github.com/jsugarelli/tagr |
This function adds tags to an existing R object. The tags are stored as attributes of the object and can be later used to identify and manipulate the object. If the specified object does not exist in the specified environment, an error is thrown.
add_tags(x, ..., envir = parent.frame())
add_tags(x, ..., envir = parent.frame())
x |
The name of the object to tag (unquoted). |
... |
The tags to add to the object. |
envir |
The environment in which the object exists (defaults to the parent frame). |
No return value.
# create a vector and add some tags x <- c(1, 2, 3) add_tags(x, "foo", "bar")
# create a vector and add some tags x <- c(1, 2, 3) add_tags(x, "foo", "bar")
This function removes the 'tags' attribute from an object, effectively removing all tags attached to the object. If the object does not have a 'tags' attribute, the function simply returns without modifying the object.
clean_tags(obj, envir = parent.frame())
clean_tags(obj, envir = parent.frame())
obj |
The object to remove tags from |
envir |
The environment in which to look for the object |
No return value.
# create an object and add some tags my_vec <- c(1, 2, 3) add_tags(my_vec, "important", "numeric") # remove the tags from the object clean_tags(my_vec)
# create an object and add some tags my_vec <- c(1, 2, 3) add_tags(my_vec, "important", "numeric") # remove the tags from the object clean_tags(my_vec)
This function checks if an existing R object has specified tags. The tags are stored as attributes of the object. If the specified object does not exist in the specified environment, an error is thrown.
has_tag(x, ..., envir = parent.frame())
has_tag(x, ..., envir = parent.frame())
x |
The name of the object to check (unquoted). |
... |
The tags to check for. |
envir |
The environment in which the object exists (defaults to the parent frame). |
A logical value indimessageing whether the object has all of the specified tags.
# create a vector and add some tags x <- c(1, 2, 3) add_tags(x, "foo", "bar") # check if the vector has the specified tags has_tag(x, "foo", "bar")
# create a vector and add some tags x <- c(1, 2, 3) add_tags(x, "foo", "bar") # check if the vector has the specified tags has_tag(x, "foo", "bar")
This function lists all objects in a specified environment that have all of the specified tags. The tags are stored as attributes of the objects. If the only.tags argument is set to TRUE, only objects that have at least one tag are listed. If no objects with the specified tags are found, a message is printed to the console. If the specified environment does not exist, an error is thrown.
ls_bytag(..., only.tags = TRUE, envir = parent.frame())
ls_bytag(..., only.tags = TRUE, envir = parent.frame())
... |
The tags to filter by. |
only.tags |
Logical value indimessageing whether to include only objects that have at least one tag. |
envir |
The environment to search for tagged objects (defaults to the parent frame). |
No return value. A table with information about each tagged object, including its name, type, and tags is printed to the console.
# create some objects and add tags x <- c(1, 2, 3) y <- matrix(1:9, nrow = 3) z <- "hello world" add_tags(x, "foo") add_tags(y, "bar") add_tags(z, "baz") # list objects with specified tags ls_bytag("foo", "bar")
# create some objects and add tags x <- c(1, 2, 3) y <- matrix(1:9, nrow = 3) z <- "hello world" add_tags(x, "foo") add_tags(y, "bar") add_tags(z, "baz") # list objects with specified tags ls_bytag("foo", "bar")
Remove all objects in the current environment that have a specified tag.
rm_bytag( tag, envir = parent.frame(), confirm = getOption("tagsr.confirm", FALSE) )
rm_bytag( tag, envir = parent.frame(), confirm = getOption("tagsr.confirm", FALSE) )
tag |
The tag to search for. |
envir |
The environment to search in. Defaults to the current environment. |
confirm |
If |
No return value.
# create some objects with tags x <- 1:10 y <- matrix(rnorm(16), 4, 4) add_tags(x, "numbers") add_tags(y, "matrix") # remove all objects with the "numbers" tag rm_bytag("numbers") # remove all objects with the "matrix" tag without confirmation prompt set_confirm(FALSE) rm_bytag("matrix") # confirm that objects have been removed ls() # clean up rm(set_confirm, x, y)
# create some objects with tags x <- 1:10 y <- matrix(rnorm(16), 4, 4) add_tags(x, "numbers") add_tags(y, "matrix") # remove all objects with the "numbers" tag rm_bytag("numbers") # remove all objects with the "matrix" tag without confirmation prompt set_confirm(FALSE) rm_bytag("matrix") # confirm that objects have been removed ls() # clean up rm(set_confirm, x, y)
This function sets the confirmation prompt for the rm_bytag
function
when deleting objects with a specified tag.
set_confirm(confirm = NULL)
set_confirm(confirm = NULL)
confirm |
A logical value indimessageing whether to prompt for confirmation before deleting objects. If |
If confirm
is NULL
, the current setting for the confirmation prompt is returned. Otherwise, the confirmation prompt setting is updated.
# turn on confirmation prompt set_confirm(TRUE) # turn off confirmation prompt set_confirm(FALSE) # get current confirmation prompt setting set_confirm()
# turn on confirmation prompt set_confirm(TRUE) # turn off confirmation prompt set_confirm(FALSE) # get current confirmation prompt setting set_confirm()
This function retrieves the tags associated with a specified object.
tags(obj, envir = parent.frame())
tags(obj, envir = parent.frame())
obj |
The object to retrieve tags from. |
envir |
The environment in which the object exists. Defaults to the parent environment. |
Returns a sorted vector of tags associated with the object. If the object has no tags, the function prints a message and returns NULL.
# create a variable x <- 5 # add tags to the variable add_tags(x, "important", "numeric") # retrieve the tags tags(x)
# create a variable x <- 5 # add tags to the variable add_tags(x, "important", "numeric") # retrieve the tags tags(x)
This function removes one or more tags from an object. The tags to be removed can be specified as separate arguments or using the ellipsis ('...') syntax. Alternatively, setting 'all' to TRUE will remove all tags from the object. If the specified object does not have the specified tag(s), the function will throw an error. If 'all' is set to TRUE and the object has no tags, the function will do nothing.
untag(obj, ..., all = FALSE, envir = parent.frame())
untag(obj, ..., all = FALSE, envir = parent.frame())
obj |
an object in the current environment |
... |
one or more character strings specifying the tags to remove |
all |
a logical value indimessageing whether to remove all tags from the object |
envir |
the environment in which the object is defined |
No return value.
x <- 1:10 add_tags(x, "numbers", "positive") add_tags(x, "even") tags(x) # "even", "numbers", "positive" # Remove the "positive" tag from x untag(x, "positive") tags(x) # "even", "numbers" # Remove all tags from x untag(x, all = TRUE) tags(x) # "NULL"
x <- 1:10 add_tags(x, "numbers", "positive") add_tags(x, "even") tags(x) # "even", "numbers", "positive" # Remove the "positive" tag from x untag(x, "positive") tags(x) # "even", "numbers" # Remove all tags from x untag(x, all = TRUE) tags(x) # "NULL"