大智慧龍軟DTSR語言量化投資云培訓(xùn)RStudio學(xué)習(xí)手冊(中文翻譯)_第1頁
大智慧龍軟DTSR語言量化投資云培訓(xùn)RStudio學(xué)習(xí)手冊(中文翻譯)_第2頁
大智慧龍軟DTSR語言量化投資云培訓(xùn)RStudio學(xué)習(xí)手冊(中文翻譯)_第3頁
大智慧龍軟DTSR語言量化投資云培訓(xùn)RStudio學(xué)習(xí)手冊(中文翻譯)_第4頁
大智慧龍軟DTSR語言量化投資云培訓(xùn)RStudio學(xué)習(xí)手冊(中文翻譯)_第5頁
已閱讀5頁,還剩48頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

WorkingintheConsole控制臺中的工作Overview概述TheRStudioconsoleincludesavarietyoffeaturesintendedtomakeworkingwithRmoreproductiveandstraightforward.Thisarticlereviewsthesefeatures.LearningtousethesefeaturesalongwiththerelatedfeaturesavailableintheSourceandHistorypanescanhaveasubstantialpayoffinyouroverallproductivitywithR.RStudio控制臺包含許多使R工作更為有效和直觀的特征。本文綜述這些特征。學(xué)習(xí)使用這些特征以及Source和History窗口中可用的相關(guān)特征可以對你使用R的效率有實際性的幫助。CodeCompletion代碼補(bǔ)全RStudiosupportstheautomaticcompletionofcodeusingtheTabkey.Forexample,ifyouhaveanobjectnamedpollResultsinyourworkspaceyoucantypepollandthenTabandRStudiowillautomaticallycompletethefullnameoftheobject.RStudio支持使用Tab鍵來自動補(bǔ)全代碼。例如,如果你在工作空間中有名為pollResults的對象,你可輸入poll然后按Tab鍵,RStudio將自動完成該對象的全名。Thecodecompletionfeaturealsoprovidesinlinehelpforfunctionswheneverpossible.Forexample,ifyoutypedsubthenpressedTabyouwouldsee:代碼補(bǔ)全特征同樣提供了盡可能的嵌入式幫助。例如,如果你可輸入sub然后按Tab鍵,你將看到:Codecompletionalsoworksforfunctionarguments,soifyoutypedsubset(andthenpressedTabyou'dseethefollowing:代碼補(bǔ)全還可以對參數(shù)功能進(jìn)行工作,如果你可輸入subset(然后按Tab鍵,你將看到:RetrievingPreviousCommands檢索以前命令A(yù)syouworkwithRyou'lloftenwanttore-executeacommandwhichyoupreviouslyentered.AswiththestandardRconsole,theRStudioconsolesupportstheabilitytorecallpreviouscommandsusingthearrowkeys:當(dāng)你使用R進(jìn)行工作,你將經(jīng)??奢斎胫匦聢?zhí)行你之前輸入的命令。與標(biāo)準(zhǔn)的R控制臺一樣,RStudio控制臺支持使用以下方向鍵回憶之前命令的功能:Up—Recallpreviouscommand(s)Down—ReverseofUpUp—回憶前一條命令;Down—回Up相反;IfyouwishtoreviewalistofyourrecentcommandsandthenselectacommandfromthislistyoucanuseCtrl+Uptoreviewthelist(notethatontheMacyoucanalsouseCommand-Up):如果你希望回顧你當(dāng)前的命令列表并從中選擇一條命令,則可使用Ctrl+Up來回顧命令列表(注意:在Mac你還可使用Command-Up)Youcanalsousethissamekeyboardshortcuttoquicklysearchforcommandsthatmatchagivenprefix.Forexample,tosearchforpreviousinstancesoftheplotfunctionsimplytypeplotandthenCtrl+Up:你還可以使用同樣的快捷鍵來快速查找匹配給定前綴的命令。例如,查找前面出現(xiàn)過的plot函數(shù),先輸入plot然后Ctrl+UpConsoleTitleBar控制臺標(biāo)題欄ThisscreenshotillustratesafewadditionalcapabilitiesprovidedbytheConsoletitlebar:這個截屏闡明了控制臺標(biāo)題欄的一些額外功能。Displayofthecurrentworkingdirectory.TheabilitytointerruptRduringalongcomputation.MinimizingandmaximizingtheConsoleinrelationtotheSourcepane(usingthebuttonsatthetop-rightorbydouble-clickingthetitlebar).顯示當(dāng)前工作目錄;能夠在一個長期的計算里中斷R;最小化和最大化Console和Source窗口(使用右上按鈕或雙擊標(biāo)題欄)KeyboardShortcuts快捷鍵Beyondthehistoryandcode-completionorientedkeyboardshortcutsdescribedabove,thereareawidevarietyofothershortcutsavailable.Someofthemoreusefulshortcutsinclude:除了上述的歷史和代碼補(bǔ)全快捷鍵外,還有很多其他快捷鍵可用,其中最為有用的快捷鍵包括Ctrl+1—MovefocustotheSourceEditorCtrl+2—MovefocustotheConsoleCtrl+L—CleartheConsoleEsc—InterruptRCtrl+1—移動焦點到Source編輯器。Ctrl+2—移動焦點到Console。Ctrl+L—清理控制臺;Esc—中斷RYoucanfindalistofallshortcutsintheKeyboardShortcutsarticle.你可從KeyboardShortcuts文中找到所有快捷鍵列表。RelatedTopics相關(guān)主題EditingandExecutingCode編輯和執(zhí)行代碼UsingCommandHistory使用命令歷史

