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

asis_output does not work in loop #1137

Closed
renkun-ken opened this issue Dec 4, 2015 · 4 comments
Closed

asis_output does not work in loop #1137

renkun-ken opened this issue Dec 4, 2015 · 4 comments
Labels
doc Need to improve documentation
Milestone

Comments

@renkun-ken
Copy link
Sponsor

In a normal Rmd document, the following code works:

asis_output("## hello\n")  

but this does not work:

for (i in 1:3) {
  asis_output("## hello\n\n")  
}

When I try to create a group of knit_printable objects in a for-loop, it can be very unnatural to print every object. Is it possible to make it easier to do so?

@yihui
Copy link
Owner

yihui commented Dec 6, 2015

Unfortunately no, it is not possible. asis_output() only works in top-level R expressions. To create as-is output from a loop, you have to either use the chunk option results = 'asis' (but this option applies to all output in this code chunk, i.e. you cannot have both as-is output and verbatim output in the same chunk), or collect the character results in a loop, and write them out using asis_output() after the loop.

@yihui yihui added this to the v1.12 milestone Dec 6, 2015
@yihui yihui added the doc Need to improve documentation label Dec 6, 2015
@renkun-ken
Copy link
Sponsor Author

Thanks for the explanation!

@yihui yihui closed this as completed in d9c29a5 Dec 6, 2015
@1beb
Copy link
Contributor

1beb commented Jan 14, 2016

knit_expand is also a good solution to this type of expansion problem. You can use a template and cycle your code through over and over.

@github-actions
Copy link

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
doc Need to improve documentation
Projects
None yet
Development

No branches or pull requests

3 participants