inbo / INBOmd

An R package with a collection of RMarkdown styles and templates. Elaborate examples are available at https://github.com/inbo/INBOmd_examples
https://inbo.github.io/INBOmd
GNU General Public License v3.0
11 stars 3 forks source link

function to install LaTeX dependencies #88

Closed hansvancalster closed 1 year ago

hansvancalster commented 2 years ago

As noted in #87, it can be a good idea to simplify the installation-instructions in the README.md by wrapping the latex related stuff of the installation steps into a single function INBOmd::install_latex().

This would include:

ThierryO commented 1 year ago

Below is an attempt. tinytex::check_installed() is buggy and tinytex::tlmgr_install() generates false positive errors. Not ideal to integrate in a function.

#' Install all required INBOmd dependencies
#'
#' This installs the `tinytex` package in case is it missing.
#' @family utils
#' @export
#' @importFrom assertthat assert_that
#' @importFrom utils install.packages
install_depedencies <- function() {
  if (!requireNamespace("tinytex", quietly = TRUE)) {
    install.packages("tinytex")
    assert_that(
      requireNamespace("tinytex", quietly = TRUE),
      msg = "failed to run `install.packages(\"tinytex\")`"
    )
  }
  if (!tinytex:::is_tinytex()) {
    tinytex::install_tinytex()
    assert_that(
      tinytex:::is_tinytex(), msg = "failed to run `tinytex::install_tinytex()`"
    )
  }

  tex_package <- c(
    "amsmath", "array", "babel", "booktabs", "calc", "color", "colorbl",
    "emptypage", "etoolbox", "eurosym", "fancyvrb", "float", "fontspec",
    "footmisc", "framed", "hyperref", "ifxetex", "inconsolata", "inputenc",
    "geometry", "graphicx", "kvoptions", "kvsetkeys", "lastpage", "lmodern",
    "longtable", "lscape", "makecell", "marginnote", "mdframed", "multicol",
    "multirow", "natbib", "needspace", "parskip", "pdfpages", "pdflscape",
    "pgf", "placeins", "tabu", "textpos", "threeparttable", "threeparttablex",
    "titlesec", "tocloft", "ulem", "upquote", "url", "wrapfig", "xcolor"
  )
  available <- vapply(tex_package, tinytex::check_installed, logical(1))
  tinytex::tlmgr_install(pkgs = tex_package[!available])
  c("auxtrees", "add", system.file("local_tex", package = "INBOmd")) |>
    tinytex::tlmgr_conf()
  return(invisible(NULL))
}
hansvancalster commented 1 year ago

When I run the code in the body of this function, it seems to work (reprex below), but my first attempt which used the function after loading it in the global environment indeed reported errors:

image

  if (!requireNamespace("tinytex", quietly = TRUE)) {
    install.packages("tinytex")
    assert_that(
      requireNamespace("tinytex", quietly = TRUE),
      msg = "failed to run `install.packages(\"tinytex\")`"
    )
  }
  if (!tinytex:::is_tinytex()) {
    tinytex::install_tinytex()
    assert_that(
      tinytex:::is_tinytex(), msg = "failed to run `tinytex::install_tinytex()`"
    )
  }

  tex_package <- c(
    "amsmath", "array", "babel", "booktabs", "calc", "color", "colorbl",
    "emptypage", "etoolbox", "eurosym", "fancyvrb", "float", "fontspec",
    "footmisc", "framed", "hyperref", "ifxetex", "inconsolata", "inputenc",
    "geometry", "graphicx", "kvoptions", "kvsetkeys", "lastpage", "lmodern",
    "longtable", "lscape", "makecell", "marginnote", "mdframed", "multicol",
    "multirow", "natbib", "needspace", "parskip", "pdfpages", "pdflscape",
    "pgf", "placeins", "tabu", "textpos", "threeparttable", "threeparttablex",
    "titlesec", "tocloft", "ulem", "upquote", "url", "wrapfig", "xcolor"
  )
  available <- vapply(tex_package, tinytex::check_installed, logical(1))
  tinytex::tlmgr_install(pkgs = tex_package[!available])
