Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vectorize tex.catwitherror #303

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 36 additions & 26 deletions R/tex-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,43 +38,53 @@ tex.catwitherror <- function(x, dx, digits=1, with.dollar=TRUE, with.cdot=TRUE,
if(missing(x) || length(x) == 0) {
stop("x must be a numeric vector with length > 0")
}
if(length(x) == 2){

if (!missing(dx)) {
have.error <- TRUE
} else if (length(x) == 2) {
dx <- x[2]
x <- x[1]
have.error <- TRUE
}else if(!missing(dx)){
have.error <- TRUE
}else{
} else {
have.error <- FALSE
}

tmp <- character(length(x))

if(!have.error){
if (!have.error) {
## just a number without error
tmp <- formatC(x, digits=digits, ...)
tmp <- sapply(x, formatC, digits = digits, ...)
}
else if(is.numeric(dx) && dx == 0){
tmp <- formatC(x, digits=digits, ...)
if(grepl("e", tmp, fixed=TRUE)){
tmp <- sub("e", "(0)e", tmp)
}else{
tmp <- paste0(tmp, "(0)")
else {
zero_error <- is.numeric(dx) & dx == 0
if (any(zero_error)) {
tmp[zero_error] <- formatC(x[zero_error], digits = digits, ...)
tmp[zero_error] <- ifelse(
grepl("e", tmp[zero_error], fixed = TRUE),
sub("e", "(0)e", tmp[zero_error]),
paste0(tmp[zero_error], "(0)"))
}
}
else{
if(requireNamespace('errors')){
tmp <- format(errors::set_errors(x, dx), digits=digits, ...)
}else{
warning("The `errors`-package is not installed. The output of `tex.catwitherror` might not be as you want it.")
tmp <- formatC(x, digits=digits, ...)
tmp <- paste0(tmp, " +- ", formatC(dx, digits=digits, ...))
if (any(!zero_error)) {
if (requireNamespace('errors')) {
tmp[!zero_error] <-
format(errors::set_errors(x[!zero_error], dx[!zero_error]), digits = digits, ...)
} else{
warning(
"The `errors`-package is not installed. The output of `tex.catwitherror` might not be as you want it."
)
tmp[!zero_error] <-
formatC(x[!zero_error], digits = digits, ...)
tmp[!zero_error] <-
paste0(tmp[!zero_error], " +- ", formatC(dx[!zero_error], digits = digits, ...))
}
}
}
}

if(with.cdot){
if(grepl("e", tmp, fixed=TRUE)){
tmp <- sub("e", "\\\\cdot 10^{", tmp)
tmp <- paste0(tmp, "}")
}
if (with.cdot) {
tmp <- ifelse(
grepl("e", tmp, fixed = TRUE),
paste0(sub("e", "\\\\cdot 10^{", tmp), "}"),
tmp)
tmp <- sub("+-", "\\\\pm", tmp, fixed=TRUE)
}
if (with.dollar) {
Expand Down
28 changes: 28 additions & 0 deletions tests/testthat/test_tex_catwitherror.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,31 @@ test_that('vector', {
tex.catwitherror(c(x, dx), digits = 4))
}
})


test_that('vectorized_two_elements', {
val <- runif(2)
err <- runif(2)

str_apply <- mapply(function (v, e) tex.catwitherror(v, e), val, err)
str_vec <- tex.catwitherror(val, err)
expect_equal(str_apply, str_vec)
})

test_that('vectorized_with_zero_error', {
val <- runif(20)
err <- c(runif(10), rep(0.0, 10))

str_apply <- mapply(function (v, e) tex.catwitherror(v, e), val, err)
str_vec <- tex.catwitherror(val, err)
expect_equal(str_apply, str_vec)
})

test_that('vectorized_with_zero_error_at_front', {
val <- runif(20)
err <- c(rep(0.0, 10), runif(10))

str_apply <- mapply(function (v, e) tex.catwitherror(v, e), val, err)
str_vec <- tex.catwitherror(val, err)
expect_equal(str_apply, str_vec)
})