使用 R 交互窗口Work with the R interactive window

针对 Visual Studio 的 R 工具 (RTVS) 提供了一个 R 交互窗口,也称为 REPL(读取-求值-打印-循环)窗口,可在其中输入 R 代码并立即查看结果。R Tools for Visual Studio (RTVS) provides an R interactive window, also known as a REPL (Read-Evaluate-Print-Loop) window, in which you can enter R code and immediately see the results. 所有模块、语法、变量以及 IntelliSense 都可在该交互窗口中使用。All modules, syntax, and variables, as well as IntelliSense, is available in the interactive window.

交互窗口还与常规 R 编辑器窗口集成。The interactive window is also integrated with regular R editor windows. 可选择代码并按 Ctrl+Enter,也可右键单击并选择“交互执行”,代码将在交互窗口中逐行运行,就像直接键入一样。You can select code and press Ctrl+Enter, or right-click and select Execute in Interactive, and the code is run line-by-line in the interactive window as if you typed it directly. 如果光标位于编辑器窗口中的某一行上,则按 Ctrl+Enter 将该行发送至交互窗口,然后将光标移到下一行。When the cursor is on a single line in an editor window, Ctrl+Enter sends that line to the interactive window and then moves the cursor to the next line. 这样,只需重复按 Ctrl+Enter 即可逐行执行代码。This way you can just press Ctrl+Enter repeatedly to step through the code.

要体验这些功能,可按照 R 入门演练及本文中的相关部分进行操作。To experience these features, follow the Get started with R walkthrough as well as the sections in this article. 代码片段在交互窗口中的运行方式与在 R 编辑器窗口中的运行方式一样。Code snippets also work in the interactive window like they do in R editor windows.

交互窗口概述Overview of the Interactive Window

在代码行末尾键入有效的 R 代码,按 Enter 可以运行该行代码:Typing valid R code and pressing Enter at the end of the line runs the code on that line:

> 3 + 3
[1] 6

在单行输入中的任意位置按 Enter,也会运行该代码行。Pressing Enter anywhere on a single-line input also runs that line.

REPL 中以前的所有输入和输出都是只读的,不能更改。All previous input and output in the REPL is read-only and cannot be changed. 但是,随时都可从窗口中复制并粘贴文本。However, you can select and copy text from the window at any time, as well as pasted. 粘贴的代码会像逐行输入的代码一样运行。Pasted code runs as if it were entered line by line.

也就是说,如果开始键入语句并按 Enter,RTVS 将知道语句何时需要继续,并通过左侧的 + 提示和适当的缩进进入多行模式。That is, when you start typing a statement and press Enter, RTVS knows when the statement must be continued and enters multi-line mode with a + prompt on the left and the appropriate indentation. RTVS 还会使用成对的圆括号、方括号和大括号:RTVS also completes parentheses, brackets, and curly braces:

交互窗口中的多行语句条目

在这个多行模式下,仅当位于代码块末尾时,按 Enter 才会运行该代码块,否则将插入新行。In this multi-line mode, the Enter key runs the code block only when positioned at the end of the block, otherwise it inserts a new line. 但是,可在任意位置按 Ctrl+Enter 来立即运行代码块。However, you can press Ctrl+Enter at any position to run that code block immediately.

工具栏命令Toolbar commands

以下是交互窗口及其工具栏:Here is the interactive window with its toolbar:

交互窗口与工具栏

工具栏命令如下所示,其中多数命令具有键盘等效项,并且可在“R 工具” > “会话”和“R 工具” > “工作目录”菜单中获取(或如上所述):The toolbar commands are as follows, most of which have keyboard equivalents and are also available on the R Tools > Session and R Tools > Working Directory menus (or as noted):