#> tlmgr install array calc color colorbl ifxetex inputenc graphicx lmodern longtable lscape multicol
#> tlmgr update --self
#> tlmgr install array calc color colorbl ifxetex inputenc graphicx lmodern longtable lscape multicol
  c("auxtrees", "add", system.file("local_tex", package = "INBOmd")) |>
    tinytex::tlmgr_conf()
#> tlmgr conf auxtrees add C:/R/library/INBOmd/local_tex

Created on 2023-03-10 with reprex v2.0.2

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.2.1 (2022-06-23 ucrt) #> os Windows 10 x64 (build 19044) #> system x86_64, mingw32 #> ui RTerm #> language (EN) #> collate Dutch_Belgium.utf8 #> ctype Dutch_Belgium.utf8 #> tz Europe/Paris #> date 2023-03-10 #> pandoc 2.19.2 @ C:/Program Files/RStudio/bin/quarto/bin/tools/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date (UTC) lib source #> cli 3.6.0 2023-01-09 [1] CRAN (R 4.2.2) #> digest 0.6.31 2022-12-11 [1] CRAN (R 4.2.2) #> evaluate 0.20 2023-01-17 [1] CRAN (R 4.2.2) #> fansi 1.0.4 2023-01-22 [1] CRAN (R 4.2.2) #> fastmap 1.1.1 2023-02-24 [1] CRAN (R 4.2.2) #> fs 1.6.1 2023-02-06 [1] CRAN (R 4.2.2) #> glue 1.6.2 2022-02-24 [1] CRAN (R 4.2.0) #> htmltools 0.5.4 2022-12-07 [1] CRAN (R 4.2.2) #> knitr 1.42 2023-01-25 [1] CRAN (R 4.2.2) #> lifecycle 1.0.3 2022-10-07 [1] CRAN (R 4.2.1) #> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.2.0) #> pillar 1.8.1 2022-08-19 [1] CRAN (R 4.2.1) #> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.2.0) #> purrr 1.0.1 2023-01-10 [1] CRAN (R 4.2.2) #> R.cache 0.16.0 2022-07-21 [1] CRAN (R 4.2.1) #> R.methodsS3 1.8.2 2022-06-13 [1] CRAN (R 4.2.0) #> R.oo 1.25.0 2022-06-12 [1] CRAN (R 4.2.0) #> R.utils 2.12.0 2022-06-28 [1] CRAN (R 4.2.1) #> reprex 2.0.2 2022-08-17 [1] CRAN (R 4.2.1) #> rlang 1.0.6 2022-09-24 [1] CRAN (R 4.2.1) #> rmarkdown 2.20 2023-01-19 [1] CRAN (R 4.2.2) #> rstudioapi 0.14 2022-08-22 [1] CRAN (R 4.2.1) #> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.2.0) #> styler 1.7.0 2022-03-13 [1] CRAN (R 4.2.0) #> tibble 3.1.8 2022-07-22 [1] CRAN (R 4.2.1) #> tinytex 0.44 2023-02-01 [1] CRAN (R 4.2.2) #> utf8 1.2.3 2023-01-31 [1] CRAN (R 4.2.2) #> vctrs 0.5.2 2023-01-23 [1] CRAN (R 4.2.2) #> withr 2.5.0 2022-03-03 [1] CRAN (R 4.2.0) #> xfun 0.37 2023-01-31 [1] CRAN (R 4.2.2) #> yaml 2.3.7 2023-01-23 [1] CRAN (R 4.2.2) #> #> [1] C:/R/library #> [2] C:/R/R-4.2.1/library #> #> ────────────────────────────────────────────────────────────────────────────── ```
ThierryO commented 1 year ago

e59aea050e4b240 installs TinyTex when missing and add the INBO styles. TinyTeX installs missing generic TeX packages on the fly.