EditingandExecutingCode編輯執(zhí)行代碼Overview概述RStudio'ssourceeditorincludesavarietyofproductivityenhancingfeaturesincludingsyntaxhighlighting,codecompletion,multiple-fileediting,andfind/replace.RStudio的souce編輯器包含各種提高效率的特征,包括語法高亮顯示,代碼自動補(bǔ)全,多文件編輯以及查找和替換。RStudioalsoenablesyoutoflexiblyexecuteRcodedirectlyfromthesourceeditor.FormanyRdevelopersthisrepresentstheirpreferredwayofworkingwithR.Byexecutingcommandsfromwithinthesourceeditorratherthantheconsoleitismucheasiertoreproducesequencesofcommandsaswellaspackagethemforre-useasafunction.ThesefeaturesaredescribedintheExecutingCodesectionbelow.RStudio還可以使你直接通過source編輯器靈活地執(zhí)行R代碼。對于許多R開發(fā)者來說,這是他們使用R的首選方式。通過source編輯器執(zhí)行命令相對于控制臺來說更便于復(fù)制命令序列和將其做為再次使用的函數(shù)進(jìn)行打包;這些特征將在后面的執(zhí)行代碼部分講解。ManagingFiles文件管理RStudiosupportssyntaxhighlightingandotherspecializedcode-editingfeaturesforthefollowingtypesoffiles:Rstudio支持語法高亮顯示和其他專業(yè)化的代碼編輯功能,針對以下類型文件。RscriptsR腳本文件SweavedocumentsSweave文件TeXdocumentsTex文件TocreateanewfileyouusetheFile->Newmenu:你可通過File->New菜單創(chuàng)建新文件。(你也可使用Ctrl+Shift+N快捷鍵)。ToopenanexistingfileyouuseeithertheFile->OpenmenuortheOpenRecentmenutoselectfromrecentlyopenedfiles.你可通過File->Open菜單或者OpenRecent菜單選擇來打開已有文件。(你也可使用Ctrl+O快捷鍵)。IfyouopenseveralfileswithinRStudiotheyareallavailableastabstofacilitatequickswitchingbetweenopendocuments.Ifyouhavealargenumberofopendocumentsyoucanalsonavigatebetweenthemusingthe>>icononthetabbarortheView->SwitchtoTab)menuitem:如果你通過RStudio打開許多文件,那么他們都可以通過標(biāo)簽進(jìn)行快速切換。如果你有大量的打開文件,你也可在它們間通過標(biāo)簽欄中的>>圖標(biāo)來進(jìn)行導(dǎo)航,或者View->SwitchtoTab菜單項(你也可使用Ctrl+O快捷鍵)。CodeCompletion代碼補(bǔ)全RStudiosupportstheautomaticcompletionofcodeusingtheTabkey.Forexample,ifyouhaveanobjectnamedpollResultsinyourworkspaceyoucantypepollandthenTabandRStudiowillautomaticallycompletethefullnameoftheobject.RStudio可以使用Tab鍵來支持代碼自動補(bǔ)全,例如,如果你在工作空間中有名為pollResults的對象,你可輸入poll然后按Tab鍵,RStudio將自動完成該對象的全名。Codecompletionalsoworksintheconsole,andmoredetailsonusingitcanbefoundtheconsoleCodeCompletiondocumentation.代碼補(bǔ)全同樣在控制臺中工作,具體用法可間控制臺代碼自動完成CodeCompletion文檔。FindandReplace查找和替換RStudiosupportsfindingandreplacingtextwithinsourcedocuments:Rstudio支持在source文件中查找和替換。FindandreplacecanbeopenedusingtheCtrl+Fshortcutkey,orfromtheEdit->FindandReplacemenuitem.可使用Ctrl+F快捷鍵來打開查找和替換欄,或者使用Edit->FindandReplace菜單項。ExtractFunction提取函數(shù)RStudiocananalyzeaselectionofcodefromwithinthesourceeditorandautomaticallyconvertitintoare-usablefunction.Any"free"variableswithintheselection(objectsthatarereferencedbutnotcreatedwithintheselection)areconvertedintofunctionarguments:RStudio可以在source編輯器中分析一組選擇的代碼,并自動將其轉(zhuǎn)化成再次使用的函數(shù)。任何選擇中的"free"變量(選擇引用對象但不創(chuàng)建)將轉(zhuǎn)化為函數(shù)參數(shù)。(你也可使用Ctrl+Shift+U快捷鍵)。Comment/Uncomment注釋/取消注釋YoucancommentanduncommententireselectionsofcodeusingtheEdit->Comment/UncommentLinesmenuitem(youcanalsodothisusingtheCtrl+/keyboardshortcut):你可使用使用Edit->Comment/UncommentLines菜單項來對所選的整個代碼進(jìn)行注釋或取消注釋(你也可使用Ctrl+Shift+C快捷鍵)。Indentation首行縮進(jìn)AsyouwriteRcodeinRStudioitisautomaticallyindentedaccordingtothecurrentindentationoptions(seeCustomizingRStudio).Rcodeisalsore-indented:如果你在RStudio中寫R代碼,他將自動根據(jù)當(dāng)前的縮進(jìn)選項(見CustomizingRStudio)進(jìn)行縮進(jìn)。R代碼也可再縮進(jìn)。Whenevernewcodeispastedintoasourcedocument.WhentheReindentLinescommand(picturedabove)isinvoked.當(dāng)新代碼黏貼到一個source文件中;當(dāng)縮進(jìn)行命令(上圖)被調(diào)用NotethatRStudioautomaticindentationisRsyntax-awareandisthereforeonlyusedonsourcefilescontainingRcode.注意:RStudio自動縮進(jìn)針對的是R語法意識,因此,只有在包含R代碼的source文件中使用ExecutingCode執(zhí)行代碼RStudiosupportsthedirectexecutionofcodefromwithinthesourceeditor(theexecutedcommandsareinsertedintotheconsolewheretheiroutputalsoappears).RStudio支持從source編輯器中直接執(zhí)行代碼(執(zhí)行代碼將插入控制臺,并在此輸出結(jié)果),ExecutingaSingleLine執(zhí)行一行代碼ToexecutethelineofsourcecodewherethecursorcurrentlyresidesyoupresstheCtrl+Enterkey(orusetheRuntoolbarbutton):你可通過Ctrl+Enter鍵來執(zhí)行當(dāng)前光標(biāo)所在行的source代碼(或者使用Run工具條按鈕)Afterexecutingthelineofcode,RStudioautomaticallyadvancesthecursortothenextline.Thisenablesyoutosingle-stepthroughasequenceoflines.執(zhí)行完一行代碼,RStudio自動將光標(biāo)跳到下一行,這將使你可以在一系列行中單步執(zhí)行代碼。ExecutingMultipleLines執(zhí)行多行代碼Therearethreewaystoexecutemultiplelinesfromwithintheeditor:有三種方法從編輯器中執(zhí)行多行代碼。SelectthelinesandpresstheCtrl+Enterkey(orusetheRuntoolbarbutton);orAfterexecutingaselectionofcode,usetheRe-RunPreviousRegioncommand(oritsassociatedtoolbarbutton)torunthesameselectionagain.Notethatchangestotheselectionincludingadditional,removal,andmodificationoflineswillbereflectedinthissubsequentrunoftheselection.ToruntheentiredocumentpresstheCtrl+Shift+Enterkey(orusetheSourcetoolbarbutton).選擇這些行,按Ctrl+Enter鍵(或者使用Run工具條按鈕)執(zhí)行選擇的代碼后,使用Re-RunPreviousRegion命令(或相應(yīng)的工具欄按鈕)再次運(yùn)行相同的選擇。注意:現(xiàn)在發(fā)生變化,包括增增加、刪除和修改行將反映到后續(xù)運(yùn)行中。(或者使用Ctrl+Shift+P快捷鍵)運(yùn)行整個文件按Ctrl+Shift+Enter鍵(或使用source工具條按鈕)??ThedifferencebetweenrunninglinesfromaselectionandinvokingSourceisthatwhenrunningaselectionalllinesareinserteddirectlyintotheconsolewhereasforSourcethefileissavedtoatemporarylocationandthensourcedintotheconsolefromthere(therebycreatinglessclutterintheconsole).從選擇的行進(jìn)行運(yùn)行和調(diào)用Source的區(qū)別在于,當(dāng)運(yùn)行所選的所有行將直接插入到控制臺中,而對于Source文件則保存到臨時位置然后從那里反映到控制臺(因此在控制臺中產(chǎn)生較少的雜亂)。SourceonSave資源保存Wheneditingre-usablefunctions(asopposedtofreestandinglinesofR)youmaywishtosettheSourceonSaveoptionforthedocument(availableonthetoolbarnexttotheSaveicon).Enablingthisoptionwillcausethefiletoautomaticallybesourcedintotheglobalenvironmenteverytimeitissaved.當(dāng)編輯一個再次調(diào)用函數(shù)(相對于獨立的R行而言),你可能希望對文件設(shè)置SourceonSave選項(通過工具條中Save圖標(biāo)的后一項實現(xiàn))。啟用該選項導(dǎo)致文件每次保存都將被全球自動采用。SettingSourceonSaveensuresthatthecopyofafunctionwithintheglobalenvironmentisalwaysinsyncwithitssource,andalsoprovidesagoodwaytoarrangeforfrequentsyntaxcheckingasyoudevelopafunction.設(shè)置SourceonSave選項確保該函數(shù)的版本在全球環(huán)境總是能夠資源同步,并且也能在你開發(fā)一個函數(shù)是提供一種好的方式安排頻繁的語法檢查。KeyboardShortcuts快捷鍵Beyondthekeyboardshortcutsdescribedabove,thereareawidevarietyofothershortcutsavailable.Someofthemoreusefulonesinclude:除了上述的快捷鍵外,還有很多其他快捷鍵可用,其中最為有用的快捷鍵包括。Ctrl+Shift+N—Newdocument新文檔Ctrl+O—Opendocument打開文檔Ctrl+S—Saveactivedocument保存活動文檔Ctrl+1—MovefocustotheSourceEditor移動焦點到Source編輯器Ctrl+2—MovefocustotheConsole移動焦點到控制臺YoucanfindalistofallshortcutsintheKeyboardShortcutsarticle.

