1、Working in the Console 控制台中的工作Overview 概述The RStudio console includes a variety of features intended to make working with R more productive and straightforward. This article reviews these features. Learning to use these features along with the related features available in the Source and History pan
2、es can have a substantial payoff in your overall productivity with R. RStudio 控制台包含许多使 R 工作更为有效和直观的特征。本文综述这些特征。学习使用这些特征以及 Source 和 History 窗口中可用的相关特征可以对你使用 R 的效率有实际性的帮助。Code Completion 代码补全RStudio supports the automatic completion of code using the Tab key. For example, if you have an object named p
3、ollResults in your workspace you can type poll and then Tab and RStudio will automatically complete the full name of the object.RStudio 支持使用 Tab 键来自动补全代码。例如,如果你在工作空间中有名为pollResults 的对象,你可输入 poll 然后按 Tab 键,RStudio 将自动完成该对象的全名。The code completion feature also provides inline help for functions wheneve
4、r possible. For example, if you typed sub then pressed Tab you would see:代码补全特征同样提供了尽可能的嵌入式帮助。例如,如果你可输入 sub 然后按 Tab键,你将看到:Code completion also works for function arguments, so if you typed subset( and then pressed Tab youd see the following:代码补全还可以对参数功能进行工作,如果你可输入 subset(然后按 Tab 键,你将看到:Retrieving Pr
5、evious Commands 检索以前命令As you work with R youll often want to re-execute a command which you previously entered. As with the standard R console, the RStudio console supports the ability to recall previous commands using the arrow keys:当你使用 R 进行工作,你将经常可输入重新执行你之前输入的命令。与标准的 R 控制台一样,RStudio 控制台支持使用以下方向键回
6、忆之前命令的功能: Up Recall previous command(s) Down Reverse of Up Up 回忆前一条命令; Down 回 Up 相反;If you wish to review a list of your recent commands and then select a command from this list you can use Ctrl+Up to review the list (note that on the Mac you can also use Command-Up):如果你希望回顾你当前的命令列表并从中选择一条命令,则可使用 Ct
7、rl+Up 来回顾命令列表(注意:在 Mac 你还可使用 Command-Up)You can also use this same keyboard shortcut to quickly search for commands that match a given prefix. For example, to search for previous instances of the plot function simply type plot and then Ctrl+Up:你还可以使用同样的快捷键来快速查找匹配给定前缀的命令。例如,查找前面出现过的 plot 函数,先输入 plot
8、然后 Ctrl+UpConsole Title Bar 控制台标题栏This screenshot illustrates a few additional capabilities provided by the Console title bar:这个截屏阐明了控制台标题栏的一些额外功能。 Display of the current working directory. The ability to interrupt R during a long computation. Minimizing and maximizing the Console in relation to the
9、 Source pane (using the buttons at the top-right or by double-clicking the title bar). 显示当前工作目录; 能够在一个长期的计算里中断 R; 最小化和最大化 Console 和 Source 窗口(使用右上按钮或双击标题栏)Keyboard Shortcuts 快捷键Beyond the history and code-completion oriented keyboard shortcuts described above, there are a wide variety of other short
10、cuts available. Some of the more useful shortcuts include:除了上述的历史和代码补全快捷键外,还有很多其他快捷键可用,其中最为有用的快捷键包括 Ctrl+1 Move focus to the Source Editor Ctrl+2 Move focus to the Console Ctrl+L Clear the Console Esc Interrupt R Ctrl+1 移动焦点到 Source 编辑器。 Ctrl+2 移动焦点到 Console。 Ctrl+L 清理控制台; Esc 中断 RYou can find a lis
11、t of all shortcuts in the Keyboard Shortcuts article. 你可从 Keyboard Shortcuts 文中找到所有快捷键列表。Related Topics 相关主题 Editing and Executing Code 编辑和执行代码 Using Command History 使用命令历史Editing and Executing Code 编辑执行代码Overview 概述RStudios source editor includes a variety of productivity enhancing features includi
12、ng syntax highlighting, code completion, multiple-file editing, and find/replace.RStudio 的 souce 编辑器包含各种提高效率的特征,包括语法高亮显示 ,代码自动补全,多文件编辑以及查找和替换。RStudio also enables you to flexibly execute R code directly from the source editor. For many R developers this represents their preferred way of working with
13、 R. By executing commands from within the source editor rather than the console it is much easier to reproduce sequences of commands as well as package them for re-use as a function. These features are described in the Executing Code section below.RStudio 还可以使你直接通过 source 编辑器灵活地执行 R 代码。对于许多 R 开发者来说,
14、这是他们使用 R 的首选方式。通过 source 编辑器执行命令相对于控制台来说更便于复制命令序列和将其做为再次使用的函数进行打包;这些特征将在后面的执行代码部分讲解。Managing Files 文件管理RStudio supports syntax highlighting and other specialized code-editing features for the following types of files:Rstudio 支持语法高亮显示和其他专业化的代码编辑功能,针对以下类型文件。 R scripts R 脚本文件 Sweave documents Sweave 文件
15、 TeX documents Tex 文件To create a new file you use the File - New menu: 你可通过 File - New 菜单创建新文件。 (你也可使用Ctrl+Shift+N 快捷键)。To open an existing file you use either the File - Open menu or the Open Recent menu to select from recently opened files. 你可通过 File - Open 菜单或者 Open Recent 菜单选择来打开已有文件。(你也可使用 Ctrl
16、+O快捷键)。If you open several files within RStudio they are all available as tabs to facilitate quick switching between open documents. If you have a large number of open documents you can also navigate between them using the icon on the tab bar or the View - Switch to Tab) menu item:如果你通过 RStudio 打开许多
17、文件,那么他们都可以通过标签进行快速切换。如果你有大量的打开文件,你也可在它们间通过标签栏中的图标来进行导航,或者 View - Switch to Tab 菜单项(你也可使用 Ctrl+O 快捷键)。Code Completion 代码补全RStudio supports the automatic completion of code using the Tab key. For example, if you have an object named pollResults in your workspace you can type poll and then Tab and RStu
18、dio will automatically complete the full name of the object.RStudio 可以使用 Tab 键来支持代码自动补全,例如,如果你在工作空间中有名为pollResults 的对象,你可输入 poll 然后按 Tab 键,RStudio 将自动完成该对象的全名。Code completion also works in the console, and more details on using it can be found the console Code Completion documentation. 代码补全同样在控制台中工作
19、,具体用法可间控制台代码自动完成 Code Completion 文档。Find and Replace 查找和替换RStudio supports finding and replacing text within source documents: Rstudio 支持在 source 文件中查找和替换。Find and replace can be opened using the Ctrl+F shortcut key, or from the Edit - Find and Replace menu item.可使用 Ctrl+F 快捷键来打开查找和替换栏,或者使用 Edit - F
20、ind and Replace 菜单项。Extract Function 提取函数RStudio can analyze a selection of code from within the source editor and automatically convert it into a re-usable function. Any “free“ variables within the selection (objects that are referenced but not created within the selection) are converted into funct
21、ion arguments:RStudio 可以在 source 编辑器中分析一组选择的代码,并自动将其转化成再次使用的函数。任何选择中的“free“变量(选择引用对象但不创建)将转化为函数参数。(你也可使用 Ctrl+Shift+U 快捷键)。Comment/Uncomment 注释/取消注释You can comment and uncomment entire selections of code using the Edit - Comment/Uncomment Lines menu item (you can also do this using the Ctrl+/ keyboa
22、rd shortcut):你可使用使用 Edit - Comment/Uncomment Lines 菜单项来对所选的整个代码进行注释或取消注释(你也可使用 Ctrl+Shift+C 快捷键)。Indentation 首行缩进As you write R code in RStudio it is automatically indented according to the current indentation options (see Customizing RStudio). R code is also re-indented: 如果你在 RStudio 中写 R 代码,他将自动根据
23、当前的缩进选项(见 Customizing RStudio)进行缩进。R 代码也可再缩进。1. Whenever new code is pasted into a source document. 2. When the Reindent Lines command (pictured above) is invoked. 当新代码黏贴到一个 source 文件中;当缩进行命令(上图)被调用Note that RStudio automatic indentation is R syntax-aware and is therefore only used on source files c
24、ontaining R code.注意:RStudio 自动缩进针对的是 R 语法意识,因此,只有在包含 R 代码的 source 文件中使用Executing Code 执行代码RStudio supports the direct execution of code from within the source editor (the executed commands are inserted into the console where their output also appears).RStudio 支持从 source 编辑器中直接执行代码(执行代码将插入控制台,并在此输出结果
25、),Executing a Single Line 执行一行代码To execute the line of source code where the cursor currently resides you press the Ctrl+Enter key (or use the Run toolbar button):你可通过 Ctrl+Enter 键来执行当前光标所在行的 source 代码(或者使用 Run 工具条按钮)After executing the line of code, RStudio automatically advances the cursor to the
26、next line. This enables you to single-step through a sequence of lines.执行完一行代码,RStudio 自动将光标跳到下一行,这将使你可以在一系列行中单步执行代码。Executing Multiple Lines 执行多行代码There are three ways to execute multiple lines from within the editor:有三种方法从编辑器中执行多行代码。 Select the lines and press the Ctrl+Enter key (or use the Run to
27、olbar button); or After executing a selection of code, use the Re-Run Previous Region command (or its associated toolbar button) to run the same selection again. Note that changes to the selection including additional, removal, and modification of lines will be reflected in this subsequent run of th
28、e selection. To run the entire document press the Ctrl+Shift+Enter key (or use the Source toolbar button). 选择这些行,按 Ctrl+Enter 键(或者使用 Run 工具条按钮) 执行选择的代码后,使用 Re-Run Previous Region 命令(或相应的工具栏按钮)再次运行相同的选择。注意:现在发生变化,包括增增加、删除和修改行将反映到后续运行中。 (或者使用 Ctrl+Shift+P 快捷键) 运行整个文件按 Ctrl+Shift+Enter 键(或使用 source 工具条
29、按钮)?The difference between running lines from a selection and invoking Source is that when running a selection all lines are inserted directly into the console whereas for Source the file is saved to a temporary location and then sourced into the console from there (thereby creating less clutter in
30、the console).从选择的行进行运行和调用 Source 的区别在于,当运行所选的所有行将直接插入到控制台中,而对于 Source 文件则保存到临时位置然后从那里反映到控制台(因此在控制台中产生较少的杂乱)。Source on Save 资源保存When editing re-usable functions (as opposed to freestanding lines of R) you may wish to set the Source on Save option for the document (available on the toolbar next to the
31、 Save icon). Enabling this option will cause the file to automatically be sourced into the global environment every time it is saved.当编辑一个再次调用函数(相对于独立的 R 行而言),你可能希望对文件设置 Source on Save 选项(通过工具条中 Save 图标的后一项实现)。启用该选项导致文件每次保存都将被全球自动采用。Setting Source on Save ensures that the copy of a function within t
32、he global environment is always in sync with its source, and also provides a good way to arrange for frequent syntax checking as you develop a function.设置 Source on Save 选项确保该函数的版本在全球环境总是能够资源同步,并且也能在你开发一个函数是提供一种好的方式安排频繁的语法检查。Keyboard Shortcuts 快捷键Beyond the keyboard shortcuts described above, there
33、are a wide variety of other shortcuts available. Some of the more useful ones include:除了上述的快捷键外,还有很多其他快捷键可用,其中最为有用的快捷键包括。 Ctrl+Shift+N New document 新文档 Ctrl+O Open document 打开文档 Ctrl+S Save active document 保存活动文档 Ctrl+1 Move focus to the Source Editor 移动焦点到 Source 编辑器 Ctrl+2 Move focus to the Console 移动焦点到控制台
Copyright © 2018-2021 Wenke99.com All rights reserved
工信部备案号:浙ICP备20026746号-2
公安局备案号:浙公网安备33038302330469号
本站为C2C交文档易平台,即用户上传的文档直接卖给下载用户,本站只是网络服务中间平台,所有原创文档下载所得归上传人所有,若您发现上传作品侵犯了您的权利,请立刻联系网站客服并提供证据,平台将在3个工作日内予以改正。