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
Nocser刚刚在WHT发布了几款促销服务器,Intel Xeon X3430,8GB内存,1TB HDD,30M不限流量,月付$60.00。Nocser是一家注册于马来西亚的主机商,主要经营虚拟主机、VPS和马来西亚独立服务器业务,数据中心位于马来西亚AIMS机房,线路方面,AIMS到国内电信一般,绕日本NTT;联通和移动比较友好,联通走新加坡,移动走香港,延迟都在100左右。促销马来西亚服务器...
在之前的一些文章中有提到HostYun商家的信息,这个商家源头是比较老的,这两年有更换新的品牌域名。在陆续的有新增机房,价格上还是走的低价格路线,所以平时的折扣力度已经是比较低的。在前面我也有介绍到提供九折优惠,这个品牌商家就是走的低价量大为主。中秋节即将到,商家也有推出稍微更低的88折。全场88折优惠码:moon88这里,整理部分HostYun商家的套餐。所有的价格目前都是原价,我们需要用折扣码...
华纳云怎么样?华纳云是香港老牌的IDC服务商,成立于2015年,主要提供中国香港/美国节点的服务器及网络安全产品、比如,香港服务器、香港云服务器、香港高防服务器、香港高防IP、美国云服务器、机柜出租以及云虚拟主机等。以极速 BGP 冗余网络、CN2 GIA 回国专线以及多年技能经验,帮助全球数十万家企业实现业务转型攀升。华纳云针对618返场活动,华纳云推出一系列热销产品活动,香港云服务器低至3折,...
syntaxhighlighter为你推荐
版本itunes重要产品信息指南支持ipad支持ipad齐鲁工业大学高水平学科建设专项boxiphonecolumnios5127.0.0.1DNS老是被修改为127.0.0.1,这是为什么?x-router设置路由器是我的上网设置是x怎么弄x-routerx-0.4x等于多少?
万网域名管理 国外服务器 光棍节日志 国外空间 1g内存 ftp免费空间 idc查询 免费网页空间 新世界服务器 独享主机 阿里dns 广州服务器托管 hosts文件 连连支付 主机箱 小米电视主机 网易轻博客 qq空间技术网 灵动鬼影实录4 摇号申请网站 更多