RelatedTopics相關(guān)主題WorkingintheConsole在控制臺中工作UsingCommandHistory使用歷史命令NavigatingCode代碼導(dǎo)航CodeFoldingandSections代碼折疊和截取

CodeFoldingandSections代碼折疊和截取CodeFolding代碼折疊RStudiosupportsbothautomaticanduser-definedfoldingforregionsofcode.Codefoldingallowsyoutoeasilyshowandhideblocksofcodetomakeiteasiertonavigateyoursourcefileandfocusonthecodingtaskathand.Forexample,inthefollowingsourcefilethebodyoftheplot.autoregressive.modelhasbeenfolded:RStudio支持自動和使用者定義的地區(qū)代碼折疊。代碼折疊允許你很方便地顯示和隱藏代碼塊,這將使你的source文件導(dǎo)航更簡易,并可以將中重點放在手頭的代碼任務(wù)上。例如,在下面的source文件中plot.autoregressive.model部分已經(jīng)被折疊。Youcanexpandthefoldedregionbyeitherclickingonthearrowinthegutterorontheiconthatoverlaysthefoldedcode.你可展開折疊部分,通過點擊箭頭槽或者覆蓋折疊代碼的圖標(biāo)。FoldableRegions折疊區(qū)域ThefollowingtypesofcoderegionsareautomaticallyfoldablewithinRStudio:RStudio中以下類型的代碼區(qū)域?qū)⒆詣诱郫B:Bracedregions(functiondefinitions,conditionalblocks,etc.)CodechunkswithinRSweaveorRMarkdowndocumentsCodesections(seebelowfordetails)支撐區(qū)域(函數(shù)定義、條件塊等)RSweave的代碼塊或者是R的Markdown文件。代碼段(具體見后文)Inthefollowingexampleyoucanseethatthetop-levelcodesection,functionbody,andconditionalblocksareallfoldable:在以下案例中,即將看到頂級代碼部分、函數(shù)體和條件塊都被折疊。YoucanalsofoldanarbitraryselectionofcodebyusingEdit->Folding->Collapse(Alt-L).你也可折疊任意選定的代碼,通過Edit->Folding->Collapse,或者使用Alt-L快捷鍵。Foldedregionsarepreservedwhileeditingadocumenthoweverwhenafileisclosedandre-openedallfoldableregionsarebydefaultshownexpanded.折疊部分在編輯文件時將被保證,但是當(dāng)文件關(guān)閉或者再次打開時,所有折疊部分將默認(rèn)展開顯示。CodeSections代碼段Codesectionsallowyoutobreakalargersourcefileintoasetofdiscreteregionsforeasynavigationbetweenthem.Codesectionsareautomaticallyfoldable—forexample,thefollowingsourcefilehasthreesections(oneexpandedandtheothertwofolded):代碼段允許你將一個大型的source文件分解成一組獨立的區(qū)域以方便再他們之間漫游。代碼段將自動折疊,例如,以下源文件有三個部分(一個展開而另兩個折疊)。ToinsertanewcodesectionyoucanusetheCode->InsertSectioncommand.Alternatively,anycommentlinewhichincludesatleastfourtrailingdashes(-),equalsigns(=),orpoundsigns(#)automaticallycreatesacodesection.Forexample,allofthefollowinglinescreatecodesections:插入一個新的代碼段你使用Code->InsertSection命令?;蛘?,任意注釋行中包含至少四個破折號(-),等號(=)或井號(#)將自動創(chuàng)建代碼段。#SectionOne#SectionTwo=================================###SectionThree#############################Notethatasillustratedabovethelinecanstartanynumberofpoundsigns(#)solongasitendswithfourormore-,=,or#characters.注意:如前所述,可輸入超過4個或更多的數(shù)量的#號,-,=等。TonavigatebetweencodesectionsyoucanusetheJumpTomenuavailableatthebottomoftheeditor:你可使用編輯器低端的JumpTo菜單在代碼段間漫游(或者使用Alt+Shift+J快捷鍵)。MenuCommandsandShortcuts菜單命令和快捷鍵Thefollowingmenucommandsandshortcutsareavailableforworkingwithfoldedregionsandcodesections:以下菜單命令和快捷鍵可以對折疊區(qū)域和代碼段進(jìn)行操作:Edit->Folding:Collapse—Alt+LExpand—Shift+Alt+LCollapseAll—Alt+AExpandAll—Shift+Alt+ACode:InsertSection—Ctrl+Shift+R(Cmd+Shift+RontheMac)JumpTo—Shift+Alt+JNotethattheCollapseAllcommandcollapsesalloftheoutermostfoldableregions(ratherthanallofthenestedregionswithinthesourcefile).注意:CollapseAll命令折疊所有外層可折疊區(qū)域(而不是源文件中的所有嵌套區(qū)域)RelatedTopicsNavigatingCode導(dǎo)航代碼EditingandExecutingCode編輯和執(zhí)行代碼

NavigatingCode導(dǎo)航代碼RStudioincludesanumberoffeaturestoenablerapidnavigationthroughRsourcecode.Learningthesefeaturescanbeamajorproductivityenhancementandcanalsoassistingainingabetterunderstandingofsourcecodewrittenbyothersonyourteamorwithinexternalpackages.RStudio包括一些在R源代碼中加速導(dǎo)航的功能。學(xué)習(xí)這些功能可以提高效率并幫助更好地理解你的團(tuán)隊中其他人所寫的源代碼,或者是外部程序包中的源代碼。FindinFiles查找文件Givenaspecificdirectory,FindinFilesallowsyoutorecursivelysearcheveryfileforeachoccurrenceofagivenstring.TodisplaytheFindinFilesdialogbox,gototheeditmenuandselectFindinFiles:給出一個特定目錄,查找文件(Edit->FindinFiles)允許你對每個出現(xiàn)的字符串遞歸查找每一個文件。顯示查找文件對話框,編輯菜單并選擇查找文件:Youcanfurthercustomizeyoursearchwithregularexpressionsandfiltersforspecificfiletypes.Yoursearchresultswilldisplayinthepaneadjacenttotheconsole.Foreachmatchingstring,thefollowingwillbedisplayed:你可以進(jìn)一步通過正式表達(dá)式和特定文件類型來自定義你的搜索。你的搜索結(jié)果將顯示在控制臺的相鄰窗口中。每一個匹配的字符串都將顯示以下信息:Thefilepath文件路徑Thelinenumberofeachoccurrence每個出現(xiàn)的行號Thematchingstring(highlighted),inthecontextofthecurrentline當(dāng)前行內(nèi)容中匹配的字符串(高亮)Doubleclickingtheline,willopenthefileintheRStudiosourceeditor.ThekeyboardshortcutforFindinFilesisCtrl+Shift+F雙擊這一行,將在RStudio的source編輯器中打開該文件。查找文件的快捷鍵是Ctrl+Shift+F。GotoFile/Function轉(zhuǎn)到文件/函數(shù)IfyouknowthenameofthesourcefileorfunctionthatyouwanttoeditnextyoucanquicklynavigatetoitusingtheGotoFile/FunctionsearchboxonthemainRStudiotoolbar:如果你知道源文件或函數(shù)的名字并想對其進(jìn)行編輯,你可以使用RStudio工具條中的GotoFile/Function搜索框迅速導(dǎo)航。TheGotoFile/Functionfeatureworksoffaconstantlyupdatedindexofyoursourcecode.Thespecificsourcefilestoindexaredeterminedasfollows:GotoFile/Function功能就不斷更新索引你的源代碼。專業(yè)源文件的索引將決定于:IfanRStudioProjectisactivethenallRsourcefileswithintheprojectdirectoryareindexed.IfaProjectisnotactivethenallcurrentlyopenRsourcefilesareindexed.1.RStudioProject是活動的,然后所有的R源文件在程序包列表中將被索引;2.如果一個Project不活動,那么所有當(dāng)前打開的R源程序?qū)⒈凰饕?;ThekeyboardshortcutforGotoFile/FunctionisCtrl+.GotoFile/Function的快捷鍵是Ctrl+.GotoFunctionDefinition轉(zhuǎn)到函數(shù)定義SinceanindexofyourRsourcecode(asdescribedabove)ismaintained,RStudiocanalsohelpyouquicklynavigatetodefinitionofanyRfunction.Tonavigateafunctiondefinitionyouplaceyourcursoronthefunctionname(itdoesn'thavetofullyselected)andthenchoosetheGotoFunctionDefinitioncommand:當(dāng)一個你的R源代碼指數(shù)(如上所述)被維護(hù),RStudio也可幫助你迅速對任何R函數(shù)進(jìn)行導(dǎo)航。你可將光標(biāo)放在函數(shù)名稱上(不需要完全選定)來導(dǎo)航函數(shù)定義,然后選擇GotoFunctionDefinition命令:Youcanalsoaccessgotofunctiondefinition:你也可這樣去訪問函數(shù)定義:UsingtheF2keyboardshortcut使用F2快捷鍵UsingCtrl+Clickwiththemouse使用Ctrl+Click鼠標(biāo)點擊Fromeitherthesourceeditorortheconsole從source編輯器或控制臺Itispossibletonavigatetobothyourownfunctions(definedinRsourcefiles)aswellasanyotherfunctiondefinedwithinanRpackage.ForfunctionsdefinedwithinpackagesthecodeisdisplayedinaspecialSourceViewerpanewhichisread-only.它同樣能夠?qū)Ш侥阕约旱暮瘮?shù)(在R源文件中定義)和其他所有在R程序包中定義的函數(shù)。程序包中定義的函數(shù),其代碼將顯示在特定的SourceViewer窗口中,它僅能只讀不能編輯,但可以復(fù)制粘貼出來使用。NavigatingwithinaSourceFile源文件導(dǎo)航Thereareseveralwaystonavigatethroughthecodewithinasourcefile:有多種方式可以對源文件中的代碼進(jìn)行導(dǎo)航:Thegotofunctiondefinitionfeaturedescribedabovealsoworksforfunctionswithinthecurrentsourcefile(givingprecedencetofunctionsdefinedwithinthecallingscope).TheJumptoLinecommand(shortcut:Command+G)canbeusedtogotoanylineinthecurrentfile.Youcanalsousethefunctionmenu(shownbelow)toquicklyjumptofunctionsbyname.上述的gotofunctiondefinition功能同樣可以對當(dāng)前源文件的函數(shù)進(jìn)行工作(在調(diào)用范圍中給出函數(shù)定義優(yōu)先級。JumptoLine命令(快捷鍵Command+G))可用于轉(zhuǎn)到當(dāng)前文件的任意一行;你也可使用函數(shù)命令(如下顯示),通過名稱迅速跳到函數(shù)。NotethatthefunctionmenucurrentlysupportsstandardRfunctionshoweverdoesnotyetsupportS4methods.注意:當(dāng)前函數(shù)菜單支持標(biāo)準(zhǔn)R函數(shù),但是不支持S4方法。GoingBackandForward向前與向后Whennavigatingthroughcode(especiallywhennavigatingthroughasequenceoffunctioncalls)youoftenwanttoquicklyreturntothepreviouseditinglocation.RStudiomaintainsalistofactivenavigationsandallowsyoutotraversethemusingtheBackandForwardcommands(availableontheEditmenuandonthefarleftofthesourceeditortoolbar).當(dāng)代碼導(dǎo)航(特別是在一系列函數(shù)中導(dǎo)航),你經(jīng)常要快速返回之前編輯的位置。RStudio維護(hù)有一個活動的導(dǎo)航列表,允許你使用Back和Forward命令(Edit菜單中或在source編輯條的最左邊)進(jìn)行來回遍歷。BackandForwardapplytothefollowingnavigationgestures:Back和Forward適用于以下導(dǎo)航方式:Openingadocument(orswitchingtabs)打開一個文件(或切換標(biāo)簽)Goingtoafunctiondefinition轉(zhuǎn)到函數(shù)定義Jumpingtoaline跳到一行Jumpingtoafunctionusingthefunctionmenu使用函數(shù)菜單跳到一個函數(shù)YoucanalsoinvokeBackandForwardusingtheCtrl+F9/Ctrl+F10(Cmd+F9/Cmd+F10ontheMac)keyboardshortcuts.你同樣可以使用快捷鍵Ctrl+F9/Ctrl+F10(Cmd+F9/Cmd+F10ontheMac)來調(diào)用Back和Forward:

UsingProjects使用項目RStudioprojectsmakeitstraightforwardtodivideyourworkintomultiplecontexts,eachwiththeirownworkingdirectory,workspace,history,andsourcedocuments.RStudio項目直接讓你的工作多語境,每一個自帶工作目錄、工作空間、歷史和源文件。CreatingProjects創(chuàng)建項目RStudioprojectsareassociatedwithRworkingdirectories.YoucancreateanRStudioproject:RStudio項目與R工作目錄相連,你可通過以下方式創(chuàng)建RStudio項目:InabrandnewdirectoryInanexistingdirectorywhereyoualreadyhaveRcodeanddataBycloningaversioncontrol(GitorSubversion)repository在全新的目錄下;在現(xiàn)有的你已存放有R代碼和數(shù)據(jù)的目錄下;通過克隆一個版本控制(Git或Subversion)元數(shù)據(jù)庫;TocreateanewprojectusetheNewProjectcommand(availableontheProjectsmenuando可使用創(chuàng)建NewProject命令(在Projects菜單中)一個新項目。

UsingCommandHistory使用命令歷史RStudiomaintainsadatabaseofallcommandswhichyouhaveeverenteredintotheConsole.YoucanbrowseandsearchthisdatabaseusingtheHistorypane.RStudio維護(hù)有所有你曾輸入控制臺的命令的數(shù)據(jù)庫,你可使用歷史窗口來瀏覽和搜索這個數(shù)據(jù)庫。BrowsingHistory瀏覽歷史CommandsyouhavepreviouslyenteredintheRStudioconsolecanbebrowsedfromtheHistorytab.Thecommandsaredisplayedinorder(mostrecentatthebottom)andgroupedbyblockoftime:你之前輸入RStudio控制臺的命令可以通過歷史標(biāo)簽進(jìn)行瀏覽,這些命令將按順序顯示出來(最近的命令在底部)并按時間分組。SearchingHistory搜索歷史ExecutingaSearch執(zhí)行一個搜索Youcanusethesearchboxatthetoprightofthehistorytabtosearchforallinstancesofapreviouscommand(e.g.plot).Thesearchcanbefurtherrefinedbyaddingadditionalwordsseparatedbyspaces(e.g.thenameofparticulardataset):你可使用歷史窗口右上角的搜索欄搜索之前命令的所有實例(例如plot)。搜索可進(jìn)一步根據(jù)添加的用空格隔開的單詞來進(jìn)行再次查找(例如一個特定的數(shù)據(jù)集)。(ShowingCommandContext顯示命令上下文Aftersearchingforacommandwithinyourhistoryyoumaywishtoviewtheothercommandsthatwereexecutedinproximitytoit.Byclickingthearrowintherightmarginofthesearchresultsyoucanviewthecommandwithinitscontext:在你的歷史中搜索完一個命令后,你可能希望看到與之接近的其他命令。點擊搜索結(jié)果的右側(cè)邊緣的箭頭,你將看到命令的上下文。UsingCommands使用命令CommandsselectedwithintheHistorypanecanbeusedintwofashions(correspondingtothetwobuttonsontheleftsideoftheHistorytoolbar):在歷史窗口中所選的命令可以通過兩種方式進(jìn)行使用(對應(yīng)歷史工具欄左側(cè)的兩個按鈕):SendtoConsole—Sendstheselectedcommand(s)totheConsole.NotethatthecommandsareinsertedintotheConsolehowevertheyarenotexecuteduntilyoupressEnter.ToConsole—將所選命令發(fā)送到控制臺。注意命令插入控制臺后直到你按Enter鍵才會執(zhí)行。InsertintoSource—Insertstheselectedcommand(s)intothecurrentlyactiveSourcedocument.Ifthereisn'tcurrentlyaSourcedocumentavailablethenanewuntitledonewillbecreated.ToSource—將所選命令發(fā)送到當(dāng)前活動的源文件。如果當(dāng)前沒有源文件則將創(chuàng)建一個新的未命名的源文件。Withinthehistorylistyoucanselectasinglecommandormultiplecommands:歷史列表中,你可選擇單個命令或者多個命令(按住Ctrl鍵不連續(xù)多選或按住Shift鍵選擇連續(xù)的命令段):

RelatedTopics相關(guān)主題WorkingintheConsole在控制臺中工作EditingandExecutingCode編輯和執(zhí)行代碼

WorkingDirectoriesandWorkspaces工作目錄和工作空間ThedefaultbehaviorofRforthehandlingof.RDatafilesandworkspacesencouragesandfacilitatesamodelofbreakingworkcontextsintodistinctworkingdirectories.ThisarticledescribesthevariousfeaturesofRStudiowhichsupportthisworkflow.R對所操作的.RData文件和工作空間的默認(rèn)行為支持和推動將工作環(huán)境劃分為不同目錄的模型。本文闡述RStudio支持這種工作流程的多種功能,IMPORTANTNOTE:Inversionv0.95ofRStudioanewProjectsfeaturewasintroducedtomakemanagingmultipleworkingdirectoriesmorestraightforward.ThefeaturesdescribedbelowstillworkhoweverProjectsarenowtherecommendedmechanismfordealingwithmultipleworkcontexts.重要提示:在RStudiov0.95中,一個新的項目功能被引入進(jìn)來以使得多個工作目錄的管理更直接。以下闡述的功能仍然工作,盡管項目作為推薦機(jī)制來處理多個工作環(huán)境。DefaultWorkingDirectory默認(rèn)工作目錄AswiththestandardRGUI,RStudioemploysthenotionofaglobaldefaultworkingdirectory.Normallythisistheuserhomedirectory(typicallyreferencedusing~inR).WhenRStudiostartsupitdoesthefollowing:與標(biāo)準(zhǔn)的RGUI一樣,RStudio采用全局默認(rèn)工作目錄的概念。通常這是使用者的主目錄(通常參考R中使用~)。當(dāng)RStudio開始他將如下操作:Executesthe.Rprofile(ifany)fromthedefaultworkingdirectory.Loadsthe.RDatafile(ifany)fromthedefaultworkingdirectoryintotheworkspace.PerformstheotheractionsdescribedinRStartup.運(yùn)行默認(rèn)工作目錄下的.Rprofile;裝載默認(rèn)工作目錄下的.RData文件到工作空間中;執(zhí)行其他活動描述在RStartup中WhenRStudioexitsandtherearechangestotheworkspace,apromptaskswhetherthesechangesshouldbesavedtothe.RDatafileinthecurrentworkingdirectory.當(dāng)RStudio退出和工作空間發(fā)生變化,一個提示將問及是否保存這些改變到當(dāng)前工作目錄的.RData文件中。ThisdefaultbehaviorcanbecustomizedinthefollowingwaysusingtheRStudioOptionsdialog:可使用RStudioOptions對話框通過以下方式來自定義默認(rèn)行為:ChangethedefaultworkingdirectoryEnable/disabletheloadingof.RDatafromthedefaultworkingdirectoryatstartupSpecifywhether.RDataisalwayssaved,neversaved,orpromptedforsaveatexit.改變默認(rèn)工作目錄;啟用/禁用開始時從默認(rèn)工作目錄中裝載.RData;指定.RData退出時是否總是保存、不保持或者提示保存。ChangingtheWorkingDirectory修改工作目錄ThecurrentworkingdirectoryisdisplayedbyRStudiowithinthetitleregionoftheConsole.Thereareanumberofwaystochangethecurrentworkingdirectory:當(dāng)前的工作目錄顯示在RStudio控制臺的標(biāo)題區(qū)域。有多種方法可以改變當(dāng)前工作目錄:UsethesetwdRfunctionUsetheTools|ChangeWorkingDir...menu.ThiswillalsochangedirectorylocationoftheFilespane.FromwithintheFilespane,usetheMore|SetAsWorkingDirectorymenu.(NavigationwithintheFilespanealonewillnotchangetheworkingdirectory.)使用R函數(shù)setwd;使用Tools|ChangeWorkingDir...菜單,他也將改變文件窗口的目錄位置;從文件窗口中使用More|SetAsWorkingDirectory菜單。(在文件窗口中導(dǎo)航將不改變工作目錄)Becarefultoconsiderthesideeffectsofchangingyourworkingdirectory:考慮改變你的工作目錄請小心,它有以下副作用:Relativefilereferencesinyourcode(fordatasets,sourcefiles,etc)willbecomeinvalidwhenyouchangeworkingdirectories.Thelocationwhere.RDataissavedatexitwillbechangedtothenewdirectory.應(yīng)用你代碼中的相關(guān)文件(如數(shù)據(jù)集、源文件等)將無效,當(dāng)你改變工作文件;.RData退出時所保持的位置將改變到新目錄中;Becausethesesideeffectscancauseconfusionanderrors,it'susuallybesttostartwithintheworkingdirectoryassociatedwithyourprojectandremainthereforthedurationofyoursession.ThesectionbelowdescribeshowtosetRStudio'sinitialworkingdirectory.因為這些副作用可能導(dǎo)致迷失和錯誤,最好是通常從與你項目相連的工作目錄開始,并保持在那里進(jìn)行你的操作。后文將闡述怎樣設(shè)置RStudio的初始工作目錄。StartinginOtherWorkingDirectories開始其他工作目錄Ifallofthefilesrelatedtoaprojectarecontainedwithinasingledirectorythenyou'lllikelywanttostartRStudiowithinthatdirectory.Thereareanumberofways(whichvarybyplatform)todothis.如果所有的文件與一個包含在單個目錄下的項目相關(guān),那么你最好從那個目錄開始RStudio。有多種方式(平臺不同將有所變化)來實現(xiàn)這個。FileAssociations文件關(guān)聯(lián)OnallplatformsRStudioregistersitselfasahandlerfor.RData,.R,andotherRrelatedfiletypes.Thismeansthatthesystemfilebrowser'scontext-menuwillshowRStudioasanOpenWithchoiceforthesefiles.RStudio的所有平臺自己注冊成一個處理程序如.RData,.R和其他R相關(guān)文件類型。這意味著系統(tǒng)文件瀏覽器的上下文菜單將對在RStudio對這些文件進(jìn)行開放選擇。YoucanalsooptionallycreateadefaultassociationbetweenRStudioandthe.RDataand/or.Rfiletypes.你也可以有選擇地創(chuàng)建RStudio和.RData或.R文件的默認(rèn)關(guān)聯(lián)。Whenlaunchedthroughafileassociation,RStudioautomaticallysetstheworkingdirectorytothedirectoryoftheopenedfile.NotethatRStudiocanalsoopenfilesviaassociationswhenitisalreadyrunning—inthiscaseRStudiosimplyopensthefileanddoesnotchangetheworkingdirectory.當(dāng)開始通過了一個文件關(guān)聯(lián),RStudio將自動設(shè)置工作目錄到打開文件的目錄。注意RStudio當(dāng)它已經(jīng)運(yùn)行也可通過連接打開文件——在這種情況下,RStudio僅打開文件,但不改變工作目錄。Shortcuts(Windows)快捷鍵OnWindows,youcancreateashortcuttoRStudioandcustomizethe"Startin"field.WhenlaunchedthroughthisshortcutRStudiowillstartupwithinthespecifiedworkingdirectory.在Windows中,你可創(chuàng)建一個RStudio快捷鍵來自定義“Startin”部分。當(dāng)啟動這個RStudio快捷鍵,將通過特定工作目錄來開始。DragandDrop(Mac)OnMac,dragginganddroppingafolderfromtheFinderontheRStudioDockiconwillcauseRStudiotostartupwiththedroppedfolderasthecurrentworkingdirectory.RunfromTerminal(MacandLinux)OnMacandLinuxsystemsyoucanrunRStudiofromaterminalandspecifywhichworkingdirectorytostartupwithin.Additionally,onLinuxsystemsifyourunRStudiofromaterminalandspecifynocommandlineargumentthenRStudiowillstartupusingthecurrentworkingdirectoryoftheterminal.Forexample,ontheMacyoucouldusethefollowingcommandstoopenRStudio(respectively)inthe'~/projects/foo'directoryorthecurrentworkingdirectory:$open-aRStudio~/projects/foo$open-aRStudio.OnLinuxyouwouldusethefollowingcommands(notethatno'.'isnecessaryinthesecondinvocation):$rstudio~/projects/foo$rstudioHandlingof.RprofileWhenstartingRStudioinanalternateworkingdirectorythe.Rprofilefilelocatedwithinthatdirectoryissourced.If(andonlyif)thereisnotan.Rprofilefileinthealternatedirectorythentheglobaldefaultprofile(e.g.~/.Rprofile)issourcedinstead.LoadingandSavingWorkspaces裝載和保存工作空間Ifyou

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論