Browse Source

Helper functions :

- get a raw object from its fullname
- get the printing name of an object
- get the fullname of an object from its base name and parent
- get the size of an object by its fullname
- get the type of an object by its fullname
master
Maxime Wack 3 years ago
parent
commit
0109c14de2
1 changed files with 112 additions and 0 deletions
  1. +112
    -0
      functions.Rmd

+ 112
- 0
functions.Rmd View File

@@ -4,6 +4,118 @@ title: R object browser functions

# Description

## Get object

Get an object from its full qualifying name (*fullname*).

```{r get object}
##' Get an object
##'
##' Get an object from its fullname
##' @title Get an object
##' @param fullname A string with the fullname of an object
##' @return The object
get_object <- function(fullname)
{
eval(parse(text = fullname))
}
```

## Get name

Get the printing name of an object given its identifier.
Names come in quoted if the object is named, or as an index number.
The return name is unquoted or surrounded with double brackets

```{r get name}
##' Get an object name
##'
##' Return the printing name of an object as given to print_object
##' @title
##' @param object A string with the name of an object within its parent
##' @return The name of the object or its index in brackets
get_name <- function(object)
{
if (substr(object, 1, 1) == "\"")
substr(object, 2, nchar(object) - 1)
else
paste0("[[", object, "]]")
}
```
## Get fullname

Get the fullname of an object given its identifier and fullname of the parent

```{r get fullname}
##' Get an object fullname
##'
##' Get the fullname of an object
##' by prepending its parent and indexing by name/position
##' @title Get fullname
##' @param object A string with the name of an object
##' @param parent A string with the name of the parent
##' @return The full object name
get_fullname <- function(object, parent)
{
if (parent == "")
fullname <- gsub("\"", "`", object)
else if (substr(parent, nchar(parent) - 1, nchar(parent)) == "::")
fullname <- paste0(parent, gsub("\"", "`", object))
else
fullname <- paste0(parent, "[[", object, "]]")

tryCatch(get_object(fullname),
error = function(e) fullname <<- gsub("::", ":::", fullname))

fullname
}
```

## Get type

Get the type / class / mode of an object from its fullname.

```{r get type}
##' Get type of an object
##'
##' Get the type of an object
##' @title Get object type
##' @param fullname A string with the fullname of an object
##' @return The type of the object
get_type <- function(fullname)
{
class(get_object(fullname))[[1]]
}
```

## Get size

Get the size of an object given its fullname.

```{r get size}
##' Get an object size
##'
##' Get the size of an object:
##' - list : length
##' - dataframe : nrow × length
##' -
##' @title
##' @param fullname A string with the fullname of an object
##' @param type The type of the object
##' @return The size of the object
get_size <- function(fullname, type)
{
object <- get_object(fullname)

switch(type,
"environment" = length(objects(object)),
"pkg" = length(objects(object)),
"data.frame" = paste0(ncol(object), " × ", nrow(object)),
"matrix" = paste0(ncol(object), " × ", nrow(object)),
"function" = length(formals(object)),
length(object))
}
```
## Print

### Package


Loading…
Cancel
Save