knitr::opts_chunk$set(echo = TRUE)
library(gt)
library(tidyverse)

Create a table with values and uncertainties

# Input table
tbl <-
  dplyr::tribble(
    ~value_1, ~uncertainty, ~value_2, ~uncertainty_2,
    0.352,    0.10,         0.32,     NA_real_,
    0.983,    0.13,         NA_real_, NA_real_,
    0.639,    NA_real_,     0.21,     0.10,
    NA_real_, 0.17,         0.74,     0.15
  )
gt(tbl) %>%
  cols_merge_uncert(
    col_val = value_1,
    col_uncert = uncertainty
  ) %>%
  cols_merge_uncert(
    col_val = value_2,
    col_uncert = uncertainty_2
  ) %>%
  fmt_number(
    columns = c(value_1, value_2),
    decimals = 2
  ) %>%
  sub_missing(columns = c(value_1, value_2))


rstudio/gt documentation built on Nov. 2, 2024, 5:53 p.m.