diff --git a/DESCRIPTION b/DESCRIPTION index 93bcce2..b933d9b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -41,4 +41,4 @@ Encoding: UTF-8 Language: en-US LazyData: true Roxygen: list(markdown = TRUE) -RoxygenNote: 7.2.3 +RoxygenNote: 7.3.1 diff --git a/NEWS.md b/NEWS.md index c8abde8..e8cf9e6 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # sasr 0.1.2.9006 +* Add a basic SAS engine for knitr. + # sasr 0.1.2 * First CRAN version of the package. diff --git a/R/sas_engine.R b/R/sas_engine.R new file mode 100644 index 0000000..e89e460 --- /dev/null +++ b/R/sas_engine.R @@ -0,0 +1,17 @@ +#' SAS engine function +#' @param options See knitr documentation on engines. +sas_engine <- function(options) { + if (options$eval) { + ret <- sasr::run_sas(paste0(options$code, collapse = "\n"), results = "HTML") + if (identical(ret$LST, "")) { + stop(ret$LOG) + } else { + output <- ret$LST + output <- gsub("", "", output) + } + } else { + output <- NULL + } + options$results <- "asis" + engine_output(options, code = options$code, out = output) +} diff --git a/R/zzz.R b/R/zzz.R index d28e38e..706cb41 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -14,4 +14,7 @@ saspy <- NULL .onLoad <- function(libname, pkgname) { options("sascfg" = "sascfg_personal.py") saspy <<- import("saspy", delay_load = TRUE) + if (require("knitr", quietly = TRUE)) { + knitr::knit_engines$set(sas = sas_engine) + } } diff --git a/inst/WORDLIST b/inst/WORDLIST index a145d97..4d75eef 100644 --- a/inst/WORDLIST +++ b/inst/WORDLIST @@ -8,6 +8,7 @@ Sabanes Viya funder hostname +knitr onLoad reticulate sas diff --git a/inst/example.Rmd b/inst/example.Rmd new file mode 100644 index 0000000..7f4bb70 --- /dev/null +++ b/inst/example.Rmd @@ -0,0 +1,32 @@ +--- +title: "My R Markdown Document" +output: html_document +--- + +# Enable SASR + +```{r} +library(sasr) +``` + +# Execute SAS + +```{sas} +data example1; +input x y $ z; +cards; +6 A 60 +6 A 70 +2 A 100 +2 B 10 +3 B 67 +2 C 81 +3 C 63 +5 C 55 +; +run; + +proc freq data = example1; +tables y; +run; +``` diff --git a/man/sas_engine.Rd b/man/sas_engine.Rd new file mode 100644 index 0000000..86a529c --- /dev/null +++ b/man/sas_engine.Rd @@ -0,0 +1,14 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/sas_engine.R +\name{sas_engine} +\alias{sas_engine} +\title{SAS engine function} +\usage{ +sas_engine(options) +} +\arguments{ +\item{options}{See knitr documentation on engines.} +} +\description{ +SAS engine function +} diff --git a/man/sasr-package.Rd b/man/sasr-package.Rd index 2353494..073c988 100644 --- a/man/sasr-package.Rd +++ b/man/sasr-package.Rd @@ -2,8 +2,8 @@ % Please edit documentation in R/sasr-package.R \docType{package} \name{sasr-package} +\alias{sasr} \alias{sasr-package} -\alias{_PACKAGE} \title{\code{sasr} Package} \description{ \code{sasr} provides interface to SAS through \code{saspy} and \code{reticulate} in R. @@ -12,6 +12,7 @@ Useful links: \itemize{ \item \url{https://github.com/insightsengineering/sasr/} + \item \url{https://insightsengineering.github.io/sasr/latest-tag/} \item Report bugs at \url{https://github.com/insightsengineering/sasr/issues} } diff --git a/tests/testthat/_snaps/rmarkdown.md b/tests/testthat/_snaps/rmarkdown.md new file mode 100644 index 0000000..8d75334 --- /dev/null +++ b/tests/testthat/_snaps/rmarkdown.md @@ -0,0 +1,25 @@ +# rmarkdown engine works + + Code + rmarkdown::render(system.file("example.Rmd", package = "sasr"), quiet = TRUE) + Output + submit the following code: + data example1; + input x y $ z; + cards; + 6 A 60 + 6 A 70 + 2 A 100 + 2 B 10 + 3 B 67 + 2 C 81 + 3 C 63 + 5 C 55 + ; + run; + + proc freq data = example1; + tables y; + run; + format of result is HTML + diff --git a/tests/testthat/test-rmarkdown.R b/tests/testthat/test-rmarkdown.R new file mode 100644 index 0000000..20c9eb9 --- /dev/null +++ b/tests/testthat/test-rmarkdown.R @@ -0,0 +1,7 @@ +# knitr engin ---- +test_that("rmarkdown engine works", { + .sasr_env$.sas_session <- dummy_session + expect_snapshot( + rmarkdown::render(system.file("example.Rmd", package = "sasr"), quiet = TRUE) + ) +})