Title: | Miscellaneous Tools Set |
---|---|
Description: | A collection of miscellaneous methods to simplify various tasks, including plotting, data.frame and matrix transformations, environment functions, regular expression methods, and string and logical operations, as well as numerical and statistical tools. Most of the methods are simple but useful wrappers of common base R functions, which extend S3 generics or provide default values for important parameters. |
Authors: | Sven E. Templer |
Maintainer: | Sven E. Templer <[email protected]> |
License: | GPL-3 |
Version: | 1.1.0 |
Built: | 2025-02-16 04:47:13 UTC |
Source: | https://github.com/setempler/miscset |
A collection of miscellaneous methods to simplify various tasks, including plotting, data.frame and matrix transformations, environment functions, regular expression methods, and string and logical operations, as well as numerical and statistical tools. Most of the methods are simple but useful wrappers of common base R functions, which extend S3 generics or provide default values for important parameters.
The package vignette provides a comprehensive overview and examples for the
usage of all available functions in the package. View with
vignette("miscset")
.
Sven E. Templer
Create barplots of a list of numeric values and error bars according to the confidence interval, standard deviation, interquartile range, etc.
ciplot(x, ...) ## Default S3 method: ciplot(x, ..., ylim, height.fun = mean, height.args = list(), error.fun = confint, error.args = list(), arrows.args = list(code = 3, angle = 90), na.rm = TRUE)
ciplot(x, ...) ## Default S3 method: ciplot(x, ..., ylim, height.fun = mean, height.args = list(), error.fun = confint, error.args = list(), arrows.args = list(code = 3, angle = 90), na.rm = TRUE)
x |
List of numeric values |
... |
Arguments forwarded to barplot in default method. |
ylim |
A range for the y-axis limits. |
height.fun |
Function to apply on each list object to calculate the height of the bars from. |
height.args |
Arguments forwarded to height.fun, as a named list. |
error.fun |
Function to calculate the error size. See also details. |
error.args |
Arguments forwarded to error.fun, as a named list. |
arrows.args |
Arguments forwarded to arrows, as a named list. |
na.rm |
Logical, remove missing values. |
Example for quantiles:interquartile <- function(x) {quartile(x,.75)-mean(x)}
quantileQ <- function(x, q) {abs(quartile(x,q[1])-mean(x))}
Sven E. Templer
Collapse objects as in the paste
function.
collapse(x, sep, ...) ## Default S3 method: collapse(x, sep = "", ..., .unique = FALSE, .sort = FALSE, .decreasing = FALSE) ## S3 method for class 'data.frame' collapse(x, sep = "", by = names(x), ..., .unique = FALSE, .sort = FALSE, .decreasing = FALSE, .unlist = FALSE, .sortby = FALSE)
collapse(x, sep, ...) ## Default S3 method: collapse(x, sep = "", ..., .unique = FALSE, .sort = FALSE, .decreasing = FALSE) ## S3 method for class 'data.frame' collapse(x, sep = "", by = names(x), ..., .unique = FALSE, .sort = FALSE, .decreasing = FALSE, .unlist = FALSE, .sortby = FALSE)
x |
Any R object. |
sep |
A character string to separate value columns.
|
... |
Forwarded to or from other methods. |
.unique |
Logical, return only unique values. |
.sort |
Logical, sort the values. |
.decreasing |
Logical, if sorting, then by decreasing values. |
by |
Column names to split data frame by, before
applying collapse on each remaining column within each piece.
Using the default (all columns), then |
.unlist |
Logical, if value columns need to be unlisted before collapsing. |
.sortby |
Logical, sort the output on the |
For the data.frame
method, x
is converted to a
data.table
before applying the piece- and column-wise collapses. If
the input is already inheriting from data.table
, then the class is
retained..sortby
is causing setkeyv(x, by)
to be applied to x
after converting to a data.table
.
Sven E. Templer
# ### some data set.seed(12) s <- s2 <- sample(LETTERS[1:4], 9, replace = TRUE) s2[1:2] <- rev(s2[1:2]) d <- data.frame(group = rep(letters[c(3,1,2)], each = 3), value = s, level = factor(s2), stringsAsFactors = FALSE) ### collapse vectors collapse(letters) collapse(1:3) # coerced to character collapse(LETTERS[1:5], "-") # separated by '-' ### collapse data.frames # by all columns (same as unique) collapse(d) # by a grouping column collapse(d, by = 1) # by multiple, but not all columns collapse(d, by = c("group", "value")) # return single row collapse(d, by = 0) # return single row, unique and sorted values collapse(d, by = 0, .unique = TRUE, .sort = TRUE) #
# ### some data set.seed(12) s <- s2 <- sample(LETTERS[1:4], 9, replace = TRUE) s2[1:2] <- rev(s2[1:2]) d <- data.frame(group = rep(letters[c(3,1,2)], each = 3), value = s, level = factor(s2), stringsAsFactors = FALSE) ### collapse vectors collapse(letters) collapse(1:3) # coerced to character collapse(LETTERS[1:5], "-") # separated by '-' ### collapse data.frames # by all columns (same as unique) collapse(d) # by a grouping column collapse(d, by = 1) # by multiple, but not all columns collapse(d, by = c("group", "value")) # return single row collapse(d, by = 0) # return single row, unique and sorted values collapse(d, by = 0, .unique = TRUE, .sort = TRUE) #
Calculate confidence intervals for values of a numeric vector.
## S3 method for class 'numeric' confint(object, parm = qnorm, level = 0.95, ..., na.rm = TRUE, ret.attr = TRUE)
## S3 method for class 'numeric' confint(object, parm = qnorm, level = 0.95, ..., na.rm = TRUE, ret.attr = TRUE)
object |
A numeric vector. |
parm |
Function for quantile calculation.
e.g. |
level |
Size of confidence (0 < size < 1). |
... |
Unused. |
na.rm |
Logical, remove missing values for |
ret.attr |
Logical, to include the mean value and function arguments as attributes of the returned object. |
Returns a numeric vector with the lower and upper range of the confidence interval.
Sven E. Templer
# confint(1:3) confint(1:3, ret.attr = FALSE) #
# confint(1:3) confint(1:3, ret.attr = FALSE) #
Same as do.call(rbind, x)
, but adding a column with the
name of each table. Missing names are replaced by integers.
do.rbind(x, idcol = "Name", keep.rownames = FALSE)
do.rbind(x, idcol = "Name", keep.rownames = FALSE)
x |
List with data.frames. Non data.frame objects are dropped. |
idcol |
Name for column with ids in output. |
keep.rownames |
Logical, keep rownames. |
Returns a data.frame
Sven E. Templer
Determine duplicates. duplicates
returns a logical vector,
duplicatei
an integer vector.
duplicates(x) duplicatei(x, first = TRUE)
duplicates(x) duplicatei(x, first = TRUE)
x |
A vector or data.frame to search for duplicates. |
first |
Logical, |
duplicates
returns a logical vector as duplicated, but with
TRUE
values also for the first occurrence of duplicated values.duplicatei
returns the index of the first occurrence of each unique
value.
Sven E. Templer
# x <- c(7, 7, 7, 2, 3, 2) data.frame( data = x, duplicated = duplicated(x), duplicates = duplicates(x), duplicatei = duplicatei(x), duplicatei0 = duplicatei(x, FALSE)) #
# x <- c(7, 7, 7, 2, 3, 2) data.frame( data = x, duplicated = duplicated(x), duplicates = duplicates(x), duplicatei = duplicatei(x), duplicatei0 = duplicatei(x, FALSE)) #
Transform a matrix
or dist
object to a pairwise list.
enpaire(x, ...) ## Default S3 method: enpaire(x, ...) ## S3 method for class 'dist' enpaire(x, upper = T, lower = T, ...) ## S3 method for class 'matrix' enpaire(x, upper = T, lower = T, ...)
enpaire(x, ...) ## Default S3 method: enpaire(x, ...) ## S3 method for class 'dist' enpaire(x, upper = T, lower = T, ...) ## S3 method for class 'matrix' enpaire(x, upper = T, lower = T, ...)
x |
Object of class |
... |
Arguments passed to methods. |
upper |
Logical, return values from upper triangle. |
lower |
Logical, return values from lower triangle. |
Returns a data.frame
. The first and second column represent the
dimension names for a value in x
. The following columns contain
the values for the upper or lower triangle.
Sven E. Templer
# m <- matrix(letters[1:9], 3, 3, dimnames = list(1:3,1:3)) enpaire(m) enpaire(m, lower = FALSE) #
# m <- matrix(letters[1:9], 3, 3, dimnames = list(1:3,1:3)) enpaire(m) enpaire(m, lower = FALSE) #
Create a factor with NA
values included
and positioned as last level.
factorNA(x, ...)
factorNA(x, ...)
x |
A vector coerced to character. |
... |
Forwarded to factor. |
Sven E. Templer
Calculate HTML colour code from a palette like ggplot2 uses.
gghcl(n, sub = 1:n, h = c(0, 360) + 15, c = 100, l = 65, ...)
gghcl(n, sub = 1:n, h = c(0, 360) + 15, c = 100, l = 65, ...)
n |
Numeric value to determine size of palette. |
sub |
Numeric vector with values within range from |
h |
Hue of the colour. Within range of a circle's degrees. |
c |
Chroma of the colour. |
l |
Luminance of the colour. Within range from |
... |
Further arguments passed to function |
See ?hcl
for explanation of h
, c
and l
.
Returns a character vector containing HTML colour code of the
standard ggplot
colour palette.
Sven E. Templer
# # Plot some palettes: par(mfrow = c(3,1), mai = c(.1,.1,1,.1)) p <- matrix(1:10, 10) image(p, col = gghcl(5), axes = FALSE, main ="gghcl(5)") image(p, col = gghcl(10), axes = FALSE, main = "gghcl(10)") image(p, col = gghcl(10, 1:5), axes = FALSE, main ="gghcl(10, 1:5)") # dev.off() # to reset \code{par} #
# # Plot some palettes: par(mfrow = c(3,1), mai = c(.1,.1,1,.1)) p <- matrix(1:10, 10) image(p, col = gghcl(5), axes = FALSE, main ="gghcl(5)") image(p, col = gghcl(10), axes = FALSE, main = "gghcl(10)") image(p, col = gghcl(10, 1:5), axes = FALSE, main ="gghcl(10, 1:5)") # dev.off() # to reset \code{par} #
Arrange a list of ggplots with grid.arrange and
output on local graphic device or as pdf/png when a path is supplied.
ggplotGridA4
writes the plots to a DIN A4 (8 x 11 inches) pdf file
directly.
ggplotGrid(l, path, ncol = 1, nrow = 1, width = 8, height = 11, res = 300, pdf.cairo = TRUE, onefile = TRUE, ...) ggplotGridA4(l, path, ncol = 2, nrow = 1, wide = TRUE) ggplotlist(x, ncol = 1, path, width = 11, height = 8)
ggplotGrid(l, path, ncol = 1, nrow = 1, width = 8, height = 11, res = 300, pdf.cairo = TRUE, onefile = TRUE, ...) ggplotGridA4(l, path, ncol = 2, nrow = 1, wide = TRUE) ggplotlist(x, ncol = 1, path, width = 11, height = 8)
l |
List with ggplot objects. |
path |
Plot to file of type pdf or png. Determine type by
path ending (.pdf or .png). |
ncol |
Number of columns. |
nrow |
Number of rows per page, only for pdfs. |
width |
For pdfs/pngs the width in inches, else ignored. |
height |
For pdfs/pngs the height in inches, else ignored. |
res |
Resolution in dpi for pngs. |
pdf.cairo |
|
onefile |
Create one file, see cairo_pdf. |
... |
Forwarded to cairo_pdf |
wide |
Wide format pdf pages (11x8 inches). |
x |
A list containing at least one ggplot object of class |
Sven E. Templer
# ## Not run: library(ggplot2) d <- data.frame(a=1:5,b=1:5) x <- list( ggplot(d, aes(x=a,y=b,col=b)) + geom_line(), ggplot(d, aes(x=a,y=b,shape=factor(b))) + geom_point()) ggplotlist(x, 2) ## End(Not run) #
# ## Not run: library(ggplot2) d <- data.frame(a=1:5,b=1:5) x <- list( ggplot(d, aes(x=a,y=b,col=b)) + geom_line(), ggplot(d, aes(x=a,y=b,shape=factor(b))) + geom_point()) ggplotlist(x, 2) ## End(Not run) #
Function to extract a certain index from gregexpr()
.
gregexprind(pattern, text, n, ...)
gregexprind(pattern, text, n, ...)
pattern |
Character string containing a regular expression to be searched in |
text |
Character vector where the search is performed. |
n |
Numeric value or character string |
... |
Arguments passed to function |
Numeric vector of length length(text)
.
Sven E. Templer
See gregexpr for further information on arguments.
See regex for the use of regular expressions.
# gregexprind(c("a"),c("ababa","ab","xyz",NA), 1) gregexprind(c("a"),c("ababa","ab","xyz",NA), 2) gregexprind(c("a"),c("ababa","ab","xyz",NA), "last") #
# gregexprind(c("a"),c("ababa","ab","xyz",NA), 1) gregexprind(c("a"),c("ababa","ab","xyz",NA), 2) gregexprind(c("a"),c("ababa","ab","xyz",NA), "last") #
Given a package name or string, start the package help index page in a browser.
help.index(pkg, browser = NULL)
help.index(pkg, browser = NULL)
pkg |
A character string or expression with the name of a package. |
browser |
The browser to display. |
Sven E. Templer
Based on and enhancing devtools::session_info
.
info(..., width = 120)
info(..., width = 120)
... |
Forwarded to other methods. |
width |
Console width in columns. |
Sven E. Templer
info() devtools::session_info() sessionInfo()
info() devtools::session_info() sessionInfo()
Transform numeric values to character string prepending leading zero(s).
leading0(num, digits = 2)
leading0(num, digits = 2)
num |
Numeric vector (character also possible) to transform. |
digits |
Numeric value of minimum length of output strings. |
Character vector with same length of strings of each value.
Original "string" is prepended by zero(s).
String length is at least max(nchar(as.character(num)))
.
Sven E. Templer [email protected]
# # use with paste to generate strings of equal size: paste0("observation", leading0(1:10, 3)) #
# # use with paste to generate strings of equal size: paste0("observation", leading0(1:10, 3)) #
Load multiple .RData files and return a (simplified) list.
lload(path = ".", pattern = ".RData", recursive = FALSE, simplify = TRUE, verbose = TRUE)
lload(path = ".", pattern = ".RData", recursive = FALSE, simplify = TRUE, verbose = TRUE)
path |
Character string with the path, as used in list.files. |
pattern |
A regular expression for file name patterns, as used in list.files. |
recursive |
Logical. Search the path recursive. |
simplify |
Logical, unlist when there are only unique object names. |
verbose |
Logical. Print information on screen about loading process. |
Returns a list of length n, when there are n data files loaded. All objects are stored in sublists. Names are according to files, and names of sublists to objects per file. If simplified, the list is of length m, when there are m objects in total loaded.
Sven E. Templer
Return a data.frame with a list of all objects of a specified environmet.
lsall(envir = .GlobalEnv, ...)
lsall(envir = .GlobalEnv, ...)
envir |
An environment where to look for objects. |
... |
Arguments forwarded to |
Returns a data.frame
with object names, lengths, classes, modes
and sizes or NULL
if the environment is empty.
Sven E. Templer
# lsall() obj1 <- 1:3 obj2 <- data.frame(1:3) obj3 <- list(1:3) lsall() #
# lsall() obj1 <- 1:3 obj2 <- data.frame(1:3) obj3 <- list(1:3) lsall() #
mgrepl
allows multiple patterns search in character vectors,
offering multicore support to parallelize search over all patterns
using mclapply.
mgrepl(patterns, text, log.fun = all, na.replace = FALSE, use.which = FALSE, cores = 1, ...)
mgrepl(patterns, text, log.fun = all, na.replace = FALSE, use.which = FALSE, cores = 1, ...)
patterns |
A vector or list containing regular expressions
(regex) to be searched in |
text |
Character vector on which the search is performed. |
log.fun |
A function to apply on the result of matching each pattern
on each element of |
na.replace |
A single value to replace each |
use.which |
A logical value.
|
cores |
Numeric value for how many cores to use for computation using
|
... |
Further arguments passed to functions grepl. |
Depending on the function defined with log.fun
, the return value is either
a matrix
is obtained with e.g. identity or as.integer. Each
row holds the result of a single pattern.
a list
is returned for functions which create results of different lengths
for each element, such as which.
Sven E. Templer
# # strings s <- c("ab","ac","bc", NA) # match all patterns (default) mgrepl(c("a", "b"), s) # match any of the patterns mgrepl(c("a", "b"), s, any) grepl("a|b", s) # return logical matrix, one column for each pattern mgrepl(c("a", "b"), s, identity) # return count of matches mgrepl(c("a", "b"), s, sum) #
# # strings s <- c("ab","ac","bc", NA) # match all patterns (default) mgrepl(c("a", "b"), s) # match any of the patterns mgrepl(c("a", "b"), s, any) grepl("a|b", s) # return logical matrix, one column for each pattern mgrepl(c("a", "b"), s, identity) # return count of matches mgrepl(c("a", "b"), s, sum) #
Return the series of triangular (/triangle) numbers up to a number of
n
rows of a triangle. The series has the entry number "A000217"
at https://oeis.org/A000217 and starts like this:
0, 1, 3, 6, 10, ...
ntri(n)
ntri(n)
n |
Positive integer value for sequence length. |
Returns an integer vector of length n
.
Sven E. Templer ([email protected])
Return the index or amount of unique values in a vector.
nunique(x, na = TRUE, ...) uniquei(x, na = TRUE, ...)
nunique(x, na = TRUE, ...) uniquei(x, na = TRUE, ...)
x |
Numeric vector to transform. |
na |
Logical, |
... |
Arguments forwarded to unique. |
Sven E. Templer
# v <- c("a","b","a", NA) nunique(v) nunique(v, FALSE) uniquei(v) uniquei(v, FALSE) #
# v <- c("a","b","a", NA) nunique(v) nunique(v, FALSE) uniquei(v) uniquei(v, FALSE) #
Transform p-values to character (e.g. stars) indicators by significance levels with the function symnum.
p2star(p, breaks = c(0, 0.001, 0.01, 0.05, 0.1, 1), symbols = c("***", "**", "*", ".", "n.s."))
p2star(p, breaks = c(0, 0.001, 0.01, 0.05, 0.1, 1), symbols = c("***", "**", "*", ".", "n.s."))
p |
Vector with p values |
breaks |
The breaks from min (0) to max (1). |
symbols |
Symbols to use for values between breaks from min to max. |
Sven E. Templer
# p2star(c(1e-5,.1,.9)) #
# p2star(c(1e-5,.1,.9)) #
Create a plot, with empty elements by presetting default parameters.
plotn(x = 0:1, y = NULL, type = "n", xlab = "", ylab = "", xaxt = "n", yaxt = "n", frame.plot = F, ...)
plotn(x = 0:1, y = NULL, type = "n", xlab = "", ylab = "", xaxt = "n", yaxt = "n", frame.plot = F, ...)
x |
Coordinates of the points. |
y |
Coordinates of the y-axis. |
type |
Plot type. |
xlab , ylab
|
Axis titles. |
xaxt , yaxt
|
Axis types. |
frame.plot |
Plot the frame. |
... |
Forwarded arguments to |
For details about the function see plot, which is called from
plotn
. More detailed information in plot.default and
par.
Sven E. Templer
Remove all objects from the global environment.
rmall(...)
rmall(...)
... |
Arguments forwarded to |
Sven E. Templer
# a <- b <- letters ls() rmall() ls() #
# a <- b <- letters ls() rmall() ls() #
Scale numeric values to a range from 0 to 1 with the function
scale0
or to a chosen range with scaler
.
scale0(x) scaler(x, r = c(0, 1), b = range(x, na.rm = TRUE))
scale0(x) scaler(x, r = c(0, 1), b = range(x, na.rm = TRUE))
x |
Numeric vector to transform. |
r |
Numeric vector of length 2 for range to scale values of
|
b |
Numeric vector of length 2 to define the border of |
Sven E. Templer
# scale0(0:10) scale0(-1:3) scale0(2:3) scaler(0:10) scaler(0:10, 1:2) scaler(0:10, 1:2, c(0, 20)) #
# scale0(0:10) scale0(-1:3) scale0(2:3) scaler(0:10) scaler(0:10, 1:2) scaler(0:10, 1:2, c(0, 20)) #
Sort a data.frame by any column(s).
## S3 method for class 'data.frame' sort(x, decreasing = FALSE, by = NULL, bye = NULL, na.last = NA, ...)
## S3 method for class 'data.frame' sort(x, decreasing = FALSE, by = NULL, bye = NULL, na.last = NA, ...)
x |
A data.frame. |
decreasing |
Logical, sort in decreasing order. See also sort. |
by |
Index (integer) or names of columns in |
bye |
Unquoted column name or |
na.last |
|
... |
Ignored for the data.frame method. |
Sven E. Templer
# d <- data.frame(a=c(1,1,1,2,NA),b=c(2,1,3,1,1),c=5:1) d sort(d) # sort by every column (a, then b, then c) sort(d, TRUE, by="c") # decreasing by column 'c' sort(d, bye=.(a,c)) # increasing by columns 'a' and then 'c' #
# d <- data.frame(a=c(1,1,1,2,NA),b=c(2,1,3,1,1),c=5:1) d sort(d) # sort by every column (a, then b, then c) sort(d, TRUE, by="c") # decreasing by column 'c' sort(d, bye=.(a,c)) # increasing by columns 'a' and then 'c' #
Transform any m x n matrix to a square matrix by column/row names. Stops if no or duplicated dimnames are provided in x.
squarematrix(x)
squarematrix(x)
x |
Object of class |
Returns a matrix
.
Sven E. Templer
# m <- matrix(1:6, 2, dimnames=list(2:3,1:3)) m squarematrix(m) #
# m <- matrix(1:6, 2, dimnames=list(2:3,1:3)) m squarematrix(m) #
Return the n
th part of a splitted string.
str_part(x, split, n, ..., roll = F) strpart(x, split, n, ..., roll = F)
str_part(x, split, n, ..., roll = F) strpart(x, split, n, ..., roll = F)
x |
Character vector. |
split |
Regular expression splitting strings. |
n |
Number of part to extract. |
... |
Arguments passed to |
roll |
Logical, if to use the last when less than maximum parts. |
A character vector of the extracted parts.
Sven E. Templer
# s <- c("abc","abcd","abc") str_part(s, "", 4) str_part(s, "", 4, roll=TRUE) #
# s <- c("abc","abcd","abc") str_part(s, "", 4) str_part(s, "", 4, roll=TRUE) #
Create a reverse version of strings.
str_rev(x) strrev(x)
str_rev(x) strrev(x)
x |
vector with strings. Is coerced to character. |
Returns a character vector with reversed strings.
Sven E. Templer
# s <- c("abc","asdf") str_rev(s) #
# s <- c("abc","asdf") str_rev(s) #
This function extracts substring(s) which match a given pattern.
strextr(x, pattern, sep = " ", mult = F, unlist = F, cores = 1)
strextr(x, pattern, sep = " ", mult = F, unlist = F, cores = 1)
x |
Character vector. |
pattern |
Regular expression. |
sep |
Character string which separates the fields. |
mult |
Logical, if multiple matching fields should be returned, or otherwise NA. |
unlist |
Logical, unlists multiple results. |
cores |
Integer for number of computational cores to use. |
The function is deprecated and will be removed with miscset
version 2.
It is recommended to use str_extract or
str_extract_all instead.
A list of character vectors containing the substrings that are
matching pattern
and are separated by sep
or NA
if
the pattern could not be found.
Sven E. Templer
# library(stringr) s <- c("A1 B1 C1","A2 B2", "AA A1", "AA", "B1 A1", "BB AB A1") strextr(s, "^[AB][[:digit:]]$") # deprecated str_extract(s, "[AB][:digit:]") strextr(s, "^[AB][[:digit:]]$", mult = TRUE) # deprecated str_extract_all(s, "[AB][:digit:]") strextr(s, "^[AB][[:digit:]]$", mult = TRUE, unlist = TRUE) # deprecated unlist(str_extract_all(s, "[AB][:digit:]")) # has no <NA> values strextr(s, "^[C][[:digit:]]$") # deprecated str_extract(s, "[C][:digit:]") #
# library(stringr) s <- c("A1 B1 C1","A2 B2", "AA A1", "AA", "B1 A1", "BB AB A1") strextr(s, "^[AB][[:digit:]]$") # deprecated str_extract(s, "[AB][:digit:]") strextr(s, "^[AB][[:digit:]]$", mult = TRUE) # deprecated str_extract_all(s, "[AB][:digit:]") strextr(s, "^[AB][[:digit:]]$", mult = TRUE, unlist = TRUE) # deprecated unlist(str_extract_all(s, "[AB][:digit:]")) # has no <NA> values strextr(s, "^[C][[:digit:]]$") # deprecated str_extract(s, "[C][:digit:]") #
This function enhances xtable
: It can write the latex code of the
table directly to a file and optionally adds a header/footer for the
document structure. Also a system command can be given to convert the
tex file to a pdf document, for example.
textable(d, file, caption = NULL, label = NULL, align = NULL, rownames = FALSE, topcapt = TRUE, digits = NULL, as.document = FALSE, landscape = FALSE, margin = 2, pt.size = 10, cmd = NULL, ...)
textable(d, file, caption = NULL, label = NULL, align = NULL, rownames = FALSE, topcapt = TRUE, digits = NULL, as.document = FALSE, landscape = FALSE, margin = 2, pt.size = 10, cmd = NULL, ...)
d |
Object (will be coerced to data.frame) to transform to a latex table. |
file |
Character string with output file name. If missing or |
caption |
Character vector with title of table. |
label |
Character vector with the latex label or HTML anchor. |
align |
Character vector with |
rownames |
Logical, include row names of |
topcapt |
Logical, put caption and label before 'tabular'. |
digits |
Number of digits to print from numeric values. |
as.document |
Logical. |
landscape |
Logical, use a landscape format for wider tables.
Only with |
margin |
Margin between table and page border in cm.
Only with |
pt.size |
Integer from 10 to 13 for the size of the characters.
Only with |
cmd |
A character vector with the system command to apply
on the output file. Only if |
... |
Forwarded arguments to print.xtable. |
Example for a system call:cmd = "pdflatex -output-directory /path/to/files/"
Returns a character vector invisible. If file
is set, then the
content is written to a file. Else it is printed to the console.
Sven E. Templer
# ## Not run: d <- head(trees) dc <- 'R "trees" dataset.' textable(d, rownames=TRUE, digits=4, caption=dc) textable(d, '/tmp/trees.tex', caption=dc, as.document=TRUE, cmd='pdflatex --output-directory /tmp') ## End(Not run) #
# ## Not run: d <- head(trees) dc <- 'R "trees" dataset.' textable(d, rownames=TRUE, digits=4, caption=dc) textable(d, '/tmp/trees.tex', caption=dc, as.document=TRUE, cmd='pdflatex --output-directory /tmp') ## End(Not run) #