Home Comments Thread
New Thread

6 Comments

giscus-bot giscus-bot 2022-12-16 22:18:29
访客 *Xiasw0601* @ 2012-04-21 02:40:50 写道:

LyX下如何使用read_chunk()?我尝试着用Rnw同样设置,无法工作!

giscus-bot giscus-bot 2022-12-16 22:18:34
Guest *Xiasw0601* @ 2012-04-21 02:50:39 originally posted:

It works, thank you!

giscus-bot giscus-bot 2022-12-16 22:18:29
Guest *Joshua Wiley* @ 2012-05-14 18:21:22 originally posted:

 Is there anyway to use chunk references to create a nonlinear page?  The specific example I have in mind is that I would like to show all the packages used to create a page at the very top.  I load packages throughout the page as they are needed.  If I run sessionInfo() at the very end, it has all the output that I want, however, I would like that output at the beginning.

yihui yihui 2022-12-16 22:18:35

The references can be nonlinear -- you can put the code in the back and reference it in the front, but the order of execution has to be linear at the moment. You can file an issue to my Github repository if you really want it so that I may think about it in the future. Thanks!

Originally posted on 2012-05-15 04:49:08

giscus-bot giscus-bot 2022-12-16 22:18:30
Guest *ADPowers* @ 2012-11-30 00:28:22 originally posted:

I really like this in principal, since R code blocks can be substantial (especially when creating intricate plots), which means one should be able to inoculate the cumbersome code, then simply make reference to it in the actual document (think say a dedicated 'Plots' .rnw), my question though is.

If I am using tikz device to put a plot directly into the document, there is a problem with the sequencing. Example below creates the plot in the first code block, with the results being printed in the second.

%CHUNK NUMBER 1
<<demo,eval=true,echo=false,external=false,results='hide',dev='tikz'>>=
set.seed(1213) # for reproducibility
x <- cumsum(rnorm(100))
x
mean(x) # mean of x
hist(x) # Histogram
@

%CHUNK NUMBER 2
<<demo,echo=false,results='markup'>>=
@

Which prevents the inoculation concept from being effective. The intended result (in this trivial example) is to have the printed data followed by the plot, whereas, what is happening here, is the plot is being printed in the first block, and not in the second. The data is being suppressed in the first, then marked up in the second.

If the first block was held in a separate .RNW file along with all the other plotting chunks, the issue would become crystal clear, since all of the plots would be printed and grouped together at one incorrect location, with the related data scattered through the document as instructed.

Any ideas?

giscus-bot giscus-bot 2022-12-16 22:18:35
Guest *ADPowers* @ 2012-11-30 01:40:36 originally posted:

Actually I just figured it out. Absolutely Fantastic.

Turns out to be pretty simple.

The source chunk needs to have options eval = FALSE, echo = FALSE

The reference chunk needs to have eval = TRUE, echo = (TRUE/FALSE depending on preference), and, external = FALSE

NOTE: the reference chunk, if external is selected as TRUE, will not insert tikz object into the LaTeX document, rather ports it to pdf into user.dir, for me this is not viable, since, at this point, the object is incomplete (?? for natbib citations) which only get swept away and replaced, fonts modified when inserted into the document.

giscus-bot giscus-bot 2022-12-16 22:20:37
Guest *ADPowers* @ 2012-11-30 04:32:56 originally posted:

but then if I try to use fig.cap = 'xyz' in the referring chunk, it goes back to the original problem. S**t.
.
.
.
fixed with fig.pos = 'h'.

yihui yihui 2022-12-16 22:20:37

This case (bib references) is too complicated for external=TRUE.

Originally posted on 2012-11-30 07:20:34

giscus-bot giscus-bot 2022-12-16 22:18:31
Guest *ADPowers* @ 2012-11-30 05:51:02 originally posted:

put a run_chunk('c') after x = 2

yihui yihui 2022-12-16 22:20:38

Then there will be infinite recursion.

Originally posted on 2012-11-30 07:23:05

giscus-bot giscus-bot 2022-12-16 22:20:39
Guest *ADPowers* @ 2012-11-30 07:26:56 originally posted:

Yes I know. Does anyone have a computer fast enough to solve?...;-)

I really wish I could get my e-banking to do that with refunds.

giscus-bot giscus-bot 2022-12-16 22:18:32
Guest *David Forrest* @ 2014-08-03 14:50:39 originally posted:

Cool --- You can use a vector of multiple chunk references in ref.label like ref.label=c('libraries',setup',helpers'...)

giscus-bot giscus-bot 2022-12-16 22:18:33
Guest *Januz* @ 2014-12-11 22:39:43 originally posted:

Can I reference chunks from another Rmarkdown file? I'd like to do all my preliminary analyses in a Rmarkdown file and then reference the important bits in the final report/paper... Thanks!

yihui yihui 2022-12-16 22:20:40

If the code chunks in the other R Markdown file are labeled, you may purl() the Rmd file to get an R script, and use read_chunk() in the current Rmd file to read the chunks.

You may also just store the code to be re-used in multiple source documents in an R script: http://yihui.name/knitr/demo/externalization/

Originally posted on 2014-12-12 03:31:29

Sign in to join the discussion

Sign in with GitHub