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 make conditional block in Rmarkdown #72

Closed
1 of 4 tasks
cderv opened this issue Oct 8, 2018 · 5 comments
Closed
1 of 4 tasks

How to make conditional block in Rmarkdown #72

cderv opened this issue Oct 8, 2018 · 5 comments

Comments

@cderv
Copy link
Collaborator

cderv commented Oct 8, 2018

A small recipe about conditional block

  • using if in inline r block : `r if(test) {do}`
  • using chunk option eval with a variable define
```{r conditional, eval = variable-test}
do_something()
```

Related SO question

Related Knitr example

@cderv
Copy link
Collaborator Author

cderv commented Oct 10, 2018

Very interesting use case based on this : apply eval = TRUE based on the name (label) of the chunk
https://stackoverflow.com/questions/26993254/how-to-get-chunk-name-in-knitr

An opportunity to show opts_current$get("label") but there seems to be a question about the execution order of the chunk. 🤔

@cderv
Copy link
Collaborator Author

cderv commented Nov 4, 2018

I did not know this was also possible with Latex (see the knitr example)

\documentclass{article}
\newif\ifmyboolean
\begin{document}

<<setup>>=
myboolean='true' # or set it to 'false'
@
% assign true or false to myboolean
\myboolean\Sexpr{myboolean}

\ifmyboolean
hello, foo!
\else
hey, bar!
\fi

\end{document}

@cderv
Copy link
Collaborator Author

cderv commented Apr 10, 2020

Some recipes already added:

  • a068891 about block conditional to package
  • aa37cb1 about asis engine for conditional text in Rmd.

@cderv
Copy link
Collaborator Author

cderv commented Apr 17, 2020

Related to #113

I think we can add more recipes based on examples in questions for conditional execution or echoing of chunks.

Conditional include example already shown with output function : https://bookdown.org/yihui/rmarkdown-cookbook/latex-html.html

@yihui
Copy link
Member

yihui commented Apr 24, 2020

I think these are special cases or applications of https://bookdown.org/yihui/rmarkdown-cookbook/chunk-variable.html so we can probably close this one.

@yihui yihui closed this as completed Apr 24, 2020
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