ButtonButton 命令Command 组合键Key combination 说明Description
“重置”按钮 重置Reset Ctrl+Shift+F10Ctrl+Shift+F10 重置交互窗口会话,清除所有变量和历史记录。Resets the interactive window session, clearing all variables and history.
“清除”按钮 清除Clear Ctrl + LCtrl+L 清除交互窗口中显示的输出,不影响会话变量或历史记录。Clears the output shown in the interactive window; does not affect session variables or history.
“历史记录”按钮 “上一条历史记录”命令Previous History Command
“下一条历史记录”命令Next History Command
向上,向下Up, Down
Alt+向上键,Alt+向下键Alt+Up, Alt+Down
滚动浏览历史记录,以及多行代码块的某些行为。Scrolls through the history, with certain behaviors for multi-line code blocks. 请参阅历史记录See History.
“加载工作区”按钮 加载工作区Load Workspace n/an/a 加载以前保存的工作区(请参阅工作区和会话Loads a previous saved workspace (see Workspaces and sessions.
“工作区另存为”按钮 工作区另存为Save Workspace As n/an/a 将当前状态的会话另存为工作区(请参阅工作区和会话Saves the current state of the session as a workspace (see Workspaces and sessions.
“源化 R 脚本”按钮 源化 R 脚本Source R Script Ctrl + Shift + SCtrl+Shift+S 在 Visual Studio 编辑器中使用当前活动的 R 脚本调用 source,它将运行代码。Calls source with the currently active R script in the Visual Studio editor, which runs the code. 仅当在 Visual Studio 编辑器中打开 R 文件时,才会显示此按钮。This button appears only when an R file is open in the Visual Studio editor.
“源化带有回响的 R 脚本”按钮 源化带有回响的 R 脚本Source R Script with Echo Ctrl+Shift+EnterCtrl+Shift+Enter 与源化 R 脚本相同,但在交互窗口中显示脚本内容。Same as Source R Script but displays the script's contents in the interactive window.
“中断 R”按钮 中断 RInterrupt R EscEsc 停止交互窗口中任何正在运行的代码,如该部分开头的屏幕截图中的 while 循环。Stops any running code in the interactive window, such as the while loop in the screenshot shows at the beginning of this section.
“附加调试器”按钮 附加调试器Attach Debugger n/an/a 也可使用“调试” > “附加到 R 交互”命令。Also available using the Debug > Attach to R Interactive command.
“将工作目录设置为源文件位置”按钮 将工作目录设置为源文件位置Set Working Directory to Source File Location Ctrl+Shift+ECtrl+Shift+E (使用 source)将工作目录设置为加载到交互窗口的最新源文件。Sets the working directory to the most recently sourced file loaded to the interactive window (using source). 请参阅工作目录See Working directory.
“将工作目录设置为项目位置”按钮 将工作目录设置为项目位置Set Working Directory to Project Location Ctrl+Shift+PCtrl+Shift+P 将工作目录设置为 Visual Studio 中当前加载项目的根目录。Sets the working directory to the root of the currently loaded project in Visual Studio. 请参阅工作目录See Working directory.
(文本字段)(Text field) 选择工作目录Select Working Directory n/an/a 工作目录的直接输入字段。Direct input field for the working directory. 请参阅工作目录See Working directory.

工作区和会话Workspaces and sessions

在交互窗口中运行代码会在当前会话中生成上下文。Running code in the interactive window builds up a context in your current session. 上下文由全局变量、函数定义、库加载等组成。The context is composed of global variables, function definitions, library loads, and so forth. 这个上下文统称为“工作区”,可随时保存和加载工作区。This context is collectively called a workspace, and you can save and load workspaces at any time.

如果选择“工作区另存为”按钮或使用“R 工具” > “会话” > “工作区另存为”命令,系统会提示你输入位置和文件名(默认扩展名为 .RData)。Selecting the Save Workspace As button or using the R Tools > Session > Save Workspace As command prompts you for a location and filename (default extension is .RData).

要使用特定文件名(默认为 .RData)保存工作区,请单击 REPL 中的“保存工作区”按钮:To save a Workspace using a specific filename (the default is .RData), click on the Save Workspace button in the REPL:

要重新加载之前保存的工作区,请选择“加载工作区”按钮,或使用“R 工具” > “会话” > “加载工作区”并导航到工作区文件。To reload a previously saved workspace, select the Load Workspace button or use R Tools > Session > Load Workspace and navigate to the workspace file.

使用“重置”按钮或“R 工具” > “会话” > “重置”,可清除会话上下文。The Reset button or R Tools > Session > Reset clears the session context. 如果使用远程会话,重置操作还会删除远程计算机上的用户配置文件,清除其中存储的所有文件。If you're using a remote session, resetting also deletes the user profile on the remote machine to clear off all files stored there. (请参阅工作区。)(See Workspaces.)

工作目录Working directory

开发人员通常想要在进行交互会话时更改其工作目录。Developers commonly want to change their working directory while in an interactive session. 工具栏、“R 工具” > “工作目录”菜单和项目上下文菜单中提供了许多命令,它们可用于轻松地将工作目录设置为源文件位置、项目位置或其他任意位置。Various commands, available on the toolbar, the R Tools > Working directory menu, and the project context menu allows you to easily set a working directory to the location of a source file, the location or your project, or any other arbitrary location. 这样做有助于避免在引用文件时键入完整路径或冗长的相对路径。Doing so helps you avoid typing out full pathnames or lengthy relative pathnames when referring to files.

历史记录History

在交互窗口中输入的每行代码(包括从编辑器发送的代码行)都将保留在 REPL 的历史记录中。Every line you enter in the interactive window, includes lines sent from an editor, are preserved in the REPL's history. 然后,可以使用向上键和向下键浏览历史记录,就像使用命令行时可能习惯进行的操作一样。You can then navigate through history with the Up and Down arrow keys, as you are likely accustomed to on the command line.

区别在于,如果在当前行上开始键入,并按向上键,即使尚未运行该行,它也将保留在历史记录中。One difference is that if you start typing on the current line and press Up, that current line is preserved in your history even through you haven't run that line yet.

交互窗口中的历史记录还会智能地与其他跨行代码块的语句进行协作。History in the interactive window also works intelligently with statements of other code block that span lines. 如果使用向上键和向下键循环浏览历史记录,将以整体单位的形式检索多行代码块,并且这些代码块将显示为当前项。When cycling through history with the Up and Down arrow keys, multi-line code blocks are retrieved as a whole unit and shown as the current entry. 此时,使用箭头键可逐行浏览代码块,直到到达顶部或底部。At this point, the arrow keys navigate through that code block line by line, until the top or bottom is reached. 在代码块顶部,使用向上键将检索上一条历史记录项,而在底部,使用向下键将检索下一条历史记录。At the top of the code block, the up arrow retrieves the previous item in the history; at the bottom line, the down arrow retrieves the next item.

此行为的一个典型用例是:使用向上键和 Enter 键击组合重新运行上一条历史记录,同时通过按向上键导航到多行代码块中,自然允许对该代码块进行编辑。This behavior accommodates the typical case of rerunning the last item in the history with an Up arrow and Enter keystroke combination, while naturally allowing for editing of a multi-line code block by pressing the Up arrow to navigate into it.

要避免导航到多行代码块中,可使用工具栏按钮或 Alt+向上键和 Alt-向下键,这样所有此类代码块均将被视为单行。To avoid navigating into multi-line code blocks, use the toolbar buttons or Alt+Up and Alt-Down, and all such blocks are treated as a single line.

体验历史记录功能的最简单方式是亲自在交互窗口中尝试。The easiest way to experience the history features is to try them for yourself in the interactive window. 以下代码可提供多个适合的单行语句和多行语句。The code below provides several suitable single- and multi-line statements. 但是,请单独复制粘贴每个语句,创建相应的历史记录。Use copy-paste with each statement individually, however, to create the appropriate history. (还可将代码粘贴到单独的代码文件中,然后使用 Ctrl+Enter 将其发送到交互窗口。)(You can also paste the code into a separate code file and then send the lines to the interactive window with Ctrl+Enter.)

3 + 3

4 + 4

5 + 5

add <- function (x, y) {
  return (x + y)
}

sub <- function (x, y) {
  return (x - y)
}