| Title: | Collection of personal practical R functions |
|---|---|
| Description: | Awesome functions in R. |
| Authors: | Dongdong Kong [aut, cre] |
| Maintainer: | Dongdong Kong <[email protected]> |
| License: | GPL-3 |
| Version: | 0.1.11 |
| Built: | 2026-05-08 04:42:18 UTC |
| Source: | https://github.com/rpkgs/Ipaper |
To aggregated data into n-day (e.g. 8-day, 16-day) like MODIS product, a n-day flag is need.
add_dn(d, days = 8)add_dn(d, days = 8)
d |
data.frame or data.table |
days |
Integer number or vector, can't have duplicated value. |
date <- seq.Date(as.Date("2010-01-01"), as.Date("2010-12-31"), by = "day") d <- data.frame(date) dnew <- add_dn(d, days = c(8, 16))date <- seq.Date(as.Date("2010-01-01"), as.Date("2010-12-31"), by = "day") d <- data.frame(date) dnew <- add_dn(d, days = c(8, 16))
NA values will be removed automatically
apply_3d( array, dim = 3, FUN = rowMeans2, by = NULL, scale = 1, na.rm = TRUE, ... )apply_3d( array, dim = 3, FUN = rowMeans2, by = NULL, scale = 1, na.rm = TRUE, ... )
array |
A 3d array |
dim |
giving the subscripts to split up data by. |
FUN |
function, should only be row applied function, e.g. |
by |
|
scale |
in the same length of |
apply_row matrixStats::rowRanges
set.seed(1) size <- c(10, 8, 31) arr <- array(rnorm(10 * 8 * 31), dim = size) by <- c(rep(1, 10), rep(2, 21)) r2 <- apply_3d(arr, 3, by = by, FUN = rowMeans) ## Not run: arr_yearly <- apply_3d(arr, by = year(dates), scale = days_in_month(dates)) ## End(Not run)set.seed(1) size <- c(10, 8, 31) arr <- array(rnorm(10 * 8 * 31), dim = size) by <- c(rep(1, 10), rep(2, 21)) r2 <- apply_3d(arr, 3, by = by, FUN = rowMeans) ## Not run: arr_yearly <- apply_3d(arr, by = year(dates), scale = days_in_month(dates)) ## End(Not run)
apply_col: aggregate by col, return a [ngrp, ncol] matrix
apply_row: aggregate by row, return a [nrow, ngrp] matrix
apply_col(mat, by, FUN = colMeans2, scale = 1, ...) apply_row(mat, by, FUN = rowMeans2, scale = 1, ...)apply_col(mat, by, FUN = colMeans2, scale = 1, ...) apply_row(mat, by, FUN = rowMeans2, scale = 1, ...)
mat |
matrix, |
by |
integer vector, with the dim of |
scale |
in the same length of |
For example, setting the dimension of mat is [ngrid, ntime],
if you want to aggregate by time, apply_row should be used here;
if you want to aggregate by region (grids), apply_col should be used.
This function also suits for big.matrix object.
mat <- matrix(rnorm(4 * 6), 4, 6) mat_bycol <- apply_col(mat, c(1, 1, 2, 2), colMeans) mat_byrow <- apply_row(mat, c(1, 1, 2, 2, 3, 3), rowMeans)mat <- matrix(rnorm(4 * 6), 4, 6) mat_bycol <- apply_col(mat, c(1, 1, 2, 2), colMeans) mat_byrow <- apply_row(mat, c(1, 1, 2, 2, 3, 3), rowMeans)
array_3dTo2d
array_3dTo2d(array, I_grid = NULL) array_2dTo3d(array, I_grid = NULL, dim)array_3dTo2d(array, I_grid = NULL) array_2dTo3d(array, I_grid = NULL, dim)
array |
array with the dimension of |
I_grid |
subindex of |
dim |
|
[nlat*nlon, ntime]
Convert array to data.table
array2dt(arr, dimnames) dt2array(dt, value_col = "value")array2dt(arr, dimnames) dt2array(dt, value_col = "value")
arr |
input array |
dimnames |
list of dimension names, e.g., |
data.table
library(Ipaper) arr <- array(1:6, dim = c(2, 3), dimnames = list( site = c("A", "B"), date = c("d1", "d2", "d3") # var = c("v1", "v2", "v3", "v4") ) ) # array -> dt dt <- array2dt(arr, dimnames(arr)) print(dt) # dt -> array arr2 <- dt2array(dt) all.equal(arr, arr2)library(Ipaper) arr <- array(1:6, dim = c(2, 3), dimnames = list( site = c("A", "B"), date = c("d1", "d2", "d3") # var = c("v1", "v2", "v3", "v4") ) ) # array -> dt dt <- array2dt(arr, dimnames(arr)) print(dt) # dt -> array arr2 <- dt2array(dt) all.equal(arr, arr2)
generate R script of character vector
char2script(x, collapse = "\"", verbose = TRUE) code_ChrVec(x, collapse = "\"", verbose = TRUE)char2script(x, collapse = "\"", verbose = TRUE) code_ChrVec(x, collapse = "\"", verbose = TRUE)
x |
character vector, data.frame or list. |
collapse |
an optional character string to separate the results. Not NA_character_. |
chunk
chunk(x, nchunk = 6)chunk(x, nchunk = 6)
https://stackoverflow.com/questions/3318333/split-a-vector-into-chunks-in-r
clamp values in the range of lims
clamp(x, lims = c(0, 1), fill.na = FALSE) clamp_min(x, value = 0) clamp_max(x, value = 0)clamp(x, lims = c(0, 1), fill.na = FALSE) clamp_min(x, value = 0) clamp_max(x, value = 0)
x |
Numeric vector |
lims |
limits |
fill.na |
If true, values of lims are set to NA; else, values are just
constrained in the range of |
clamp(1:10, lims = c(4, 7), fill.na = TRUE)clamp(1:10, lims = c(4, 7), fill.na = TRUE)
cut_plevels
cut_plevels(x, pvalue = c(0.01, 0.05, 0.1), verbose = FALSE)cut_plevels(x, pvalue = c(0.01, 0.05, 0.1), verbose = FALSE)
x |
numeric vector |
pvalue |
p <= |
x <- c(-0.09, -0.4, 0.04, 0.15) cut_plevels(x, verbose = TRUE)x <- c(-0.09, -0.4, 0.04, 0.15) cut_plevels(x, verbose = TRUE)
The corresponding date of the first day of month
date_ym(date) date_y(date) date_yj(year, doy) date_ydn(year, dn, delta = 8)date_ym(date) date_y(date) date_yj(year, doy) date_ydn(year, dn, delta = 8)
open assign path in windows explorer, and default path is current directory. This function is only designed for windows system.
dir.show(path = getwd(), verbose = FALSE)dir.show(path = getwd(), verbose = FALSE)
path |
the path you want to open |
dir2
dir2(path = ".", pattern = NULL, full.names = TRUE, ...)dir2(path = ".", pattern = NULL, full.names = TRUE, ...)
path |
a character vector of full path names; the default
corresponds to the working directory, |
pattern |
an optional regular expression. Only file names which match the regular expression will be returned. |
full.names |
a logical value. If |
... |
other parameters to |
dt_day2year
dt_day2year( dat, nmiss_day_per_mon = 3, nmiss_MonPerYear = 0, nmin_year = 55, ... )dt_day2year( dat, nmiss_day_per_mon = 3, nmiss_MonPerYear = 0, nmin_year = 55, ... )
dat |
A data.table, at least with the columns of |
data.table, apply function, and return results in a data.table.For each subset of a data.table, apply function then combine results into a data.table.
dt_ddply( .data, .variables, .f = NULL, ..., .progress = "none", .drop = TRUE, .parallel = FALSE ) dt_ldply( .data, .f = NULL, ..., .progress = "none", .parallel = FALSE, .id = NA ) dt_dlply( .data, .variables, .f = NULL, ..., .progress = "none", .drop = TRUE, .parallel = FALSE )dt_ddply( .data, .variables, .f = NULL, ..., .progress = "none", .drop = TRUE, .parallel = FALSE ) dt_ldply( .data, .f = NULL, ..., .progress = "none", .parallel = FALSE, .id = NA ) dt_dlply( .data, .variables, .f = NULL, ..., .progress = "none", .drop = TRUE, .parallel = FALSE )
.data |
data frame to be processed |
.variables |
variables to split data frame by, as |
.f |
A function, specified in one of the following ways:
Wrap a function with |
... |
other arguments passed on to |
.progress |
name of the progress bar to use, see
|
.drop |
should combinations of variables that do not appear in the input data be preserved (FALSE) or dropped (TRUE, default) |
.parallel |
if |
dt <- data.table(x = 1:10, y = 1:5) dt_dlply(dt, .(y), ~.[which.max(x)]) dt_ddply(dt, .(y), ~ top_n(., 1, x))dt <- data.table(x = 1:10, y = 1:5) dt_dlply(dt, .(y), ~.[which.max(x)]) dt_ddply(dt, .(y), ~ top_n(., 1, x))
dplyr::across
data.frame manipulating function by dplyr::across
dt_round(d, digits = 2) dt_chr2num(d, fun = as.numeric)dt_round(d, digits = 2) dt_chr2num(d, fun = as.numeric)
export data
export(x, path, ..., nthreads = 6)export(x, path, ..., nthreads = 6)
Support rda, rds, fst, csv, qs
get file name and extension
file_ext(file) file_name(file)file_ext(file) file_name(file)
file |
file path |
file_name("./a.pdf") file_ext("./a.pdf")file_name("./a.pdf") file_ext("./a.pdf")
flipud and fliplr
flipud(x, ...) fliplr(x)flipud(x, ...) fliplr(x)
fprintf Print sprintf result into console, just like C style fprintf function
fprintf(fmt, ...)fprintf(fmt, ...)
fmt |
a character vector of format strings, each of up to 8192 bytes. |
... |
other parameters will be passed to |
cat(fprintf("%s\n", "Hello phenofit!"))cat(fprintf("%s\n", "Hello phenofit!"))
fread_dir
fread_dir(indir, pattern = "*.csv", ..., .progress = "text", list2df = FALSE)fread_dir(indir, pattern = "*.csv", ..., .progress = "text", list2df = FALSE)
... |
others to |
getwd_clip: get directory path in clipboard, same as getwd function
setwd_clip: set directory path in clipboard, same as setwd function
getwd_clip() setwd_clip()getwd_clip() setwd_clip()
Only works in windows
https://stackoverflow.com/questions/10959521/how-to-write-to-clipboard-on-ubuntu-linux-in-r
## Not run: getwd_clip() setwd_clip() dir.show() ## End(Not run)## Not run: getwd_clip() setwd_clip() dir.show() ## End(Not run)
git tools
git_push(f = FALSE) git_commit_amend() git_commit(title = Sys.time()) git_set_remote(url)git_push(f = FALSE) git_commit_amend() git_commit(title = Sys.time()) git_set_remote(url)
grouped_list
group_map2(df, .f, result.name = "data", ..., .keep = FALSE, .progress = FALSE)group_map2(df, .f, result.name = "data", ..., .keep = FALSE, .progress = FALSE)
grouped_list
grouped_list(data, group)grouped_list(data, group)
data |
A list object, |
group |
A data.frame or data.table object |
ternary operator just like java test ? yes : no
ifelse2(test, yes, no)ifelse2(test, yes, no)
test |
an object which can be coerced to logical mode. |
yes |
return values for true elements of test. |
no |
return values for false elements of test. |
x <- ifelse2(TRUE, 1:4, 1:10)x <- ifelse2(TRUE, 1:4, 1:10)
import data to R
import(path, ..., nthreads = 6)import(path, ..., nthreads = 6)
nthreads |
The number of threads to use when reading data (the initial value is 1L). When TBB is not available, values greater than 1 emit a warning and fall back to 1. |
Support rda, rds, fst, csv, qs2
Attempts to install a package directly from gitee.
install_gitee(repo)install_gitee(repo)
Repository |
address in the format |
## Not run: install_gitee("adv-r/Ipaper") ## End(Not run)## Not run: install_gitee("adv-r/Ipaper") ## End(Not run)
blind shortcuts to rstudio addin
key_blind()key_blind()
label_tag
label_tag(labels, tag = TRUE, expression = TRUE, letter_begin = 1) char2expr(labels)label_tag(labels, tag = TRUE, expression = TRUE, letter_begin = 1) char2expr(labels)
labels |
character vector or expression vector |
tag |
boolean |
label_tag(1:5) char2expr(1:5)label_tag(1:5) char2expr(1:5)
listk
listk(...)listk(...)
... |
objects, possibly named. |
a <- 1 b <- 1:2 c <- 1:3 l1 <- listk(a, b, c) l2 <- listk(a, b, c = 1:3) l3 <- listk(a = 1, b = 1:2, c = 1:3) l4 <- listk(1, 1:2, c)a <- 1 b <- 1:2 c <- 1:3 l1 <- listk(a, b, c) l2 <- listk(a, b, c = 1:3) l3 <- listk(a = 1, b = 1:2, c = 1:3) l4 <- listk(1, 1:2, c)
plyr function in purrr style
llply(.data, .f = NULL, .progress = "none", .parallel = FALSE, ...) ldply(.data, .f = NULL, ...) laply(.data, .f = NULL, ...) map_simplify(.data, .f = NULL, ...)llply(.data, .f = NULL, .progress = "none", .parallel = FALSE, ...) ldply(.data, .f = NULL, ...) laply(.data, .f = NULL, ...) map_simplify(.data, .f = NULL, ...)
.data |
list to be processed |
.progress |
name of the progress bar to use, see
|
.parallel |
if |
... |
other arguments passed on to |
x <- list(a = 1:10, beta = exp(-3:3), logic = c(TRUE, FALSE, FALSE, TRUE)) llply(x, mean, .progress = "text") llply(x, ~ mean(.x), .progress = TRUE) llply(x, quantile, probs = 1:3 / 4)x <- list(a = 1:10, beta = exp(-3:3), logic = c(TRUE, FALSE, FALSE, TRUE)) llply(x, mean, .progress = "text") llply(x, ~ mean(.x), .progress = TRUE) llply(x, quantile, probs = 1:3 / 4)
melt_list
melt_list(list, ..., na.rm = TRUE, str2num = TRUE, str2factor = TRUE) melt_tree(x, names, ...)melt_list(list, ..., na.rm = TRUE, str2num = TRUE, str2factor = TRUE) melt_tree(x, names, ...)
list |
A list object, with the same colnames data.frame in every element. |
... |
other parameters to melt |
na.rm |
Boolean |
var.name |
vector of id variables. Can be integer (variable position) or string (variable name). If blank, will use all non-measured variables. |
# data.frame df <- data.frame(year = 2010, day = 1:3, month = 1, site = "A") l <- list(a = df, b = df) melt_list(l, 'id') %>% as_tibble() melt_list(l, 'id' = 1) %>% as_tibble() melt_list(set_names(l, NULL), 'id') %>% as_tibble() # data.table df <- data.table::data.table(year = 2010, day = 1:3, month = 1, site = "A") l <- list(a = df, b = df) melt_list(l, "id") melt_list(l, id = c("a", "b")) # int `id` is much more efficient melt_list(l, id = c(1, 2))# data.frame df <- data.frame(year = 2010, day = 1:3, month = 1, site = "A") l <- list(a = df, b = df) melt_list(l, 'id') %>% as_tibble() melt_list(l, 'id' = 1) %>% as_tibble() melt_list(set_names(l, NULL), 'id') %>% as_tibble() # data.table df <- data.table::data.table(year = 2010, day = 1:3, month = 1, site = "A") l <- list(a = df, b = df) melt_list(l, "id") melt_list(l, id = c("a", "b")) # int `id` is much more efficient melt_list(l, id = c(1, 2))
mkdir
mkdir(path) file_mv(files, outdir) file_cp(files, outdir) check_dir(path)mkdir(path) file_mv(files, outdir) file_cp(files, outdir) check_dir(path)
path |
character vectors |
Get object size in unit
obj_size(obj, unit = "Mb") obj.size(obj, unit = "Mb")obj_size(obj, unit = "Mb") obj.size(obj, unit = "Mb")
obj |
Object |
unit |
"Kb", "Mb" or "Gb" |
obj_size(1:100)obj_size(1:100)
*: zero or more
+: one or more
?: zero or one
parse_deg(xs)parse_deg(xs)
In windows system, conversion will not be applied.
path.mnt(path) path_mnt(path)path.mnt(path) path_mnt(path)
path |
character vector of file paths. |
pdf_view
pdf_view(file, ...) SumatraPDF(path = getwd(), verbose = FALSE) evince(path = getwd(), verbose = FALSE)pdf_view(file, ...) SumatraPDF(path = getwd(), verbose = FALSE) evince(path = getwd(), verbose = FALSE)
file |
the path of pdf file |
not work in wsl
print.data.table
## S3 method for class 'data.table' print(d, n = NULL, ..., maxrows = 1e+06)## S3 method for class 'data.table' print(d, n = NULL, ..., maxrows = 1e+06)
d <- data.table(1:100) options(datatable.print.nrow = 100) print(d)d <- data.table(1:100) options(datatable.print.nrow = 100) print(d)
Modified purrr functions with progress bar
pro_map(.x, .f, ..., .progress = FALSE)pro_map(.x, .f, ..., .progress = FALSE)
.x |
A list or atomic vector. |
.f |
A function, specified in one of the following ways:
Wrap a function with |
... |
Additional arguments passed on to the mapped function. We now generally recommend against using # Instead of x |> map(f, 1, 2, collapse = ",") # do: x |> map(\(x) f(x, 1, 2, collapse = ",")) This makes it easier to understand which arguments belong to which function and will tend to yield better error messages. |
.progress |
Whether to show a progress bar. Use |
Helper function for generating progress bar functions
progressively( .mapper, .farg = NULL, .xarg = NULL, .yarg = NULL, .larg = NULL, .atarg = NULL, .parg = NULL )progressively( .mapper, .farg = NULL, .xarg = NULL, .yarg = NULL, .larg = NULL, .atarg = NULL, .parg = NULL )
read csv or xls/xlsx file
read_excel(f, ...)read_excel(f, ...)
... |
others to fun, one of |
read_xlsx
read_xlsx(file, sheet = 1, ...)read_xlsx(file, sheet = 1, ...)
If excel file hava many sheets, this function also works.
read_xlsx2list(file, ...)read_xlsx2list(file, ...)
file |
xlsx or xls file path |
... |
other parameters to |
print the running ID in the console
runningId(i, step = 1, N, prefix = "")runningId(i, step = 1, N, prefix = "")
i |
the running Id. |
step |
how long of print step. |
N |
The number of total iteration |
prefix |
prefix string |
for (i in 1:10) { runningId(i, prefix = "phenofit") }for (i in 1:10) { runningId(i, prefix = "phenofit") }
Set dimensions of an Object
set_dim(x, dim)set_dim(x, dim)
x |
an R object, for example a matrix, array or data frame. |
x <- 1:12 set_dim(x, c(3, 4))x <- 1:12 set_dim(x, c(3, 4))
set_jupyter
set_jupyter(width = 10, height = 6, res = 200) set_dirRoot(verbose = TRUE, dir = getwd())set_jupyter(width = 10, height = 6, res = 200) set_dirRoot(verbose = TRUE, dir = getwd())
https://stackoverflow.com/questions/42729049/how-to-change-the-size-of-r-plots-in-jupyter
slope_arr
slope_arr(arr, FUN = rtrend::slope_mk, return.list = FALSE)slope_arr(arr, FUN = rtrend::slope_mk, return.list = FALSE)
arr |
a matrix ( |
FUN |
slope functions, see |
return.list |
boolean,
|
t, A 3d array, with the dim of [nx, ny, 2].
t[,,1]: slope
t[,,2]: pvalue
which functions
which.na(x) which.notna(x) which.isnull(x) which.null(x) which.notnull(x) which.notempty(x) which.empty(x) which.dup(x)which.na(x) which.notna(x) which.isnull(x) which.null(x) which.notnull(x) which.notempty(x) which.empty(x) which.dup(x)
x |
numeric vector |
write figure to pdf, tif, png, jpg, svg, or emf, according to file suffix.
write_fig( p, file = "Rplot.pdf", width = 10, height = 5, devices = NULL, res = 300, show = TRUE, use.cairo_pdf = TRUE, use.file_show = FALSE )write_fig( p, file = "Rplot.pdf", width = 10, height = 5, devices = NULL, res = 300, show = TRUE, use.cairo_pdf = TRUE, use.file_show = FALSE )
p |
could be one of |
file |
file path of output figure |
width |
the width of the device in inches. |
height |
the height of the device in inches. |
devices |
can be c("pdf", "tif", "tiff", "png", "jpg", "svg", "emf"). If
not specified, devices will be determined according to the postpix of |
res |
The nominal resolution in ppi which will be recorded in the bitmap file, if a positive integer. Also used for units other than the default. If not specified, taken as 72 ppi to set the size of text and line widths. |
show |
Boolean. Whether show file after finished writing? |
use.cairo_pdf |
This parameter is for pdf type. whether to use |
use.file_show |
boolean. If true, |
grDevices::cairo_pdf(), grDevices::png(), Cairo::Cairo()
## Not run: library(ggplot2) p <- ggplot(mpg, aes(class, hwy)) p1 <- p + geom_boxplot2() ## ggplot version write_fig(p1, "Fig. 1. ggplot.pdf", show = TRUE) # pdf_view("Fig. 1. ggplot.pdf") write_fig(p1, "Fig. 1. ggplot", show = TRUE) write_fig(p1, "Fig. 1. ggplot.pdf", show = TRUE, devices = c("jpg", "png", "svg", "pdf", "tif", "emf")) ## lattice x <- seq(pi/4, 5 * pi, length.out = 100) y <- seq(pi/4, 5 * pi, length.out = 100) r <- as.vector(sqrt(outer(x^2, y^2, "+"))) grid <- expand.grid(x=x, y=y) grid$z <- cos(r^2) * exp(-r/(pi^3)) p <- levelplot(z~x*y, grid, cuts = 50, scales=list(log="e"), xlab="", ylab="", main="Weird Function", sub="with log scales", colorkey = FALSE, region = TRUE) write_fig(p, "fig_lattice", show = TRUE) ## grid expression g <- grid::circleGrob() write_fig(g, "fig_grid", show = TRUE) ## R expression write_fig({ rx <- range(x <- 10*1:nrow(volcano)) ry <- range(y <- 10*1:ncol(volcano)) ry <- ry + c(-1, 1) * (diff(rx) - diff(ry))/2 tcol <- terrain.colors(12) # par(opar); # opar <- par(pty = "s", bg = "lightcyan") plot(x = 0, y = 0, type = "n", xlim = rx, ylim = ry, xlab = "", ylab = "") u <- par("usr") rect(u[1], u[3], u[2], u[4], col = tcol[8], border = "red") contour(x, y, volcano, col = tcol[2], lty = "solid", add = TRUE, vfont = c("sans serif", "plain")) title("A Topographic Map of Maunga Whau", font = 4) abline(h = 200*0:4, v = 200*0:4, col = "lightgray", lty = 2, lwd = 0.1) }, "fig_expr.pdf") # quote expression also works expr = quote({ rx <- range(x <- 10*1:nrow(volcano)) ry <- range(y <- 10*1:ncol(volcano)) ry <- ry + c(-1, 1) * (diff(rx) - diff(ry))/2 tcol <- terrain.colors(12) # par(opar); # opar <- par(pty = "s", bg = "lightcyan") plot(x = 0, y = 0, type = "n", xlim = rx, ylim = ry, xlab = "", ylab = "") u <- par("usr") rect(u[1], u[3], u[2], u[4], col = tcol[8], border = "red") contour(x, y, volcano, col = tcol[2], lty = "solid", add = TRUE, vfont = c("sans serif", "plain")) title("A Topographic Map of Maunga Whau", font = 4) abline(h = 200*0:4, v = 200*0:4, col = "lightgray", lty = 2, lwd = 0.1) }) write_fig(expr, "fig_expr2.pdf") ## End(Not run)## Not run: library(ggplot2) p <- ggplot(mpg, aes(class, hwy)) p1 <- p + geom_boxplot2() ## ggplot version write_fig(p1, "Fig. 1. ggplot.pdf", show = TRUE) # pdf_view("Fig. 1. ggplot.pdf") write_fig(p1, "Fig. 1. ggplot", show = TRUE) write_fig(p1, "Fig. 1. ggplot.pdf", show = TRUE, devices = c("jpg", "png", "svg", "pdf", "tif", "emf")) ## lattice x <- seq(pi/4, 5 * pi, length.out = 100) y <- seq(pi/4, 5 * pi, length.out = 100) r <- as.vector(sqrt(outer(x^2, y^2, "+"))) grid <- expand.grid(x=x, y=y) grid$z <- cos(r^2) * exp(-r/(pi^3)) p <- levelplot(z~x*y, grid, cuts = 50, scales=list(log="e"), xlab="", ylab="", main="Weird Function", sub="with log scales", colorkey = FALSE, region = TRUE) write_fig(p, "fig_lattice", show = TRUE) ## grid expression g <- grid::circleGrob() write_fig(g, "fig_grid", show = TRUE) ## R expression write_fig({ rx <- range(x <- 10*1:nrow(volcano)) ry <- range(y <- 10*1:ncol(volcano)) ry <- ry + c(-1, 1) * (diff(rx) - diff(ry))/2 tcol <- terrain.colors(12) # par(opar); # opar <- par(pty = "s", bg = "lightcyan") plot(x = 0, y = 0, type = "n", xlim = rx, ylim = ry, xlab = "", ylab = "") u <- par("usr") rect(u[1], u[3], u[2], u[4], col = tcol[8], border = "red") contour(x, y, volcano, col = tcol[2], lty = "solid", add = TRUE, vfont = c("sans serif", "plain")) title("A Topographic Map of Maunga Whau", font = 4) abline(h = 200*0:4, v = 200*0:4, col = "lightgray", lty = 2, lwd = 0.1) }, "fig_expr.pdf") # quote expression also works expr = quote({ rx <- range(x <- 10*1:nrow(volcano)) ry <- range(y <- 10*1:ncol(volcano)) ry <- ry + c(-1, 1) * (diff(rx) - diff(ry))/2 tcol <- terrain.colors(12) # par(opar); # opar <- par(pty = "s", bg = "lightcyan") plot(x = 0, y = 0, type = "n", xlim = rx, ylim = ry, xlab = "", ylab = "") u <- par("usr") rect(u[1], u[3], u[2], u[4], col = tcol[8], border = "red") contour(x, y, volcano, col = tcol[2], lty = "solid", add = TRUE, vfont = c("sans serif", "plain")) title("A Topographic Map of Maunga Whau", font = 4) abline(h = 200*0:4, v = 200*0:4, col = "lightgray", lty = 2, lwd = 0.1) }) write_fig(expr, "fig_expr2.pdf") ## End(Not run)
Write a single data frame into one sheet of a Workbook.
file can be a wbWorkbook object or a file path string. When a file path
is given, the workbook is loaded (or created if the file does not exist),
the sheet is written, and the file is saved back automatically.
Write a list of data frames into a Workbook, one sheet per
element. file can be a wbWorkbook object or a file path string. When a
file path is given, the workbook is loaded (or created if absent), written,
and saved back automatically. The file extension is normalised to .xlsx.
write_sheet(d, file, sheetName, overwrite = FALSE) write_sheets(lst, file, .progress = "none", show = FALSE, overwrite_wb = TRUE) write_list2xlsx( lst, file, .progress = "none", show = FALSE, overwrite_wb = TRUE )write_sheet(d, file, sheetName, overwrite = FALSE) write_sheets(lst, file, .progress = "none", show = FALSE, overwrite_wb = TRUE) write_list2xlsx( lst, file, .progress = "none", show = FALSE, overwrite_wb = TRUE )
d |
A data frame. |
file |
A file path string or an existing |
sheetName |
Sheet name string. |
overwrite |
if TRUE and |
lst |
List of data frames. Names are used as sheet names. |
.progress |
name of the progress bar to use, see create_progress_bar. |
show |
open the file after saving (only applies when |
overwrite_wb |
if TRUE (default) always create a fresh workbook when
|
The (modified) wbWorkbook object, invisibly.
The (modified) wbWorkbook object, invisibly.