knitr

2012-04-28 17 views
7

का उपयोग कर आर के भीतर से एचटीएमएल लिखना शायद मुझे स्पष्ट याद आ रही है, लेकिन मैं निम्नलिखित के लिए एक उदाहरण ढूंढने के लिए संघर्ष कर रहा हूं: मैं knitr पैकेज का उपयोग कर आरटीएम में अपने विश्लेषण की रिपोर्ट लिखना चाहता हूं। मुझे stitch() फ़ंक्शन मिला है, हालांकि & प्लॉट्स HTML पर लिखे गए हैं और जो नहीं हैं, इसके बारे में अधिक नियंत्रण रखना अच्छा लगेगा। मुझे लगता है कि मुझे समझ नहीं आता कि आप वास्तव में क्या कमी हैknitr

# some dummy code 
library(ggplot) 
data <- read.table('/Users/mydata', header=TRUE) 
model <- lm(Y~X*Y, data) 

# write this result to html: 
summary(model) 
+0

संकेत के लिए धन्यवाद। लेकिन यह उदाहरण नहीं दिखाता है कि आप एक HTML फ़ाइल के भीतर आर-कोड कैसे एम्बेड करते हैं। मैं एक एचटीएमएल फाइल बनाना चाहता हूं और आर के अंदर सामग्री लिखना चाहता हूं। सिलाई() की तरह ही, केवल इतना है कि मैं सिर्फ HTML फ़ाइल में सब कुछ लिखने की तुलना में अधिक लचीलापन लेना चाहता हूं। –

उत्तर

7

, लेकिन यहाँ एक न्यूनतम उदाहरण मैं पकाया है: प्रिंसिपल में मैं निम्नलिखित कोड करने के लिए सक्षम होने के लिए करना चाहते हैं। इसे चलाने के लिए

library(knitr) 
knit("r-report.html") 

और उदाहरण।

<HTML> 
<HEAD> 
    <TITLE>Analyzing Diamonds!</TITLE> 
</HEAD> 

<BODY> 
<H1>Diamonds are everywhere!</H1> 

<!--begin.rcode echo=FALSE 

## Load libraries, but do not show this 
library(ggplot2) 
library(plyr) 
testData <- rnorm(1) 
end.rcode--> 

This is an analysis of diamonds, load the data.<p> 
<!--begin.rcode echo=TRUE, fig.keep="all" 
# Load the data 
data(diamonds) 
# Preview 
head(diamonds) 
end.rcode--> 

Generate a figure, don't show code <p> 
<!--begin.rcode echo=FALSE, fig.align="center", dev="png" 
# This code is not shown, but figure will output 
qplot(diamonds$color, fill=diamonds$color) + 
    opts(title="A plot title") 
end.rcode--> 

Show some code, don't output the figure<p> 
<!--begin.rcode echo=TRUE, fig.keep="none" 
# Show the code for this one, but don't write out the figure 
ggplot(diamonds, aes(carat, price, colour=cut)) + 
    geom_point(aes(alpha=0.9)) 
end.rcode--> 


And the value testData: <!--rinline testData --> inside a text block. 

</BODY> 
</HTML> 
6

लेखन एचटीएमएल आर के भीतर (@dready एक सभ्य उदाहरण दिया है) और अधिक एक टेम्पलेट और यह knit() लिखने की तुलना में मेरी आँखों में श्रमसाध्य है। कोड बदसूरत होगा, और आप चारों ओर कूदने वाली कई "बिल्लियों" देखेंगे। आप कुछ इस तरह से खत्म हो सकता है:

sink('test.html') # redirect output to test.html 
cat('<h1>First level header</h1>\n') 
cat('<pre>') 
summary(mtcars) 
cat('</pre>\n') 
sink() 
browseURL('test.html') 

वैसे भी, वहाँ एक और पैकेज R2HTML जो इस मामले में अधिक उपयुक्त हो सकता है।

संबंधित मुद्दे