knitr

2012-06-26 8 views
5

के साथ लेआउट का उपयोग करना मैं knitr के साथ एक मार्कडाउन फ़ाइल में दो भूखंडों के साथ आर में एक एकल आंकड़ा बनाना चाहता हूं। आम तौर पर, layout(t(1:2)) या par(mfrow=c(1,2)) के साथ ऐसा करना आसान है। क्या मैं इसे knitr के साथ कर सकता हूं, या क्या यह हमेशा दो अलग-अलग आंकड़े बनाएगा?knitr

यहाँ एक न्यूनतम काम कर उदाहरण जो दो फ़ाइलों ./figure/junkislands1.png (जो केवल शामिल पहले साजिश) और ./figure/junkislands2.png (जो दोनों भूखंडों कि मैं चाहता हूँ भी शामिल है) के साथ एक फ़ाइल अपने काम करने निर्देशिका में ./junk.Rmd और ./junk.md कहा जाता है बनाता है।

require(knitr) 
temp <- "```{r junkislands, fig.width=8, fig.height=5} 
layout(t(1:2)) 
pie(islands) 
barplot(islands) 
```" 
cat(temp, file="junk.Rmd") 
knit("junk.Rmd", "junk.md") 

समस्या यह है कि दो .png फ़ाइलें बनाता है, लेकिन markdown फ़ाइल junk.md उन दोनों को भी शामिल है बल्कि यह है कि इतना नहीं है।

जब मैं HTML में उस मार्कडाउन को बनाता हूं, तो इसमें दोनों .png फ़ाइलों को शामिल किया जाता है जब मैं केवल दोनों आंकड़ों के साथ एक चाहता हूं।

```r 
par(mfrow = c(1, 2)) 
pie(islands) 
``` 

![plot of chunk junkislands](figure/junkislands1.png) 

```r 
barplot(islands) 
``` 

![plot of chunk junkislands](figure/junkislands2.png) 
+0

कडाई के साथ देता है, इस knitr' (https://github.com/yihui/knitr/issues/292) 'का एक बग है; मैं इसे बाद में ठीक करने की कोशिश करूंगा; धन्यवाद –

+0

मेरे पास [निश्चित] है (https://github.com/yihui/knitr/commit/3508ddb86f0068c17570b3c71dc1e79414041627) विकास संस्करण में यह बग: https://github.com/yihui/knitr#readme आप निश्चित रूप से 'अंजीर का उपयोग कर सकते हैं .keep = 'last'', लेकिन इसके लिए अब और आवश्यकता नहीं होगी। –

उत्तर

11

http://yihui.name/knitr/options पर एक नज़र है और विशेष रूप fig.keep है:

यहाँ फ़ाइल junk.md कि knitr से उत्पन्न किया जाता है। मुझे लगता है कि आप चाहते हैं fig.keep = 'last'

require(knitr) 
temp <- "```{r junkislands, fig.width=8, fig.height=5, fig.keep = 'last'} 
layout(t(1:2)) 
pie(islands) 
barplot(islands) 
```" 
cat(temp, file="junk.Rmd") 
knit("junk.Rmd", "junk.md") 

```r 
layout(t(1:2)) 
pie(islands) 
barplot(islands) 
``` 

![plot of chunk junkislands](figure/junkislands.png) 
+1

सिर्फ एक साइड नोट - आप 'junk.Rmd' पर टेक्स्ट लिखने के बजाय सीधे 'बुनाई (टेक्स्ट = temp)' कॉल कर सकते हैं। –