Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove quarto complications #1

Merged
merged 3 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,3 @@ export(sas_run_selected)
export(sas_run_string)
export(sas_to_r)
export(sas_upload)
export(sas_use_quarto)
38 changes: 1 addition & 37 deletions R/knitr.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#' @examples
#' knitr::knit_engines$set(sas = sas_engine)
sas_engine <- function (options) {
options$engine <- "txt"
check_connection()

code <- paste(options$code, collapse = "\n")
Expand Down Expand Up @@ -66,40 +67,3 @@ sas_engine <- function (options) {

knitr::engine_output(options, code, out)
}

#' Add SAS quarto template to specified directory
#'
#' @description
#' Adds SAS quarto template and syntax highlighting XML to a specified
#' directory.
#'
#' @param path (Default: Current directory) Path to copy quarto template and
#' syntax highlighting XML into.
#' @param xml Include syntax highlighting XML?
#'
#' @return No return value.
#'
#' @export
#'
#' @examples
#' \dontrun{
#' sas_connect()
#' sas_use_quarto()
#' }
sas_use_quarto <- function(path, xml = TRUE) {
if (missing(path)) {
path <- "."
} else if (!dir.exists(path)) {
stop("Specified directory does not exist")
}

template <- system.file("sasquatch.qmd", package = "sasquatch")
file.copy(template, path)

if (xml) {
sas_xml <- system.file("sas.xml", package = "sasquatch")
file.copy(sas_xml, path)
}

invisible()
}
13 changes: 3 additions & 10 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,11 @@ wrap_in_iframe <- function(html) {
html <- paste(html, collapse = "\n")
html <- gsub("'", "\"", html)

html <- paste(
"<iframe width = '100%' srcdoc = '",
html,
"<style>table {margin-left: auto; margin-right: auto;}</style>",
"<script src=\"https://cdn.jsdelivr.net/npm/@iframe-resizer/[email protected]\"></script>",
"'></iframe>",
sep = "\n"
)

paste(
"<iframe width = '100%' class='resizable-iframe' srcdoc = '",
html,
"<script>iframeResize({license: 'GPLv3', scrolling: 'yes', waitForLoad: true}, 'iframe' );</script>",
"<style>table {margin-left: auto; margin-right: auto;}</style>",
"'></iframe>",
sep = "\n"
)
}
Expand Down
11 changes: 10 additions & 1 deletion R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,13 @@
.onAttach <- function(libname, pkgname) {
.pkgenv$SASPy <- reticulate::import("saspy", delay_load = TRUE)
knitr::knit_engines$set(sas = sas_engine)
}

knitr::knit_hooks$set(document = function(x, options) {
resizer_url <- system.file("resize-iframes.js", package = "sasquatch")
resizer_code <- paste(readLines(resizer_url), collapse = "\n")
resizer_script <- paste("<script>", resizer_code, "</script>", sep = "\n")

iframe_start <- "<iframe width = '100%' class='resizable-iframe'"
sub(iframe_start, paste0(resizer_script, iframe_start), x)
})
}
23 changes: 14 additions & 9 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,26 @@ pak::pkg_install("ryanzomorrodi/sasquatch")

## Usage

Once you have setup `SASPy` and connected to the right python environment using
`reticulate` (if necessary), use `sas_use_quarto()` to create the `sasquatch`
template to a specified directory.
Once you have setup `SASPy` and connected to the right python
environment using `reticulate` (if necessary), you can create a quarto document
like any other, call `sas_connect()`, and just get going!

```{r, eval = FALSE}
sasquatch::sas_use_quarto("path to directory")
```

Run the first code block within the template.
`````{verbatim}
---
format: html
engine: knitr
---

```{r, eval = FALSE}
```{r}
library(sasquatch)
sas_connect()
```

```{sas}

```
`````

#### Code blocks

Now, you should be able to run SAS code blocks in RStudio like any other.
Expand Down
21 changes: 13 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,25 @@ pak::pkg_install("ryanzomorrodi/sasquatch")
## Usage

Once you have setup `SASPy` and connected to the right python
environment using `reticulate` (if necessary), use `sas_use_quarto()` to
create the `sasquatch` template to a specified directory.
environment using `reticulate` (if necessary), you can create a quarto
document like any other, call `sas_connect()`, and just get going!

``` r
sasquatch::sas_use_quarto("path to directory")
```
```` default
---
format: html
engine: knitr
---

Run the first code block within the template.

``` r
```{r}
library(sasquatch)
sas_connect()
```

```{sas}

```
````

#### Code blocks

Now, you should be able to run SAS code blocks in RStudio like any
Expand Down
1 change: 0 additions & 1 deletion _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ reference:
- sas_run_string
- sas_run_selected
- sas_run_file
- sas_use_quarto
- sas_engine

- title: R <-> SAS
Expand Down
1 change: 0 additions & 1 deletion inst/.gitignore

This file was deleted.

21 changes: 21 additions & 0 deletions inst/resize-iframes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function adjustIframeHeight(iframe) {
const iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
iframe.style.height = iframeDocument.documentElement.scrollHeight + 'px';
}

function adjustAllIframes() {
const iframes = document.querySelectorAll('.resizable-iframe');

iframes.forEach((iframe) => {
adjustIframeHeight(iframe);
});
}

document.querySelectorAll('.resizable-iframe').forEach((iframe) => {
iframe.onload = function() {
adjustIframeHeight(iframe);
};
});

window.addEventListener('resize', adjustAllIframes);
window.onload = adjustAllIframes;
18 changes: 0 additions & 18 deletions inst/sasquatch.qmd

This file was deleted.

27 changes: 0 additions & 27 deletions man/sas_use_quarto.Rd

This file was deleted.

9 changes: 0 additions & 9 deletions tests/testthat/test-knitr.R

This file was deleted.

40 changes: 10 additions & 30 deletions vignettes/setting_up.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -150,45 +150,25 @@ Open up your favorite R IDE and install `sasquatch`
pak::pkg_install("ryanzomorrodi/sasquatch")
```

Use `sas_use_quarto()` to create a `sasquatch` quarto template and download a syntax definitions xml for SAS within the current directory.
Now you can create SAS quarto code blocks just like any other!

```{r, eval = FALSE}
library(sasquatch)
sas_use_quarto()
```
Example:

Regardless of your IDE, every sasquatch quarto starts with:
`````{verbatim}
---
format: html
engine: knitr
---

```{r, eval = FALSE}
```{r}
library(sasquatch)
sas_connect()
```

You may also notice that there are a few quarto yaml options applied within the `sasquatch` quarto template.

The following portion of the yaml options, specifies that quarto will render to html and that the sas xml file downloaded will be used for syntax highlighting.

``` yml
format:
html:
syntax-definitions:
- sas.xml
```

**Note:** At this time only rendering to html is supported.

The next section of the xml specifies that a JavaScript library called `iframe-resizer`. This allows the SAS output to be render to its full extent within the quarto output.

``` yml
include-in-header:
text: <script src = 'https://cdn.jsdelivr.net/npm/@iframe-resizer/[email protected]'></script>
```

Lastly, we specify our rendering engine as `knitr`.
```{sas}

``` yml
engine: knitr
```
`````

### Interactive use

Expand Down
Loading