Skip to content

Commit

Permalink
Merge pull request #56 from yonicd/refactor_req_check
Browse files Browse the repository at this point in the history
refactor the default tex pkgs needed
  • Loading branch information
yonicd committed Dec 10, 2023
2 parents a2aac4a + b45a474 commit 4b98f4c
Show file tree
Hide file tree
Showing 45 changed files with 867 additions and 132 deletions.
6 changes: 6 additions & 0 deletions .Rbuildignore
Expand Up @@ -8,6 +8,12 @@
^revdep$
^data-raw$
^pkgdown$
^vignettes/post\_render\.R$
^vignettes/classes\.Rmd\.orig$
^vignettes/engine\.Rmd\.orig$
^vignettes/kable\.Rmd\.orig$
^vignettes/rmarkdown\.Rmd\.orig$
^vignettes/tikz\.Rmd\.orig$
^CRAN-RELEASE$
^\.github$
^LICENSE\.md$
Expand Down
8 changes: 3 additions & 5 deletions .github/workflows/tmate.yml
@@ -1,12 +1,10 @@
name: CI
on: [push]
jobs:
check:
runs-on: macOS-latest
if: "!contains(github.event.head_commit.message, 'gha ssh')"
build:
runs-on: macOS-latest
runs-on: ubuntu-latest
if: "contains(github.event.head_commit.message, 'gha ssh')"
steps:
- uses: actions/checkout@v3
- name: Setup tmate session
uses: mxschmitt/action-tmate@v2
uses: mxschmitt/action-tmate@v3
5 changes: 3 additions & 2 deletions DESCRIPTION
@@ -1,7 +1,7 @@
Type: Package
Package: texPreview
Title: Compile and Preview Snippets of 'LaTeX'
Version: 2.0.1
Version: 2.1.0
Date: 2023-12-04
Authors@R:
c(person(given = "Jonathan",
Expand Down Expand Up @@ -52,5 +52,6 @@ RdMacros:
LazyData: false
NeedsCompilation: no
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.2
RoxygenNote: 7.2.3
License: MIT + file LICENSE
Encoding: UTF-8
3 changes: 3 additions & 0 deletions NAMESPACE
Expand Up @@ -40,8 +40,11 @@ importFrom(rstudioapi,callFun)
importFrom(rstudioapi,getActiveDocumentContext)
importFrom(stats,setNames)
importFrom(svgPanZoom,svgPanZoom)
importFrom(tinytex,check_installed)
importFrom(tinytex,latexmk)
importFrom(tinytex,tlmgr_install)
importFrom(utils,getFromNamespace)
importFrom(utils,menu)
importFrom(utils,modifyList)
importFrom(utils,rc.options)
importFrom(whisker,whisker.render)
Expand Down
39 changes: 34 additions & 5 deletions R/tex_check.R
Expand Up @@ -16,14 +16,16 @@ tex_requirements <- function(file = system.file('tmpl.tex',package = 'texPreview
if(!is.null(file))
lines <- readLines(file)

x <- grep('\\usepackage',lines,value = TRUE)
tex_req_type('usepackage', lines)

}

tex_req_type <- function(arg_type, tex_lines){
x <- grep(sprintf('\\\\%s',arg_type), tex_lines, value = TRUE)

x <- gsub('\\[(.*?)\\]','',x)

ret <- gsub('[{}]','',unlist(regmatches(x, gregexpr('\\{(.*?)\\}', x))))

ret

gsub('[{}]','',unlist(regmatches(x, gregexpr('\\{(.*?)\\}', x))))
}

#' @title Check TeX Requirements for Package
Expand Down Expand Up @@ -72,3 +74,30 @@ check_texlive <- function(x) length(suppressWarnings(system(sprintf('kpsewhich %

check_mpm <- function(x) length(shell(sprintf("mpm --list-package-names | grep %s",x),intern=TRUE))>0

texpreview_base_req <- c('standalone', 'xcolor', 'booktabs', 'multirow', 'amsmath',
'listings', 'setspace', 'caption', 'graphics', 'tools', 'psnfss', 'varwidth',
'colortbl', 'epstopdf-pkg', 'pgf','makeindex')

#' @importFrom tinytex check_installed tlmgr_install
#' @importFrom utils menu
check_base_req <- function(){
chk <- texpreview_base_req[!tinytex::check_installed(texpreview_base_req)]
if(length(chk)>0){
update_pkgs <- TRUE
warning( sprintf( 'missing tex packages needed for texPreview: %s', paste0( chk, collapse = ', ' ) ) )
if(interactive()){
update_pkgs <- switch(
utils::menu(
title = 'Install missing tex packages?',
choices = c("Yes", "No")) + 1,
cat("Nothing done\n"),
TRUE,
FALSE)
}

if(update_pkgs){
tinytex::tlmgr_install(chk)
}

}
}
4 changes: 2 additions & 2 deletions R/zzz.R
Expand Up @@ -9,7 +9,7 @@ wenv_init <- function(){
.onLoad <- function(lib,pkg) {
wenv_init()
if(interactive()){
check_requirements()
check_base_req()
rc.options(custom.completer = completeme)
register_completion(thispkg = populate)
}
Expand All @@ -20,7 +20,7 @@ wenv_init <- function(){
.onAttach <- function(lib,pkg) {
wenv_init()
if(interactive()){
check_requirements()
check_base_req()
rc.options(custom.completer = completeme)
register_completion(thispkg = populate)
}
Expand Down
6 changes: 3 additions & 3 deletions README.md
@@ -1,7 +1,7 @@
[![CRAN\_Status\_Badge](https://www.r-pkg.org/badges/version/texPreview)](https://cran.r-project.org/package=texPreview)
[![Project Status: Active - The project has reached a stable, usable state and is being actively developed.](http://www.repostatus.org/badges/0.1.0/active.svg)](https://www.repostatus.org)
[![](https://cranlogs.r-pkg.org/badges/texPreview)](https://cran.r-project.org/package=texPreview)
[![Coverage Status](https://img.shields.io/codecov/c/github/yonicd/texPreview/master.svg)](https://codecov.io/github/yonicd/texPreview?branch=master)
[![Coverage Status](https://img.shields.io/codecov/c/github/yonicd/texPreview/master.svg)](https://app.codecov.io/github/yonicd/texPreview?branch=master)
[![Covrpage Summary](https://img.shields.io/badge/covrpage-Last_Build_2022_03_28-yellowgreen.svg)](http://tinyurl.com/ybkr8fsu)
[![R-win build
status](https://github.com/yonicd/texPreview/workflows/R-win/badge.svg)](https://github.com/yonicd/texPreview)
Expand Down Expand Up @@ -33,7 +33,7 @@ Efficiently iterate, refine and share snippets of LaTeX in R with ease.
- __System Requirements__:
- Must have pdflatex in PATH, Windows users can install by running [installr::install.MikTeX](http://talgalili.github.io/installr/reference/install.MikTeX.html)
- TeX libraries that are used: `standalone`, `xcolor`, `booktabs`, `multirow`, `array`, `helvet`, `amsmath`, `rotating`, `listings`, `graphicx`, `setspace`, `caption`
- Using [tinytex](https://github.com/yihui/tinytex) you can run
- Using [tinytex](hhttps://github.com/rstudio/tinytex) you can run

```
install.packages('tinytex')
Expand All @@ -42,7 +42,7 @@ Efficiently iterate, refine and share snippets of LaTeX in R with ease.
'colortbl', 'epstopdf-pkg', 'pgf','makeindex'))
```
- To Check if the system is in compliance with these libraries run `texPreview::check_requirements()`.
- Ghostscript: If you are installing TeX with [tinytex](https://github.com/yihui/tinytex) make sure to install ghostscript onto your system.
- Ghostscript: If you are installing TeX with [tinytex](https://github.com/rstudio/tinytex) make sure to install ghostscript onto your system.

## Examples

Expand Down
5 changes: 4 additions & 1 deletion inst/tmpl.tex
@@ -1,4 +1,5 @@
\documentclass[varwidth, border={ {{left}} {{top}} {{right}} {{bottom}} }]{standalone}
\documentclass[border={ {{left}} {{top}} {{right}} {{bottom}} }]{standalone}
\usepackage{varwidth}
\usepackage[dvipsnames*,svgnames,table]{xcolor}
\usepackage{booktabs}
\usepackage{multirow}
Expand All @@ -14,5 +15,7 @@
\captionsetup{labelformat=empty}
{{{usrPackages}}}
\begin{document}
\begin{varwidth}{\linewidth}
\input{{{file}}}
\end{varwidth}
\end{document}
9 changes: 6 additions & 3 deletions tests/testthat/helper-functions.R
@@ -1,8 +1,11 @@
skip_on_windows <- function () {
skip_logic <- function () {
if (identical(.Platform$OS.type, "unix")) {
return(invisible(TRUE))
if(any(grepl('fedora',system('cat /etc/os-release', intern = TRUE))))
skip("fedora")
}else{
skip("Non Unix")
}
skip("Non Unix")
return(invisible(TRUE))
}

cleanup <- function(path, create = TRUE){
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/test-kable.R
@@ -1,5 +1,7 @@
testthat::context('converting to kable')

skip_logic()

path <- file.path(tempdir(),'tex')

cleanup(path)
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-packages.R
Expand Up @@ -5,7 +5,7 @@ testthat::describe('parse packages',{
tex <- readLines(system.file('tmpl.tex',package = 'texPreview'))

it('lines', {
testthat::expect_equal(length(tex_requirements(lines = tex,file = NULL)),11)
testthat::expect_equal(length(tex_requirements(lines = tex,file = NULL)),12)
})

})
2 changes: 2 additions & 0 deletions tests/testthat/test-print.R
@@ -1,5 +1,7 @@
testthat::context('utilities')

skip_logic()

path <- file.path(tempdir(),'tex')

cleanup(path)
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/test-rmd.R
@@ -1,5 +1,7 @@
testthat::context('Rendering markdown')

skip_logic()

path <- file.path(tempdir(),'rmd')

cleanup(path)
Expand Down
19 changes: 10 additions & 9 deletions tests/testthat/test-tex.R
@@ -1,7 +1,8 @@
library(texPreview)
testthat::context('core tex function')

skip_logic()

testthat::context('core tex function')
library(texPreview)

path <- file.path(tempdir(),'tex')

Expand All @@ -10,7 +11,7 @@ cleanup(path)
tex_opts$set(returnType = 'tex',fileDir = path)

testthat::describe('porting to tex',{

x <- texPreview::tex_preview(obj = xtable::xtable(head(iris,10)))

it('files generated', {
Expand All @@ -28,7 +29,7 @@ tex_opts$set(returnType = 'tex',fileDir = path)
tex_opts$set(returnType = 'tex')

testthat::describe('porting to tex no filedir',{

x <- texPreview::tex_preview(obj = xtable::xtable(head(iris,10)))

it('no files generated', {
Expand All @@ -46,7 +47,7 @@ tex_opts$set(returnType = 'tex',fileDir = path)
tex_opts$set(returnType = 'tex',fileDir = path)

testthat::describe('keep pdf as an output',{

x <- texPreview::tex_preview(obj = xtable::xtable(head(iris,10)),keep_pdf = TRUE)

it('files generated', {
Expand All @@ -64,7 +65,7 @@ tex_opts$set(returnType = 'tex',fileDir = path)
tex_opts$set(returnType = 'html',fileDir = path)

testthat::describe('html output',{

it('return magick object', {

x <- texPreview::tex_preview(obj = xtable::xtable(head(iris,10)))
Expand All @@ -80,7 +81,7 @@ tex_opts$set(returnType = 'tex',fileDir = path)
tex_opts$set(returnType = 'tex',fileDir = path)

testthat::describe('tex lines directly input',{

tex='\\begin{tabular}{llr}
\\hline
\\multicolumn{2}{c}{Item} \\\\
Expand Down Expand Up @@ -113,7 +114,7 @@ tex_opts$set(returnType = 'tex',fileDir = path)
tex_opts$set(returnType = 'viewer',fileDir = path,imgFormat='svg',svg_max=Inf)

testthat::describe('use svg device',{
testthat::skip_on_cran()

x <- texPreview::tex_preview(obj = xtable::xtable(head(iris,10)), stem="danp-test")
it('check if file created', {
testthat::expect_equal(length(list.files(path,pattern = 'svg$')),1)
Expand All @@ -129,7 +130,7 @@ tex_opts$set(returnType = 'tex',fileDir = path)
tex_opts$set(returnType = 'tex',fileDir = path,imgFormat = 'png')

testthat::describe('space in file path',{

x <- texPreview::tex_preview(obj = xtable::xtable(head(iris,10)))

it('check if file created', {
Expand Down
4 changes: 1 addition & 3 deletions tests/testthat/test-utilities.R
Expand Up @@ -2,8 +2,6 @@ library(texPreview)

testthat::context('utility functions of package')

#testthat::skip_on_cran()

testthat::describe('build usepackage call',{

it('basic call', {
Expand Down Expand Up @@ -48,7 +46,7 @@ testthat::describe('build usepackage multiple calls',{

testthat::describe('find packages',{

skip_on_windows()
skip_logic()

if(length(system('tlmgr --version',intern = TRUE))>0){

Expand Down
6 changes: 4 additions & 2 deletions tests/testthat/test-viewer.R
@@ -1,5 +1,7 @@
testthat::context('Rendering viewer')

testthat::skip_on_cran()

path <- file.path(tempdir(),'rmd')

cleanup(path)
Expand All @@ -15,7 +17,7 @@ options(viewer = function(x, filename){
tex_opts$set(returnType = 'viewer',fileDir = path)

testthat::describe('viewer output',{

it('validate viewer',{
testthat::expect_output(
mtcars |>
Expand All @@ -31,7 +33,7 @@ testthat::describe('viewer output',{
options(viewer = default_viewer)

testthat::describe('shiny output',{

it('validate shiny output',{
testthat::expect_null(
mtcars |>
Expand Down

0 comments on commit 4b98f4c

Please sign in to comment.