library(xrayr)
library(tibble)
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
x <- c(NA,1:10)
x <- x + rnorm(length(x))
err <- runif(length(x))
df <- tibble(best = x,
min = x - err,
max = x + err)
df
#> # A tibble: 11 × 3
#> best min max
#> <dbl> <dbl> <dbl>
#> 1 NA NA NA
#> 2 1.26 1.06 1.45
#> 3 -0.437 -1.42 0.543
#> 4 2.99 2.25 3.74
#> 5 4.62 4.57 4.67
#> 6 6.15 5.62 6.68
#> 7 4.18 3.48 4.87
#> 8 6.75 6.06 7.44
#> 9 7.76 7.72 7.79
#> 10 8.72 8.49 8.94
#> 11 9.45 9.15 9.75
Symmetric error case
df %>%
dplyr::mutate(latex=latex_range(best, min, max, ndigits = 2))
#> Warning: There was 1 warning in `dplyr::mutate()`.
#> ℹ In argument: `latex = latex_range(best, min, max, ndigits = 2)`.
#> Caused by warning in `check_ndigits()`:
#> ! Is argument `ndigits` too small?
#> # A tibble: 11 × 4
#> best min max latex
#> <dbl> <dbl> <dbl> <chr>
#> 1 NA NA NA "$-$"
#> 2 1.26 1.06 1.45 "$1.3\\pm0.2$"
#> 3 -0.437 -1.42 0.543 "$-0.4\\pm1.0$"
#> 4 2.99 2.25 3.74 "$3.0\\pm0.7$"
#> 5 4.62 4.57 4.67 "$4.6\\pm0.1$"
#> 6 6.15 5.62 6.68 "$6.1\\pm0.5$"
#> 7 4.18 3.48 4.87 "$4.2\\pm0.7$"
#> 8 6.75 6.06 7.44 "$6.8\\pm0.7$"
#> 9 7.76 7.72 7.79 "$7.8\\pm0.0$"
#> 10 8.72 8.49 8.94 "$8.7\\pm0.2$"
#> 11 9.45 9.15 9.75 "$9.4\\pm0.3$"
df %>%
dplyr::mutate(latex=latex_range(best, min, max, ndigits = 2, na = ''))
#> Warning: There was 1 warning in `dplyr::mutate()`.
#> ℹ In argument: `latex = latex_range(best, min, max, ndigits = 2, na = "")`.
#> Caused by warning in `check_ndigits()`:
#> ! Is argument `ndigits` too small?
#> # A tibble: 11 × 4
#> best min max latex
#> <dbl> <dbl> <dbl> <chr>
#> 1 NA NA NA "$$"
#> 2 1.26 1.06 1.45 "$1.3\\pm0.2$"
#> 3 -0.437 -1.42 0.543 "$-0.4\\pm1.0$"
#> 4 2.99 2.25 3.74 "$3.0\\pm0.7$"
#> 5 4.62 4.57 4.67 "$4.6\\pm0.1$"
#> 6 6.15 5.62 6.68 "$6.1\\pm0.5$"
#> 7 4.18 3.48 4.87 "$4.2\\pm0.7$"
#> 8 6.75 6.06 7.44 "$6.8\\pm0.7$"
#> 9 7.76 7.72 7.79 "$7.8\\pm0.0$"
#> 10 8.72 8.49 8.94 "$8.7\\pm0.2$"
#> 11 9.45 9.15 9.75 "$9.4\\pm0.3$"
Unsymmetric case
df %>%
dplyr::mutate(max = max + 1e-2*max) %>%
dplyr::mutate(latex=latex_range(best, min, max, ndigits = 1))
#> Warning: There was 1 warning in `dplyr::mutate()`.
#> ℹ In argument: `latex = latex_range(best, min, max, ndigits = 1)`.
#> Caused by warning in `check_ndigits()`:
#> ! Is argument `ndigits` too small?
#> # A tibble: 11 × 4
#> best min max latex
#> <dbl> <dbl> <dbl> <chr>
#> 1 NA NA NA $-$
#> 2 1.26 1.06 1.47 $1.3_{-0.2}^{+0.2}$
#> 3 -0.437 -1.42 0.549 $-0.4_{-1.0}^{+1.0}$
#> 4 2.99 2.25 3.77 $3.0_{-0.7}^{+0.8}$
#> 5 4.62 4.57 4.72 $4.6_{-0.1}^{+0.1}$
#> 6 6.15 5.62 6.75 $6.1_{-0.5}^{+0.6}$
#> 7 4.18 3.48 4.92 $4.2_{-0.7}^{+0.7}$
#> 8 6.75 6.06 7.52 $6.8_{-0.7}^{+0.8}$
#> 9 7.76 7.72 7.86 $7.8_{-0.0}^{+0.1}$
#> 10 8.72 8.49 9.03 $8.7_{-0.2}^{+0.3}$
#> 11 9.45 9.15 9.84 $9.4_{-0.3}^{+0.4}$
Scientific notation
Symmetric case
x <- c(NA, 1e21, 3e22, 1e23)
err <- c(NA, 0.1234e21, 0.2345e21, 0.6789e23)
df <- tibble(best= x,
min = best - err,
max = best + err)
df1 <- df %>%
mutate(latex = latex_range_sci(best, min, max, ndigits = 2, symmetry = TRUE))
#> Warning: There was 1 warning in `mutate()`.
#> ℹ In argument: `latex = latex_range_sci(best, min, max, ndigits = 2, symmetry =
#> TRUE)`.
#> Caused by warning in `check_ndigits()`:
#> ! Is argument `ndigits` too small?
df1
#> # A tibble: 4 × 4
#> best min max latex
#> <dbl> <dbl> <dbl> <chr>
#> 1 NA NA NA "$-$"
#> 2 1e21 8.77e20 1.12e21 "$1.00_{-0.12}^{+0.12}~\\times10^{21}$"
#> 3 3e22 2.98e22 3.02e22 "$0.30_{-0.00}^{+0.00}~\\times10^{23}$"
#> 4 1e23 3.21e22 1.68e23 "$1.00_{-0.68}^{+0.68}~\\times10^{23}$"
HTML output
df1 %>%
select(latex) %>%
knitr::kable(format = 'html',
escape = FALSE,
booktabs = TRUE,
linesep='')
latex
|
|
|
|
|
Latex output
df1 %>%
select(latex) %>%
knitr::kable(format = 'latex',
escape = FALSE,
booktabs = TRUE,
linesep='') %>%
cat()
#>
#> \begin{tabular}{l}
#> \toprule
#> latex\\
#> \midrule
#> $-$\\
#> $1.00_{-0.12}^{+0.12}~\times10^{21}$\\
#> $0.30_{-0.00}^{+0.00}~\times10^{23}$\\
#> $1.00_{-0.68}^{+0.68}~\times10^{23}$\\
#> \bottomrule
#> \end{tabular}
Unsymmetric output
df2 <- df %>%
mutate(max = max*1.1) %>%
mutate(latex = latex_range_sci(best, min, max, ndigits = 2))
#> Warning: There was 1 warning in `mutate()`.
#> ℹ In argument: `latex = latex_range_sci(best, min, max, ndigits = 2)`.
#> Caused by warning in `check_ndigits()`:
#> ! Is argument `ndigits` too small?
df2 %>%
select(latex) %>%
knitr::kable(format = 'html',
escape = FALSE,
booktabs = TRUE,
linesep='')
latex
|
|
|
|
|
The same power for all values base_pow
latex
|
|
|
|
|
Row-oriented workflow
df <- tribble(~param_name, ~x, ~xmin, ~xmax,
'nH', 1e21, 0.81e21, 1.2e21,
'p', 1.7, 1.6, 2.5)
df
#> # A tibble: 2 × 4
#> param_name x xmin xmax
#> <chr> <dbl> <dbl> <dbl>
#> 1 nH 1 e21 8.10e20 1.20e21
#> 2 p 1.7e 0 1.6 e 0 2.5 e 0
foo <- function(best, min, max, param_name, param_options=list(nH=c(2,T),
phoind=c(1,F))){
purrr::pmap_chr(list(best, min, max, param_name), function(best, min, max, param_name){
if(param_options[[param_name]][2]){
latex_range_sci(best, min, max, ndigits = param_options[[param_name]][1])
} else{
latex_range(best, min, max, ndigits = param_options[[param_name]][1])
}
})
}
df %>%
dplyr::mutate(latex = foo(x, xmin, xmax, param_name, param_options = list(nH=c(1,T), p=c(2,F)))) %>%
knitr::kable(format = 'html',
escape = FALSE,
booktabs = TRUE,
linesep=''
)
param_name
|
x
|
xmin
|
xmax
|
latex
|
nH
|
1.0e+21
|
8.1e+20
|
1.2e+21
|
|
p
|
1.7e+00
|
1.6e+00
|
2.5e+00
|
|