Is there anything like a macro in R Markdown?

When I use r-markdown and Ryacas, I often want to display a yacas expression as an inline equation. E.g.

```{r}
x <- ysym("a^2 + b^2");
```
$`r yac_str(x)` $

Since I do this so often, I'd like to find a more succinct way to express the inline equation command, something like

f(x)

Is a way to define something like a macro that will do this in r-markdown?

Thanks

Not that I've found. snippets can do this for an *.R file by reading Tools | Global Options | Code | Snippets, but those don't execute in *.Rmd

Can you be more precise with your example with what you want to write in the Rmd file and what you want as output ?

I am sure to understand if you want :

  • Just a way to quickly write this inline expression in your Rmd document, as something like snippet or Rstudio adding with keyboard shortcut could help
  • A way to write something in your Rmd document (f(x) is too general regarding what you want for me to clearly understand) and get the correct formating and content in the resulting HTML file (or PDF file - not sure what your output is)

If you can provide more information and maybe a reproducible example it would help !

knitr and rmarkdown are pretty customisable (using something like hooks or custom engine for example), so I think you can do what you want.

1 Like

In principle, a snippet provides functionality I could use (I wasn't aware of them). However, snippets do not seem to work outside of chunks. I'm looking for something that will work in r-markdown outside of {r} chunks.

The documentation (https://support.rstudio.com/hc/en-us/articles/204463668-Code-Snippets ) states

Note then for markdown snippets within R Markdown documents you always need to use the Shift+Tab sequence as there is no standard tab completion available within the markdown editing mode.

I'm unable to get any snippets to work outside of {r} chunks.

FYI I'm using Rstudio 1.3.1073 on a Debian 10 system.

Re output, I'm using the default settings for R notebook

output: html_notebook
editor_options:
chunk_output_type: inline

Re what I'm looking for:

Given an {r} chunk such as

```{r}
x <- ysym("a^2 + b^2");
```

what I'd really likes is something I can type outside the chunk that expands into

$`r yac_str(x)` $

where x (defined in the chunk) is supplied as an argument.

3 Likes

You can use snippets in rmarkdown outside a chunk, but the snippet needs to be defined in the "Markdown" snippets page (rather than the "R" snippets page) in order for this to work.

If you click on "edit snippets" in Global Options, the snippets file will open. What you see by default are the "R" snippets. However, in the left column of the window, you can see other languages listed, including "Markdown." Click on "Markdown" and you'll see the Markdown snippets page, where you can add your snippet.

For example, I added the following snippet:

snippet yy
	\$`r yac_str(x)`\$ 

This snippet assumes that the object we're turning into a string will always be named x (you can change the snippet to allow a user-provided name, if desired).

To use the snippet, type yy and then shift+tab and it will replace yy with the text string of the equation. If x doesn't exist, the replacement output will be $$.

4 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.