knitr:AGeneral-PurposeToolforDynamicReportGenerationinRYihuiXieJanuary6,2013Theoriginalparadigmofliterateprogrammingwasbroughtforwardmainlyforsoftwaredevelopment,orspecically,tomixsourcecode(forcomputer)anddocumentation(forhuman)together.
EarlysystemsincludeWEBandNoweb;Sweave(Leisch,2002)wasderivedfromthelatter,butitislessfocusedondocumentingsoftware,insteaditismainlyusedforreproducibledataanalysisandgeneratingstatisticalreports.
Theknitrpackage(Xie,2012c)isfollowingthestepsofSweave.
Forthismanual,IassumereadershavesomebackgroundknowledgeofSweavetounderstandthetechnicaldetails;forareferenceofavailableoptions,hooksanddemos,seethepackagehomepagehttp://yihui.
name/knitr/.
1HelloWorldAnaturalquestioniswhytoreinventthewheel.
TheshortansweristhatextendingSweavebyhackingSweaveDrivers.
Rintheutilspackageisadifcultjobtome.
Manyfeaturesinknitrcomenaturallyasuserswouldhaveexpected.
Figure1isasimpledemoofsomefeaturesofknitr.
IwouldhavechosentohidetheRcodeifthiswerearealreport,buthereIshowthecodejustforthesakeofdemonstration.
Ifwetypeqplot()inR,wegetaplot,andthesamethinghappensinknitr.
Ifwefitxx[1]-0.
630160.
12716-1.
100260.
07677-0.
10064>var(x)[1]0.
2776Theexamplebelowshowstheeffectoftidy=TRUE/FALSE:##optiontidy=FALSEfor(kin1:10){j=cos(sin(k)*k2)+3;print(j-5)}##optiontidy=TRUEfor(kin1:10){j>=@DifferentdocumentscanreadthesameRscript,sotheRcodecanbereusableacrossdifferentinputdocuments.
3.
5EvaluationofChunkOptionsBydefaultknitrusesanewsyntaxtoparsechunkoptions:ittreatsthemasfunctionargumentsinsteadofatextstringtobesplittoobtainoptionvalues.
Thisgivestheusermuchmorepowerthantheoldsyntax;wecanpassarbitraryRobjectstochunkoptionsbesidessimpleoneslikeTRUE/FALSE,numbersandcharacterstrings.
Thepagehttp://yihui.
name/knitr/demo/sweave/hasgiventwoexamplestoshowtheadvantagesofthenewsyntax.
Hereweshowyetanotherusefulapplication.
Beforeknitr0.
3,therewasafeaturenamed"conditionalevaluation"6.
Theideais,insteadofsettingchunkoptionsevalandechotobeTRUEorFALSE(constants),theirvaluescanbecontrolledbyglobalvariablesinthecurrentRsession.
Thisenablesknitrtoconditionallyevaluatecodechunksaccordingtovariables.
Forexample,hereweassignTRUEtoavariabledothis:dothis<-TRUEInthenextchunk,wesetchunkoptionseval=dothisandecho=!
dothis,botharevalidRexpressionssincethevariabledothisexists.
Aswecansee,thesourcecodeishidden,butitwasindeedevaluated:##[1]"youcannotseemysourcebecause!
dothisisFALSE"Thenweseteval=dothisandecho=dothisforanotherchunk:dothis##[1]TRUEIfwechangethevalueofdothistoFALSE,neitheroftheabovechunkswillbeevaluatedanymore.
Thereforewecancontrolmanychunkswithasinglevariable,andpresentresultsselectively.
Thisoldfeaturerequiresknitrtotreatevalandechospecially,andwecaneasilyseethatitisnolongernecessarywiththenewsyntax:eval=dothiswilltellRtondthevariabledothisautomaticallyjustlikewecallafunctionfoobar(eval=dothis).
Whatismore,alloptionswillbeevaluatedasRexpressionsunlesstheyarealreadyconstantswhichdonotneedtobeevaluated,sothisoldfeaturehasbeengeneralizedtoallotheroptionsnaturally.
3.
6CustomizationTheknitrpackageisreadyforcustomization.
Boththepatternsandhookscanbecustomized;seethepackagewebsitefordetails.
HereIshowanexampleonhowtosaverglplots(AdlerandMurdoch,2012)usingacustomizedhookfunction.
Firstwedeneahooknamedrglusingthefunctionhookrgl()inknitr:6requestfromhttps://plus.
google.
com/u/0/116405544829727492615/posts/43WrRUffjzK9knit_hooks$set(rgl=hook_rgl)head(hook_rgl)#thehookfunctionisdefinedasthis####1function(before,options,envir)##2{##3library(rgl)##4if(before||rgl.
cur()==0)##5return()##6name=fig_path()Thenweonlyhavetosetthechunkoptionrgl=TRUE:library(rgl)demo("bivar",package="rgl",echo=FALSE)par3d(zoom=0.
7)Duetotheexibilityofoutputhooks,knitrsupportsseveraldifferentoutputformats.
Theimplemen-tationisfairlyeasy,e.
g.
,forLATEXweputRoutputinverbatimenvironments,andinHTML,itisonlyamatterofputtingoutputindivlayers.
Thesearesimplycharacterstringoperations.
Manydemosinhttp://yihui.
name/knitr/demosshowthisideaclearly.
Thismanualdidnotcoverallthefeaturesofknitr,andusersareencouragedtothumbthroughthewebsitetoknowmorepossiblefeatures.
4EditorsYoucanuseanytexteditorstowritethesourcedocuments,butsomehavebuilt-insupportforknitr.
BothRStudio(http://www.
rstudio.
org)andLYX(http://www.
lyx.
org)havefullsupportforknitr,andyoucancompilethedocumenttoPDFwithjustoneclick.
Seehttp://yihui.
name/knitr/demo/rstudio/andhttp://yihui.
name/knitr/demo/lyx/respectively.
ItisalsopossibletosupportothereditorslikeEclipse,TexmakerandWinEdt;seethedemolistinthewebsiteforcongurationinstructions.
AboutThisDocumentThismanualwaswritteninLYXandcompiledwithknitr(version0.
9.
5).
TheLYXsourceandtheRnwdocumentexportedfromLYXcanbefoundunderthesedirectories:system.
file("examples","knitr-manual.
lyx",package="knitr")#lyxsourcesystem.
file("examples","knitr-manual.
Rnw",package="knitr")#Rnwsource10Youcanusethefunctionknit()toknittheRnwdocument(remembertoputthetwo.
biblesunderthesamedirectory),andyouneedtomakesurealltheRpackagesusedinthisdocumentareinstalled:install.
packages(c("animation","rgl","tikzDevice","ggplot2"))Feedbackandcommentsonthismanualandthepackagearealwayswelcome.
Bugreportsandfeaturerequestscanbesenttohttps://github.
com/yihui/knitr/issues,andquestionscanbedeliveredtothemailinglisthttps://groups.
google.
com/group/knitr.
ReferencesAdlerD,MurdochD(2012).
rgl:3Dvisualizationdevicesystem(OpenGL).
Rpackageversion0.
92.
894,URLhttp://CRAN.
R-project.
org/package=rgl.
BrackenC,SharpsteenC(2012).
pgfSweave:QualityspeedygraphicscompilationandcachingwithSweave.
Rpackageversion1.
3.
0,URLhttp://CRAN.
R-project.
org/package=pgfSweave.
FrancoisR(2012).
highlight:Syntaxhighlighter.
Rpackageversion0.
3.
2,URLhttp://CRAN.
R-project.
org/package=highlight.
LeischF(2002).
"Sweave:DynamicGenerationofStatisticalReportsUsingLiterateDataAnalysis.
"InCOMPSTAT2002ProceedingsinComputationalStatistics,69,pp.
575–580.
PhysicaVerlag,Heidelberg.
PengRD(2012).
cacheSweave:ToolsforcachingSweavecomputations.
Rpackageversion0.
6-1,URLhttp://CRAN.
R-project.
org/package=cacheSweave.
SharpsteenC,BrackenC(2012).
tikzDevice:ADeviceforRGraphicsOutputinPGF/TikZFormat.
Rpackageversion0.
6.
2,URLhttp://CRAN.
R-project.
org/package=tikzDevice.
WickhamH(2012).
evaluate:Parsingandevaluationtoolsthatprovidemoredetailsthanthedefault.
Rpackageversion0.
4.
3.
XieY(2012a).
animation:Agalleryofanimationsinstatisticsandutilitiestocreateanimations.
Rpackageversion2.
1.
1,URLhttps://github.
com/yihui/animation.
XieY(2012b).
formatR:FormatRCodeAutomatically.
Rpackageversion0.
7,URLhttp://yihui.
name/formatR.
XieY(2012c).
knitr:Ageneral-purposepackagefordynamicreportgenerationinR.
Rpackageversion0.
9,URLhttp://yihui.
name/knitr/.
11
萤光云怎么样?萤光云是一家国人云厂商,总部位于福建福州。其成立于2002年,主打高防云服务器产品,主要提供福州、北京、上海BGP和香港CN2节点。萤光云的高防云服务器自带50G防御,适合高防建站、游戏高防等业务。目前萤光云推出北京云服务器优惠活动,机房为北京BGP机房,购买北京云服务器可享受6.5折优惠+51元代金券(折扣和代金券可叠加使用)。活动期间还支持申请免费试用,需提交工单开通免费试用体验...
【双十二】兆赫云:全场vps季付六折优惠,低至50元/季,1H/1G/30M/20G数据盘/500G流量/洛杉矶联通9929商家简介:兆赫云是一家国人商家,成立2020年,主要业务是美西洛杉矶联通9929线路VPS,提供虚拟主机、VPS和独立服务器。VPS采用KVM虚拟架构,线路优质,延迟低,稳定性强。是不是觉得黑五折扣力度不够大?还在犹豫徘徊中?这次为了提前庆祝双十二,特价推出全场季付六折优惠。...
昨天,有在"阿里云秋季促销活动 轻量云服务器2G5M配置新购年60元"文章中记录到阿里云轻量服务器2GB内存、5M带宽一年60元的活动,当然这个也是国内机房的。我们很多人都清楚备案是需要接入的,如果我们在其他服务商的域名备案的,那是不能解析的。除非我们不是用来建站,而是用来云端的,是可以用的。这不看到其对手腾讯云也有推出两款轻量服务器活动。其中一款是4GB内存、8M带宽,这个比阿里云还要狠。这个真...
syntaxhighlighter为你推荐
中南财经政法大学知识产权研究中心设置media支持ipadgetIntjava重庆宽带测速重庆电信测速我的网速溢出xp系统关闭445端口xp中,如何关闭掉一些没有用的端口,请高手解答?联通iphone4联通iphone4怎么样,好不好用?谷歌sbgoogle一下"SB",虽然显示的是baidu排第一,链接的不是baidu.csshackcss中 *bottom是什么意思?css3按钮HTML中,怎么表示一个图片按钮
com域名价格 域名服务器上存放着internet主机的 美国独立服务器 enom fastdomain Vultr php主机 哈喽图床 免费博客空间 国外免费空间 网通服务器ip 韩国网名大全 权嘉云 admit的用法 isp服务商 t云 web服务器安全 上海联通宽带测速 789 论坛主机 更多