knitr::opts_knit$set(root.dir = rprojroot::find_rstudio_root_file()) # Set WD to Root here::i_am("dev/config.Rmd") library(here) library(cli) library(dplyr) library(testthat)
usethis::use_r("config") usethis::use_test("config")
fs::path_package("thaipdf")
thai_font
: thai language font
line_spacing
: line spacing (default = 1.5)
TO ADD
par_indent
: paragraph indent (default = NULL
)
par_spacing
: paragraph spacing (default = NULL
)
thaipdf_config_set()
:
render USER-based thai-preamble
in rmarkdown/
and pre-tex/
folder from template template-thai-preamble.tex
with metadata from config.yml
args: thai_font
thaipdf_config_set <- function(thai_font = "TH Sarabun New", line_spacing = 1.5){ # Validate Metadata thaipdf_config_validate(thai_font = thai_font, line_spacing = line_spacing) # Metadata: Named List as Pandoc Var metadata <- list(thai_font = thai_font, line_spacing = line_spacing) # All relevant file paths in PKG paths <- thaipdf_paths() ## Template Path paths_temp <- paths[["path_temp"]] ## Output Paths (UPDATE HERE) paths_out <- list( paths[["path_pre_global"]], paths[["path_pre_proj"]] ) # Write config.yml yaml::write_yaml(metadata, file = paths[["path_config"]]) # Render to all output: thai-preamble.tex for (i in seq_along(paths_out)) { rmarkdown::pandoc_template( metadata = metadata, template = paths[["path_temp"]], output = paths_out[[i]] ) } # Display Message cli::cli_alert_success("Setting thaipdf global configuration") thaipdf_config_get() } thaipdf_config_set()
thaipdf_config_validate <- function(thai_font = "TH Sarabun New", line_spacing = 1.5 ){ # Validate Thai font is_valid_font <- all(is.character(thai_font), length(thai_font) == 1, (thai_font != "")) if(!is_valid_font) stop("`thai_font` is invalid. You must provide only 1 valid Thai font name.", call. = FALSE) # Validate Line Spacing is_valid_lin_sp <- all(is.numeric(line_spacing), length(line_spacing) == 1, line_spacing > 0) if(!is_valid_lin_sp) stop("`line_spacing` is invalid. You must provide a numeric value.", call. = FALSE) } thaipdf_config_validate() thaipdf_config_validate(1) thaipdf_config_validate(line_spacing = "a")
testthat::test_that("thaipdf_config_set() works",{ # Error Test expect_error(thaipdf_config_set(thai_font = "")) expect_error(thaipdf_config_set(thai_font = c("a", "b"))) expect_error(thaipdf_config_set(thai_font = 1)) })
Test Validation
thai_font <- c(NULL) is_valid <- all(is.character(thai_font), length(thai_font) == 1, (thai_font != "")) is_valid rm(is_valid)
ft <- "ABC" cli_alert_success("Setting thaipdf configuration") cli::cli_li("Set Thai font to {.val {ft}}") rm(ft)
thaipdf_config_get()
: read config.yml
and print to console nicely
thaipdf_config_get <- function(){ path_config <- fs::path_package("thaipdf", "templates", "config.yml") ## Read YAML config_ls <- suppressWarnings(yaml::read_yaml(file = path_config)) ## Add class "thaipdf_config new_thaipdf_config(config_ls) } thaipdf_config_get()
To get list value
l <- thaipdf_config_get() unclass(l) rm(l)
thaipdf_config_get
print.thaipdf_config <- function(x, ...){ th_font <- x[["thai_font"]] cli::cli_h2("thaipdf Global Setting") cli::cli_li("Thai font setting is {.val {th_font}}") cli::cli_li("Line spacing is {.val {line_spacing}}") invisible(x) } print(thaipdf_config_get())
testing
ft <- "ABC" cli::cli_h2("thaipdf Global Setting") cli::cli_li("Thai font setting is {.val {ft}}") rm(ft)
If YAML field empty return NULL
list(a = NULL, b=1) %>% is.null() list(a = NULL, b=1)$a %>% is.null()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.