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

selectively include output of a chunk? #108

Closed
baptiste opened this issue Jan 25, 2012 · 17 comments
Closed

selectively include output of a chunk? #108

baptiste opened this issue Jan 25, 2012 · 17 comments
Labels
feature Feature requests
Milestone

Comments

@baptiste
Copy link

Hi,

I've been using the very handy externalization feature of knitr, where I can conveniently develop the R code, and when it's polished insert the relevant bits inside a Rnw document. A common problem I face is the following: say my analysis is split into a few steps,

1- (lengthy / messy) definition of some functions [ don't show in final report ]

2- call to the function [ show in final report ]

3- (messy / lengthy) creation of the plot using ggplot2 [ don't show in final report ]

4- printing the plot [ only show the plot in the final report, not the print(p) statement ]

In the final report, I often find I don't want to overwhelm the reader (me, in fact) with technical details such as (1) and (3). In the current workflow, this means that I'll have to split this into 4 chunks, even though it's really the same part of the study. Further, I need to worry about setting echo=T/F, include=T/F, dependson= for each of these 4 chunks.

I wonder if this could be simplified somehow, by allowing one of two things:

  • have the option show no text output, but still print plots (meaning I can keep steps 3 and 4 in the same chunk)
  • provide an internal keyword in the form of R comments that defines lines that may be exluded from the echoed code. Something along those lines:
##@knitr messychunk

##@hide
foo = function(...){

## ...

}

##@show
result <- lapply(1:10, foo)
summary(result)

plot(result)

##@hide
p <- ggplot(result) +
 ## ....

##@show
p
@yihui
Copy link
Owner

yihui commented Jan 25, 2012

You may read the main manual again to know the six types of output from a chunk (section 2: https://github.com/downloads/yihui/knitr/knitr-manual.pdf). They are orthogonal to each other, meaning that you can turn on/off each piece without worrying about other pieces.

In your case, you can use results=hide to turn off normal text output without affecting the plots (as long as fig.keep != none, the plots will be there).

For the second problem, at the moment you have to split the code into 4 chunks and use the ugly settings, but this issue has given me an inspiration: currently we can choose which plot to show in a chunk using fig.show, and I believe it is also plausible to selectively show code lines. I may add support to the echo option so that it can take a numeric vector indicating which lines to show, e.g. echo=5:8 to show 5th line to 8th. A binary option (TRUE/FALSE) is often restrictive.

@ramnathv
Copy link
Contributor

I would support this feature. One immediate application I can see is for blogs. I like to explain my code in steps, but want to provide a single downloadable file. Having an option like echo = 5:8, will allow me to use a single source file and generate the blog entry.

@baptiste
Copy link
Author

thanks for the results=hide tip, I had missed it from the manual, somehow. I like your idea of line numbers, my only worry is how these numbers are defined with respect to line wrap, especially with the tidy option. Consider this code,

1. foo = lapply(1:10, function(x) {
2.                                  print(x)
3.                                   })

using echo=1:2, referring to the line numbers in the initial source file where it encompassed the full expression, might mean that after tidying, say, one also has to include line 3. One safe option here might be to include syntactically complete expressions.

@yihui
Copy link
Owner

yihui commented Jan 25, 2012

Good point; echo=5:8 should mean the 5th complete expression up to the 8th instead of lines.

@yihui yihui closed this as completed in 0068578 Jan 26, 2012
@yihui
Copy link
Owner

yihui commented Jan 26, 2012

This feature has been implemented. See echo in http://yihui.github.com/knitr/options

@baptiste
Copy link
Author

That's great, thanks a lot! echo=c(1,3) would be quite useful.

@yihui
Copy link
Owner

yihui commented Jan 26, 2012

The difficulty is that , is reserved as the option separator. One possible future solution is to use ; to replace , and write c(1;3) which is not valid R code. I do not know if users can accept such ugly code. Or do as the documentation tells you: use \Sexpr{show_lines <- c(1,3); NULL}, then

<<echo=show_lines>>=
3
2
5
@ 

@baptiste
Copy link
Author

Oh, I see. That's unfortunate. If you're going to introduce a non-standard notation, I'd suggest not mixing it with pseudo-R code like c( ), and go for something totally different:

  • [3:4;7]

or

  • 3:4+7, in the spirit of using a minus sign to negate values such as -3

@yihui
Copy link
Owner

yihui commented Jan 26, 2012

Before I make this option powerful, I think it is often enough with 3:4 and -3. It may not be desirable to select some discrete pieces of the code to show in the output, which can confuse the reader. Often times you just want to show one expression or a certain number of successive expressions, or want to hide one expression.

@baptiste
Copy link
Author

When I use echo=1:2, fig.show=hold, results=hide I do not see any plots from the chunk. Is this a bug?

@yihui
Copy link
Owner

yihui commented Jan 29, 2012

I did this on purpose. When the source code is removed, its following output is also removed. I was mainly thinking about text output like this:

1 + 1
## [1] 2

I thought it could be weird if the reader saw the output without the corresponding source code.

But you seem to be right -- plots may need to be treated differently.

@baptiste
Copy link
Author

i see, and yes, it would be very useful to keep the plots. Thanks!

@yihui
Copy link
Owner

yihui commented Feb 11, 2012

it is fixed in the devel version 0.2.5

@baptiste
Copy link
Author

thanks!

kohske pushed a commit to kohske/knitr that referenced this issue Jun 21, 2012
@RoyalTS
Copy link

RoyalTS commented Dec 6, 2013

Would it be possible to mark not only the beginning of a chunk in an external file but also it end?

I often have an .R file in which I try out a whole bunch of different things, few of which I would ever want in the final document. If, instead of just being able to declare

## @knitr chunk1

but also have something like

## @knitr end chunk1

(no idea if that's the ideal syntax) I could easily isolate those parts of the script I actually want to appear in the document without having to keep track of the number of expressions in the chunk that I then have to pass to echo

@yihui
Copy link
Owner

yihui commented Dec 7, 2013

The beginning of the next chunk is the end of the previous chunk, so you can just start another chunk, e.g.

# ---- chunk1 ----
code...

# ---- chunk2 ----

code...

@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
feature Feature requests
Projects
None yet
Development

No branches or pull requests

4 participants