




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
WorkingintheConsole控制臺中的工作Overview概述TheRStudioconsoleincludesavarietyoffeaturesintendedtomakeworkingwithRmoreproductiveandstraightforward.Thisarticlereviewsthesefeatures.LearningtousethesefeaturesalongwiththerelatedfeaturesavailableintheSourceandHistorypanescanhaveasubstantialpayoffinyouroverallproductivitywithR.RStudio控制臺包含許多使R工作更為有效和直觀的特征。本文綜述這些特征。學習使用這些特征以及Source和History窗口中可用的相關特征可以對你使用R的效率有實際性的幫助。CodeCompletion代碼補全RStudiosupportstheautomaticcompletionofcodeusingtheTabkey.Forexample,ifyouhaveanobjectnamedpollResultsinyourworkspaceyoucantypepollandthenTabandRStudiowillautomaticallycompletethefullnameoftheobject.RStudio支持使用Tab鍵來自動補全代碼。例如,如果你在工作空間中有名為pollResults的對象,你可輸入poll然后按Tab鍵,RStudio將自動完成該對象的全名。Thecodecompletionfeaturealsoprovidesinlinehelpforfunctionswheneverpossible.Forexample,ifyoutypedsubthenpressedTabyouwouldsee:代碼補全特征同樣提供了盡可能的嵌入式幫助。例如,如果你可輸入sub然后按Tab鍵,你將看到:Codecompletionalsoworksforfunctionarguments,soifyoutypedsubset(andthenpressedTabyou'dseethefollowing:代碼補全還可以對參數功能進行工作,如果你可輸入subset(然后按Tab鍵,你將看到:RetrievingPreviousCommands檢索以前命令AsyouworkwithRyou'lloftenwanttore-executeacommandwhichyoupreviouslyentered.AswiththestandardRconsole,theRStudioconsolesupportstheabilitytorecallpreviouscommandsusingthearrowkeys:當你使用R進行工作,你將經常可輸入重新執行你之前輸入的命令。與標準的R控制臺一樣,RStudio控制臺支持使用以下方向鍵回憶之前命令的功能:Up—Recallpreviouscommand(s)Down—ReverseofUpUp—回憶前一條命令;Down—回Up相反;IfyouwishtoreviewalistofyourrecentcommandsandthenselectacommandfromthislistyoucanuseCtrl+Uptoreviewthelist(notethatontheMacyoucanalsouseCommand-Up):如果你希望回顧你當前的命令列表并從中選擇一條命令,則可使用Ctrl+Up來回顧命令列表(注意:在Mac你還可使用Command-Up)Youcanalsousethissamekeyboardshortcuttoquicklysearchforcommandsthatmatchagivenprefix.Forexample,tosearchforpreviousinstancesoftheplotfunctionsimplytypeplotandthenCtrl+Up:你還可以使用同樣的快捷鍵來快速查找匹配給定前綴的命令。例如,查找前面出現過的plot函數,先輸入plot然后Ctrl+UpConsoleTitleBar控制臺標題欄ThisscreenshotillustratesafewadditionalcapabilitiesprovidedbytheConsoletitlebar:這個截屏闡明了控制臺標題欄的一些額外功能。Displayofthecurrentworkingdirectory.TheabilitytointerruptRduringalongcomputation.MinimizingandmaximizingtheConsoleinrelationtotheSourcepane(usingthebuttonsatthetop-rightorbydouble-clickingthetitlebar).顯示當前工作目錄;能夠在一個長期的計算里中斷R;最小化和最大化Console和Source窗口(使用右上按鈕或雙擊標題欄)KeyboardShortcuts快捷鍵Beyondthehistoryandcode-completionorientedkeyboardshortcutsdescribedabove,thereareawidevarietyofothershortcutsavailable.Someofthemoreusefulshortcutsinclude:除了上述的歷史和代碼補全快捷鍵外,還有很多其他快捷鍵可用,其中最為有用的快捷鍵包括Ctrl+1—MovefocustotheSourceEditorCtrl+2—MovefocustotheConsoleCtrl+L—CleartheConsoleEsc—InterruptRCtrl+1—移動焦點到Source編輯器。Ctrl+2—移動焦點到Console。Ctrl+L—清理控制臺;Esc—中斷RYoucanfindalistofallshortcutsintheKeyboardShortcutsarticle.你可從KeyboardShortcuts文中找到所有快捷鍵列表。RelatedTopics相關主題EditingandExecutingCode編輯和執行代碼UsingCommandHistory使用命令歷史
EditingandExecutingCode編輯執行代碼Overview概述RStudio'ssourceeditorincludesavarietyofproductivityenhancingfeaturesincludingsyntaxhighlighting,codecompletion,multiple-fileediting,andfind/replace.RStudio的souce編輯器包含各種提高效率的特征,包括語法高亮顯示,代碼自動補全,多文件編輯以及查找和替換。RStudioalsoenablesyoutoflexiblyexecuteRcodedirectlyfromthesourceeditor.FormanyRdevelopersthisrepresentstheirpreferredwayofworkingwithR.Byexecutingcommandsfromwithinthesourceeditorratherthantheconsoleitismucheasiertoreproducesequencesofcommandsaswellaspackagethemforre-useasafunction.ThesefeaturesaredescribedintheExecutingCodesectionbelow.RStudio還可以使你直接通過source編輯器靈活地執行R代碼。對于許多R開發者來說,這是他們使用R的首選方式。通過source編輯器執行命令相對于控制臺來說更便于復制命令序列和將其做為再次使用的函數進行打包;這些特征將在后面的執行代碼部分講解。ManagingFiles文件管理RStudiosupportssyntaxhighlightingandotherspecializedcode-editingfeaturesforthefollowingtypesoffiles:Rstudio支持語法高亮顯示和其他專業化的代碼編輯功能,針對以下類型文件。RscriptsR腳本文件SweavedocumentsSweave文件TeXdocumentsTex文件TocreateanewfileyouusetheFile->Newmenu:你可通過File->New菜單創建新文件。(你也可使用Ctrl+Shift+N快捷鍵)。ToopenanexistingfileyouuseeithertheFile->OpenmenuortheOpenRecentmenutoselectfromrecentlyopenedfiles.你可通過File->Open菜單或者OpenRecent菜單選擇來打開已有文件。(你也可使用Ctrl+O快捷鍵)。IfyouopenseveralfileswithinRStudiotheyareallavailableastabstofacilitatequickswitchingbetweenopendocuments.Ifyouhavealargenumberofopendocumentsyoucanalsonavigatebetweenthemusingthe>>icononthetabbarortheView->SwitchtoTab)menuitem:如果你通過RStudio打開許多文件,那么他們都可以通過標簽進行快速切換。如果你有大量的打開文件,你也可在它們間通過標簽欄中的>>圖標來進行導航,或者View->SwitchtoTab菜單項(你也可使用Ctrl+O快捷鍵)。CodeCompletion代碼補全RStudiosupportstheautomaticcompletionofcodeusingtheTabkey.Forexample,ifyouhaveanobjectnamedpollResultsinyourworkspaceyoucantypepollandthenTabandRStudiowillautomaticallycompletethefullnameoftheobject.RStudio可以使用Tab鍵來支持代碼自動補全,例如,如果你在工作空間中有名為pollResults的對象,你可輸入poll然后按Tab鍵,RStudio將自動完成該對象的全名。Codecompletionalsoworksintheconsole,andmoredetailsonusingitcanbefoundtheconsoleCodeCompletiondocumentation.代碼補全同樣在控制臺中工作,具體用法可間控制臺代碼自動完成CodeCompletion文檔。FindandReplace查找和替換RStudiosupportsfindingandreplacingtextwithinsourcedocuments:Rstudio支持在source文件中查找和替換。FindandreplacecanbeopenedusingtheCtrl+Fshortcutkey,orfromtheEdit->FindandReplacemenuitem.可使用Ctrl+F快捷鍵來打開查找和替換欄,或者使用Edit->FindandReplace菜單項。ExtractFunction提取函數RStudiocananalyzeaselectionofcodefromwithinthesourceeditorandautomaticallyconvertitintoare-usablefunction.Any"free"variableswithintheselection(objectsthatarereferencedbutnotcreatedwithintheselection)areconvertedintofunctionarguments:RStudio可以在source編輯器中分析一組選擇的代碼,并自動將其轉化成再次使用的函數。任何選擇中的"free"變量(選擇引用對象但不創建)將轉化為函數參數。(你也可使用Ctrl+Shift+U快捷鍵)。Comment/Uncomment注釋/取消注釋YoucancommentanduncommententireselectionsofcodeusingtheEdit->Comment/UncommentLinesmenuitem(youcanalsodothisusingtheCtrl+/keyboardshortcut):你可使用使用Edit->Comment/UncommentLines菜單項來對所選的整個代碼進行注釋或取消注釋(你也可使用Ctrl+Shift+C快捷鍵)。Indentation首行縮進AsyouwriteRcodeinRStudioitisautomaticallyindentedaccordingtothecurrentindentationoptions(seeCustomizingRStudio).Rcodeisalsore-indented:如果你在RStudio中寫R代碼,他將自動根據當前的縮進選項(見CustomizingRStudio)進行縮進。R代碼也可再縮進。Whenevernewcodeispastedintoasourcedocument.WhentheReindentLinescommand(picturedabove)isinvoked.當新代碼黏貼到一個source文件中;當縮進行命令(上圖)被調用NotethatRStudioautomaticindentationisRsyntax-awareandisthereforeonlyusedonsourcefilescontainingRcode.注意:RStudio自動縮進針對的是R語法意識,因此,只有在包含R代碼的source文件中使用ExecutingCode執行代碼RStudiosupportsthedirectexecutionofcodefromwithinthesourceeditor(theexecutedcommandsareinsertedintotheconsolewheretheiroutputalsoappears).RStudio支持從source編輯器中直接執行代碼(執行代碼將插入控制臺,并在此輸出結果),ExecutingaSingleLine執行一行代碼ToexecutethelineofsourcecodewherethecursorcurrentlyresidesyoupresstheCtrl+Enterkey(orusetheRuntoolbarbutton):你可通過Ctrl+Enter鍵來執行當前光標所在行的source代碼(或者使用Run工具條按鈕)Afterexecutingthelineofcode,RStudioautomaticallyadvancesthecursortothenextline.Thisenablesyoutosingle-stepthroughasequenceoflines.執行完一行代碼,RStudio自動將光標跳到下一行,這將使你可以在一系列行中單步執行代碼。ExecutingMultipleLines執行多行代碼Therearethreewaystoexecutemultiplelinesfromwithintheeditor:有三種方法從編輯器中執行多行代碼。SelectthelinesandpresstheCtrl+Enterkey(orusetheRuntoolbarbutton);orAfterexecutingaselectionofcode,usetheRe-RunPreviousRegioncommand(oritsassociatedtoolbarbutton)torunthesameselectionagain.Notethatchangestotheselectionincludingadditional,removal,andmodificationoflineswillbereflectedinthissubsequentrunoftheselection.ToruntheentiredocumentpresstheCtrl+Shift+Enterkey(orusetheSourcetoolbarbutton).選擇這些行,按Ctrl+Enter鍵(或者使用Run工具條按鈕)執行選擇的代碼后,使用Re-RunPreviousRegion命令(或相應的工具欄按鈕)再次運行相同的選擇。注意:現在發生變化,包括增增加、刪除和修改行將反映到后續運行中。(或者使用Ctrl+Shift+P快捷鍵)運行整個文件按Ctrl+Shift+Enter鍵(或使用source工具條按鈕)??ThedifferencebetweenrunninglinesfromaselectionandinvokingSourceisthatwhenrunningaselectionalllinesareinserteddirectlyintotheconsolewhereasforSourcethefileissavedtoatemporarylocationandthensourcedintotheconsolefromthere(therebycreatinglessclutterintheconsole).從選擇的行進行運行和調用Source的區別在于,當運行所選的所有行將直接插入到控制臺中,而對于Source文件則保存到臨時位置然后從那里反映到控制臺(因此在控制臺中產生較少的雜亂)。SourceonSave資源保存Wheneditingre-usablefunctions(asopposedtofreestandinglinesofR)youmaywishtosettheSourceonSaveoptionforthedocument(availableonthetoolbarnexttotheSaveicon).Enablingthisoptionwillcausethefiletoautomaticallybesourcedintotheglobalenvironmenteverytimeitissaved.當編輯一個再次調用函數(相對于獨立的R行而言),你可能希望對文件設置SourceonSave選項(通過工具條中Save圖標的后一項實現)。啟用該選項導致文件每次保存都將被全球自動采用。SettingSourceonSaveensuresthatthecopyofafunctionwithintheglobalenvironmentisalwaysinsyncwithitssource,andalsoprovidesagoodwaytoarrangeforfrequentsyntaxcheckingasyoudevelopafunction.設置SourceonSave選項確保該函數的版本在全球環境總是能夠資源同步,并且也能在你開發一個函數是提供一種好的方式安排頻繁的語法檢查。KeyboardShortcuts快捷鍵Beyondthekeyboardshortcutsdescribedabove,thereareawidevarietyofothershortcutsavailable.Someofthemoreusefulonesinclude:除了上述的快捷鍵外,還有很多其他快捷鍵可用,其中最為有用的快捷鍵包括。Ctrl+Shift+N—Newdocument新文檔Ctrl+O—Opendocument打開文檔Ctrl+S—Saveactivedocument保存活動文檔Ctrl+1—MovefocustotheSourceEditor移動焦點到Source編輯器Ctrl+2—MovefocustotheConsole移動焦點到控制臺YoucanfindalistofallshortcutsintheKeyboardShortcutsarticle.
RelatedTopics相關主題WorkingintheConsole在控制臺中工作UsingCommandHistory使用歷史命令NavigatingCode代碼導航CodeFoldingandSections代碼折疊和截取
CodeFoldingandSections代碼折疊和截取CodeFolding代碼折疊RStudiosupportsbothautomaticanduser-definedfoldingforregionsofcode.Codefoldingallowsyoutoeasilyshowandhideblocksofcodetomakeiteasiertonavigateyoursourcefileandfocusonthecodingtaskathand.Forexample,inthefollowingsourcefilethebodyoftheplot.autoregressive.modelhasbeenfolded:RStudio支持自動和使用者定義的地區代碼折疊。代碼折疊允許你很方便地顯示和隱藏代碼塊,這將使你的source文件導航更簡易,并可以將中重點放在手頭的代碼任務上。例如,在下面的source文件中plot.autoregressive.model部分已經被折疊。Youcanexpandthefoldedregionbyeitherclickingonthearrowinthegutterorontheiconthatoverlaysthefoldedcode.你可展開折疊部分,通過點擊箭頭槽或者覆蓋折疊代碼的圖標。FoldableRegions折疊區域ThefollowingtypesofcoderegionsareautomaticallyfoldablewithinRStudio:RStudio中以下類型的代碼區域將自動折疊:Bracedregions(functiondefinitions,conditionalblocks,etc.)CodechunkswithinRSweaveorRMarkdowndocumentsCodesections(seebelowfordetails)支撐區域(函數定義、條件塊等)RSweave的代碼塊或者是R的Markdown文件。代碼段(具體見后文)Inthefollowingexampleyoucanseethatthetop-levelcodesection,functionbody,andconditionalblocksareallfoldable:在以下案例中,即將看到頂級代碼部分、函數體和條件塊都被折疊。YoucanalsofoldanarbitraryselectionofcodebyusingEdit->Folding->Collapse(Alt-L).你也可折疊任意選定的代碼,通過Edit->Folding->Collapse,或者使用Alt-L快捷鍵。Foldedregionsarepreservedwhileeditingadocumenthoweverwhenafileisclosedandre-openedallfoldableregionsarebydefaultshownexpanded.折疊部分在編輯文件時將被保證,但是當文件關閉或者再次打開時,所有折疊部分將默認展開顯示。CodeSections代碼段Codesectionsallowyoutobreakalargersourcefileintoasetofdiscreteregionsforeasynavigationbetweenthem.Codesectionsareautomaticallyfoldable—forexample,thefollowingsourcefilehasthreesections(oneexpandedandtheothertwofolded):代碼段允許你將一個大型的source文件分解成一組獨立的區域以方便再他們之間漫游。代碼段將自動折疊,例如,以下源文件有三個部分(一個展開而另兩個折疊)。ToinsertanewcodesectionyoucanusetheCode->InsertSectioncommand.Alternatively,anycommentlinewhichincludesatleastfourtrailingdashes(-),equalsigns(=),orpoundsigns(#)automaticallycreatesacodesection.Forexample,allofthefollowinglinescreatecodesections:插入一個新的代碼段你使用Code->InsertSection命令。或者,任意注釋行中包含至少四個破折號(-),等號(=)或井號(#)將自動創建代碼段。#SectionOne#SectionTwo=================================###SectionThree#############################Notethatasillustratedabovethelinecanstartanynumberofpoundsigns(#)solongasitendswithfourormore-,=,or#characters.注意:如前所述,可輸入超過4個或更多的數量的#號,-,=等。TonavigatebetweencodesectionsyoucanusetheJumpTomenuavailableatthebottomoftheeditor:你可使用編輯器低端的JumpTo菜單在代碼段間漫游(或者使用Alt+Shift+J快捷鍵)。MenuCommandsandShortcuts菜單命令和快捷鍵Thefollowingmenucommandsandshortcutsareavailableforworkingwithfoldedregionsandcodesections:以下菜單命令和快捷鍵可以對折疊區域和代碼段進行操作: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命令折疊所有外層可折疊區域(而不是源文件中的所有嵌套區域)RelatedTopicsNavigatingCode導航代碼EditingandExecutingCode編輯和執行代碼
NavigatingCode導航代碼RStudioincludesanumberoffeaturestoenablerapidnavigationthroughRsourcecode.Learningthesefeaturescanbeamajorproductivityenhancementandcanalsoassistingainingabetterunderstandingofsourcecodewrittenbyothersonyourteamorwithinexternalpackages.RStudio包括一些在R源代碼中加速導航的功能。學習這些功能可以提高效率并幫助更好地理解你的團隊中其他人所寫的源代碼,或者是外部程序包中的源代碼。FindinFiles查找文件Givenaspecificdirectory,FindinFilesallowsyoutorecursivelysearcheveryfileforeachoccurrenceofagivenstring.TodisplaytheFindinFilesdialogbox,gototheeditmenuandselectFindinFiles:給出一個特定目錄,查找文件(Edit->FindinFiles)允許你對每個出現的字符串遞歸查找每一個文件。顯示查找文件對話框,編輯菜單并選擇查找文件:Youcanfurthercustomizeyoursearchwithregularexpressionsandfiltersforspecificfiletypes.Yoursearchresultswilldisplayinthepaneadjacenttotheconsole.Foreachmatchingstring,thefollowingwillbedisplayed:你可以進一步通過正式表達式和特定文件類型來自定義你的搜索。你的搜索結果將顯示在控制臺的相鄰窗口中。每一個匹配的字符串都將顯示以下信息:Thefilepath文件路徑Thelinenumberofeachoccurrence每個出現的行號Thematchingstring(highlighted),inthecontextofthecurrentline當前行內容中匹配的字符串(高亮)Doubleclickingtheline,willopenthefileintheRStudiosourceeditor.ThekeyboardshortcutforFindinFilesisCtrl+Shift+F雙擊這一行,將在RStudio的source編輯器中打開該文件。查找文件的快捷鍵是Ctrl+Shift+F。GotoFile/Function轉到文件/函數IfyouknowthenameofthesourcefileorfunctionthatyouwanttoeditnextyoucanquicklynavigatetoitusingtheGotoFile/FunctionsearchboxonthemainRStudiotoolbar:如果你知道源文件或函數的名字并想對其進行編輯,你可以使用RStudio工具條中的GotoFile/Function搜索框迅速導航。TheGotoFile/Functionfeatureworksoffaconstantlyupdatedindexofyoursourcecode.Thespecificsourcefilestoindexaredeterminedasfollows:GotoFile/Function功能就不斷更新索引你的源代碼。專業源文件的索引將決定于:IfanRStudioProjectisactivethenallRsourcefileswithintheprojectdirectoryareindexed.IfaProjectisnotactivethenallcurrentlyopenRsourcefilesareindexed.1.RStudioProject是活動的,然后所有的R源文件在程序包列表中將被索引;2.如果一個Project不活動,那么所有當前打開的R源程序將被索引;ThekeyboardshortcutforGotoFile/FunctionisCtrl+.GotoFile/Function的快捷鍵是Ctrl+.GotoFunctionDefinition轉到函數定義SinceanindexofyourRsourcecode(asdescribedabove)ismaintained,RStudiocanalsohelpyouquicklynavigatetodefinitionofanyRfunction.Tonavigateafunctiondefinitionyouplaceyourcursoronthefunctionname(itdoesn'thavetofullyselected)andthenchoosetheGotoFunctionDefinitioncommand:當一個你的R源代碼指數(如上所述)被維護,RStudio也可幫助你迅速對任何R函數進行導航。你可將光標放在函數名稱上(不需要完全選定)來導航函數定義,然后選擇GotoFunctionDefinition命令:Youcanalsoaccessgotofunctiondefinition:你也可這樣去訪問函數定義:UsingtheF2keyboardshortcut使用F2快捷鍵UsingCtrl+Clickwiththemouse使用Ctrl+Click鼠標點擊Fromeitherthesourceeditorortheconsole從source編輯器或控制臺Itispossibletonavigatetobothyourownfunctions(definedinRsourcefiles)aswellasanyotherfunctiondefinedwithinanRpackage.ForfunctionsdefinedwithinpackagesthecodeisdisplayedinaspecialSourceViewerpanewhichisread-only.它同樣能夠導航你自己的函數(在R源文件中定義)和其他所有在R程序包中定義的函數。程序包中定義的函數,其代碼將顯示在特定的SourceViewer窗口中,它僅能只讀不能編輯,但可以復制粘貼出來使用。NavigatingwithinaSourceFile源文件導航Thereareseveralwaystonavigatethroughthecodewithinasourcefile:有多種方式可以對源文件中的代碼進行導航:Thegotofunctiondefinitionfeaturedescribedabovealsoworksforfunctionswithinthecurrentsourcefile(givingprecedencetofunctionsdefinedwithinthecallingscope).TheJumptoLinecommand(shortcut:Command+G)canbeusedtogotoanylineinthecurrentfile.Youcanalsousethefunctionmenu(shownbelow)toquicklyjumptofunctionsbyname.上述的gotofunctiondefinition功能同樣可以對當前源文件的函數進行工作(在調用范圍中給出函數定義優先級。JumptoLine命令(快捷鍵Command+G))可用于轉到當前文件的任意一行;你也可使用函數命令(如下顯示),通過名稱迅速跳到函數。NotethatthefunctionmenucurrentlysupportsstandardRfunctionshoweverdoesnotyetsupportS4methods.注意:當前函數菜單支持標準R函數,但是不支持S4方法。GoingBackandForward向前與向后Whennavigatingthroughcode(especiallywhennavigatingthroughasequenceoffunctioncalls)youoftenwanttoquicklyreturntothepreviouseditinglocation.RStudiomaintainsalistofactivenavigationsandallowsyoutotraversethemusingtheBackandForwardcommands(availableontheEditmenuandonthefarleftofthesourceeditortoolbar).當代碼導航(特別是在一系列函數中導航),你經常要快速返回之前編輯的位置。RStudio維護有一個活動的導航列表,允許你使用Back和Forward命令(Edit菜單中或在source編輯條的最左邊)進行來回遍歷。BackandForwardapplytothefollowingnavigationgestures:Back和Forward適用于以下導航方式:Openingadocument(orswitchingtabs)打開一個文件(或切換標簽)Goingtoafunctiondefinition轉到函數定義Jumpingtoaline跳到一行Jumpingtoafunctionusingthefunctionmenu使用函數菜單跳到一個函數YoucanalsoinvokeBackandForwardusingtheCtrl+F9/Ctrl+F10(Cmd+F9/Cmd+F10ontheMac)keyboardshortcuts.你同樣可以使用快捷鍵Ctrl+F9/Ctrl+F10(Cmd+F9/Cmd+F10ontheMac)來調用Back和Forward:
UsingProjects使用項目RStudioprojectsmakeitstraightforwardtodivideyourworkintomultiplecontexts,eachwiththeirownworkingdirectory,workspace,history,andsourcedocuments.RStudio項目直接讓你的工作多語境,每一個自帶工作目錄、工作空間、歷史和源文件。CreatingProjects創建項目RStudioprojectsareassociatedwithRworkingdirectories.YoucancreateanRStudioproject:RStudio項目與R工作目錄相連,你可通過以下方式創建RStudio項目:InabrandnewdirectoryInanexistingdirectorywhereyoualreadyhaveRcodeanddataBycloningaversioncontrol(GitorSubversion)repository在全新的目錄下;在現有的你已存放有R代碼和數據的目錄下;通過克隆一個版本控制(Git或Subversion)元數據庫;TocreateanewprojectusetheNewProjectcommand(availableontheProjectsmenuando可使用創建NewProject命令(在Projects菜單中)一個新項目。
UsingCommandHistory使用命令歷史RStudiomaintainsadatabaseofallcommandswhichyouhaveeverenteredintotheConsole.YoucanbrowseandsearchthisdatabaseusingtheHistorypane.RStudio維護有所有你曾輸入控制臺的命令的數據庫,你可使用歷史窗口來瀏覽和搜索這個數據庫。BrowsingHistory瀏覽歷史CommandsyouhavepreviouslyenteredintheRStudioconsolecanbebrowsedfromtheHistorytab.Thecommandsaredisplayedinorder(mostrecentatthebottom)andgroupedbyblockoftime:你之前輸入RStudio控制臺的命令可以通過歷史標簽進行瀏覽,這些命令將按順序顯示出來(最近的命令在底部)并按時間分組。SearchingHistory搜索歷史ExecutingaSearch執行一個搜索Youcanusethesearchboxatthetoprightofthehistorytabtosearchforallinstancesofapreviouscommand(e.g.plot).Thesearchcanbefurtherrefinedbyaddingadditionalwordsseparatedbyspaces(e.g.thenameofparticulardataset):你可使用歷史窗口右上角的搜索欄搜索之前命令的所有實例(例如plot)。搜索可進一步根據添加的用空格隔開的單詞來進行再次查找(例如一個特定的數據集)。(ShowingCommandContext顯示命令上下文Aftersearchingforacommandwithinyourhistoryyoumaywishtoviewtheothercommandsthatwereexecutedinproximitytoit.Byclickingthearrowintherightmarginofthesearchresultsyoucanviewthecommandwithinitscontext:在你的歷史中搜索完一個命令后,你可能希望看到與之接近的其他命令。點擊搜索結果的右側邊緣的箭頭,你將看到命令的上下文。UsingCommands使用命令CommandsselectedwithintheHistorypanecanbeusedintwofashions(correspondingtothetwobuttonsontheleftsideoftheHistorytoolbar):在歷史窗口中所選的命令可以通過兩種方式進行使用(對應歷史工具欄左側的兩個按鈕):SendtoConsole—Sendstheselectedcommand(s)totheConsole.NotethatthecommandsareinsertedintotheConsolehowevertheyarenotexecuteduntilyoupressEnter.ToConsole—將所選命令發送到控制臺。注意命令插入控制臺后直到你按Enter鍵才會執行。InsertintoSource—Insertstheselectedcommand(s)intothecurrentlyactiveSourcedocument.Ifthereisn'tcurrentlyaSourcedocumentavailablethenanewuntitledonewillbecreated.ToSource—將所選命令發送到當前活動的源文件。如果當前沒有源文件則將創建一個新的未命名的源文件。Withinthehistorylistyoucanselectasinglecommandormultiplecommands:歷史列表中,你可選擇單個命令或者多個命令(按住Ctrl鍵不連續多選或按住Shift鍵選擇連續的命令段):
RelatedTopics相關主題WorkingintheConsole在控制臺中工作EditingandExecutingCode編輯和執行代碼
WorkingDirectoriesandWorkspaces工作目錄和工作空間ThedefaultbehaviorofRforthehandlingof.RDatafilesandworkspacesencouragesandfacilitatesamodelofbreakingworkcontextsintodistinctworkingdirectories.ThisarticledescribesthevariousfeaturesofRStudiowhichsupportthisworkflow.R對所操作的.RData文件和工作空間的默認行為支持和推動將工作環境劃分為不同目錄的模型。本文闡述RStudio支持這種工作流程的多種功能,IMPORTANTNOTE:Inversionv0.95ofRStudioanewProjectsfeaturewasintroducedtomakemanagingmultipleworkingdirectoriesmorestraightforward.ThefeaturesdescribedbelowstillworkhoweverProjectsarenowtherecommendedmechanismfordealingwithmultipleworkcontexts.重要提示:在RStudiov0.95中,一個新的項目功能被引入進來以使得多個工作目錄的管理更直接。以下闡述的功能仍然工作,盡管項目作為推薦機制來處理多個工作環境。DefaultWorkingDirectory默認工作目錄AswiththestandardRGUI,RStudioemploysthenotionofaglobaldefaultworkingdirectory.Normallythisistheuserhomedirectory(typicallyreferencedusing~inR).WhenRStudiostartsupitdoesthefollowing:與標準的RGUI一樣,RStudio采用全局默認工作目錄的概念。通常這是使用者的主目錄(通常參考R中使用~)。當RStudio開始他將如下操作:Executesthe.Rprofile(ifany)fromthedefaultworkingdirectory.Loadsthe.RDatafile(ifany)fromthedefaultworkingdirectoryintotheworkspace.PerformstheotheractionsdescribedinRStartup.運行默認工作目錄下的.Rprofile;裝載默認工作目錄下的.RData文件到工作空間中;執行其他活動描述在RStartup中WhenRStudioexitsandtherearechangestotheworkspace,apromptaskswhetherthesechangesshouldbesavedtothe.RDatafileinthecurrentworkingdirectory.當RStudio退出和工作空間發生變化,一個提示將問及是否保存這些改變到當前工作目錄的.RData文件中。ThisdefaultbehaviorcanbecustomizedinthefollowingwaysusingtheRStudioOptionsdialog:可使用RStudioOptions對話框通過以下方式來自定義默認行為:ChangethedefaultworkingdirectoryEnable/disabletheloadingof.RDatafromthedefaultworkingdirectoryatstartupSpecifywhether.RDataisalwayssaved,neversaved,orpromptedforsaveatexit.改變默認工作目錄;啟用/禁用開始時從默認工作目錄中裝載.RData;指定.RData退出時是否總是保存、不保持或者提示保存。ChangingtheWorkingDirectory修改工作目錄ThecurrentworkingdirectoryisdisplayedbyRStudiowithinthetitleregionoftheConsole.Thereareanumberofwaystochangethecurrentworkingdirectory:當前的工作目錄顯示在RStudio控制臺的標題區域。有多種方法可以改變當前工作目錄:UsethesetwdRfunctionUsetheTools|ChangeWorkingDir...menu.ThiswillalsochangedirectorylocationoftheFilespane.FromwithintheFilespane,usetheMore|SetAsWorkingDirectorymenu.(NavigationwithintheFilespanealonewillnotchangetheworkingdirectory.)使用R函數setwd;使用Tools|ChangeWorkingDir...菜單,他也將改變文件窗口的目錄位置;從文件窗口中使用More|SetAsWorkingDirectory菜單。(在文件窗口中導航將不改變工作目錄)Becarefultoconsiderthesideeffectsofchangingyourworkingdirectory:考慮改變你的工作目錄請小心,它有以下副作用:Relativefilereferencesinyourcode(fordatasets,sourcefiles,etc)willbecomeinvalidwhenyouchangeworkingdirectories.Thelocationwhere.RDataissavedatexitwillbechangedtothenewdirectory.應用你代碼中的相關文件(如數據集、源文件等)將無效,當你改變工作文件;.RData退出時所保持的位置將改變到新目錄中;Becausethesesideeffectscancauseconfusionanderrors,it'susuallybesttostartwithintheworkingdirectoryassociatedwithyourprojectandremainthereforthedurationofyoursession.ThesectionbelowdescribeshowtosetRStudio'sinitialworkingdirectory.因為這些副作用可能導致迷失和錯誤,最好是通常從與你項目相連的工作目錄開始,并保持在那里進行你的操作。后文將闡述怎樣設置RStudio的初始工作目錄。StartinginOtherWorkingDirectories開始其他工作目錄Ifallofthefilesrelatedtoaprojectarecontainedwithinasingledirectorythenyou'lllikelywanttostartRStudiowithinthatdirectory.Thereareanumberofways(whichvarybyplatform)todothis.如果所有的文件與一個包含在單個目錄下的項目相關,那么你最好從那個目錄開始RStudio。有多種方式(平臺不同將有所變化)來實現這個。FileAssociations文件關聯OnallplatformsRStudioregistersitselfasahandlerfor.RData,.R,andotherRrelatedfiletypes.Thismeansthatthesystemfilebrowser'scontext-menuwillshowRStudioasanOpenWithchoiceforthesefiles.RStudio的所有平臺自己注冊成一個處理程序如.RData,.R和其他R相關文件類型。這意味著系統文件瀏覽器的上下文菜單將對在RStudio對這些文件進行開放選擇。YoucanalsooptionallycreateadefaultassociationbetweenRStudioandthe.RDataand/or.Rfiletypes.你也可以有選擇地創建RStudio和.RData或.R文件的默認關聯。Whenlaunchedthroughafileassociation,RStudioautomaticallysetstheworkingdirectorytothedirectoryoftheopenedfile.NotethatRStudiocanalsoopenfilesviaassociationswhenitisalreadyrunning—inthiscaseRStudiosimplyopensthefileanddoesnotchangetheworkingdirectory.當開始通過了一個文件關聯,RStudio將自動設置工作目錄到打開文件的目錄。注意RStudio當它已經運行也可通過連接打開文件——在這種情況下,RStudio僅打開文件,但不改變工作目錄。Shortcuts(Windows)快捷鍵OnWindows,youcancreateashortcuttoRStudioandcustomizethe"Startin"field.WhenlaunchedthroughthisshortcutRStudiowillstartupwithinthespecifiedworkingdirectory.在Windows中,你可創建一個RStudio快捷鍵來自定義“Startin”部分。當啟動這個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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 生態環境監測技術規范與標準考核試卷
- 電腦刺繡技術考核試卷
- 空調器運行數據挖掘與分析考核試卷
- 糕點烘焙的環保生產理念考核試卷
- 電機在電力質量改善的應用考核試卷
- 生物質能源在農村能源中的應用考核試卷
- 江蘇省宿遷市2025年初三5月第二次聯考化學試題含解析
- 上海師范大學天華學院《交替傳譯1》2023-2024學年第一學期期末試卷
- 遂寧能源職業學院《外國語言文學導論(1)》2023-2024學年第一學期期末試卷
- 揚州市職業大學《現代計算方法與工具》2023-2024學年第二學期期末試卷
- 生物技術概論(全套課件958P)
- 中藥學電子版教材
- 地鐵礦山法施工技術方法圖文講解附案例
- 第五版-FMEA-新版FMEA【第五版】
- 人大黃達《金融學》-超級完整版
- 守株待兔兒童故事繪本PPT
- 人工挖孔樁施工驗收規范
- 城市道路綠化工程施工設計方案
- YY/T 0342-2002外科植入物 接骨板彎曲強度和剛度的測定
- GB/T 38315-2019社會單位滅火和應急疏散預案編制及實施導則
- GB/T 30726-2014固體生物質燃料灰熔融性測定方法
評論
0/150
提交評論