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

How to evaluate include argument? #10

Open
wzbillings opened this issue Nov 24, 2023 · 2 comments
Open

How to evaluate include argument? #10

wzbillings opened this issue Nov 24, 2023 · 2 comments

Comments

@wzbillings
Copy link

wzbillings commented Nov 24, 2023

Sometimes it's easier to construct the path to a file in code, for example:
model_path = here::here("my-code-file.stan")
in a quarto document.

Is it possible to have the include argument of a chunk evaluated?

```{.stan include=model_path}
```

doesn't appear to evaluate the variable model_path correctly. Is there a syntax option to have this variable get evaluated to return the file path, and then have the path parsed correctly as normal?

@wzbillings
Copy link
Author

wzbillings commented Mar 18, 2024

If anyone was curious, you can get this to work with the following syntax:

```{.stan include=`r model_path`}
```

noting, that as mentioned in #5, whitespace is meaningful. Quarto's syntax highlighting doesn't like this, but it seems to work correctly for me (and the resulting code is highlighted correctly in the output).

Perhaps a similar example could be included in the README? To me, this feature is extremely useful.

@cderv
Copy link
Contributor

cderv commented Mar 20, 2024

To clarify, the syntax

```{.stan include=model_path}
```

is raw markdown, and will be processed in Lua directly through Pandoc. So if you have a variable with the path you want to include, you need usually to come up with similar logic as any of the output you can produce with R though knitr.

This means have R code generate the raw mardkown for you - like table function do for R dataframes, or knitr do for plots, or HTML widgets producing raw HTML.

So you could have a wrapper like this

  
```{r}
#| include: false
include_code_file <- function(path, language) {
  glue::glue("
     ```{.%language% include=%path%}
     ```", 
     .open = "%", .close = "%")
}
```

`r include_code_file(model_path, "stan")`

But indeed knitr inline R code works ok in this case too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants