i'm having lots of fun knitr noticed reusing code in bad way - cut , paste. in example want load dataset, calculate statistics , print those, , plot dataset -- easy few chunks, if want same thing dataset have copy , paste chunks , change name of dataset.
suppose have :
<p>load dataset <tt>dataset01</tt></p> <!--begin.rcode load-dataset01 # create alias there no need change several times in # chunks mydataset <- dataset01 <- calcsomestats(mydataset) input <- mydataset[,1:2] ideal <- class.ind(mydataset$label) end.rcode--> <p>now let's plot it</p> <!--begin.rcode plot-dataset01, fig.width=10, fig.height=10 neurons <- 1 nnet = nnet(input, ideal, size=neurons,softmax=true) plotnet(nnet) par(pty="s",xpd=t, mar=par()$mar+c(0,0,0,2)) axis(1, @ = seq(bbox[1],bbox[2], = 2), las=1) axis(2, @ = seq(bbox[1],bbox[2], = 2), las=2) points(mydataset$x,mydataset$y, col=mypal[unclass(mydataset$label)],cex=2,pch=16) legend("topright", levels(factor(mydataset$label)),fill=mypal,inset=c(-0.1,0)) end.rcode--> code not complete, there other parts still developing, working.
my question is, considering 2 chunks shown code above, best (or riest) way reuse it? suppose have list of dozens of datasets , want run same chunks on them, possible substituting non-r, html parts. possible?
i've naively tried create function since starts this:
<!--begin.rcode abc <- function(n) { <!--begin.rcode howdoinamethischunkwithanuniquename n <- n*2 end.rcode--> } end.rcode--> it did not work (error: unexpected end of input)
thanks rafael
edit: there similar questions answers in using loops knitr produce multiple pdf reports... need little me on hump , https://github.com/yihui/knitr/issues/435 cover latex and/or r markdown, not html.
another edit: things i've tried after @yuhui comment:
using same label both chunks
<!--begin.rcode chunka, echo=true, results='hide' x <- rnorm(100) end.rcode--> <p>plot it?</p> <!--begin.rcode chunka, echo=false, results='markup' mean(x) end.rcode--> with "error in parse_block(g[-1], g[1], params.src) : duplicate label 'chunka'" message.
using chunk option ref.label
<!--begin.rcode chunka, echo=true, results='hide' x <- rnorm(100) end.rcode--> <p>plot it?</p> <!--begin.rcode chunkb, ref.label='chunka', echo=false, results='markup' mean(x) end.rcode--> with r code (x <- rnorm(100)), "plot it?" , nothing. changing echo true repeat (x <- rnorm(100)).
more information
my scenario having several small data frames have same structure (x,y,label) , want process them in chunk "a" , plot them similar parameters in chunk "b". if without reusing code, have copy-and-paste chunks "a" , "b" several times, not idea.
i know cannot pass parameter html chunk, , recipes @ http://yihui.name/knitr/demo/reference/ seems close need, cannot figure out how them in r+html.
ok, got it, , posting serve example.
from understand, not possible create knitr chunk works function. so, not possible:
<!--begin.rcode fakefunction # mydata, assume defined! end.rcode--> <!--begin.rcode myplot1 ref.label='fakefunction' mydata <- iris # assume fakefunction executed somehow iris end.rcode--> <!--begin.rcode myplot2 ref.label='fakefunction' mydata <- cars # assume fakefunction executed somehow cars end.rcode--> what will work this:
<!--begin.rcode mydata <- iris end.rcode--> <!--begin.rcode plot summary(mydata) end.rcode--> <!--begin.rcode mydata <- cars end.rcode--> <!--begin.rcode plot2, ref.label='plot' end.rcode--> basically we're saying chunk plot2 "paste" code chunk plot. don't need define else in plot2, , guess ignored anyway.
i haven't figured out detail, though. suppose have chunk plot working ok (imagine dozens of lines of r code) , want slight different behavior in plot2, impact single line of code. understand won't able knitr -- knows how reuse code writing chunks procedures or functions?
Comments
Post a Comment