R语言集成开发环境RStudio使用入门
RStudio能为使用R语言编程的用户提供更好的体验。本文摘取并整理《Learning RStudio for R Statistical Computing》中有关RStudio的介绍,其中英文部分直接取自原书,中文部分为本文作者整理的结构脉络,帮助读者能快速上手这款针对R语言的IDE。
一、软件和包的更新
check for updates from RStudio by clicking on Help | Check for updates.
update your installed packages:update. packages ( )
二、界面
The main panels are as follows:
1 The source editor and data viewer panel: This panel can harbor a variable number of tabs, each containing an open (source) file or a view of a data.frame
1)At the bottom left-hand side is the Jump To option that allows for easy navigation.
2)The bottom right (R Script) allows you to set the type of a file and syntax coloring explicitly.
2 The command history and workspace browser: When working with RStudio projects, a tab for version control features can be added
1)Multiple lines can be selected by holding Shift while clicking on the lines or by holding the Shift key while pressing the up and down arrow keys.
2)The commands are stored with the extension .Rhistory.
3)Selected commands can be copied to a source file by clicking on the To Source button at the top of the history panel. If no source file was open yet, a new one will be opened for you. This way you may edit the commands into a real script and store them as a .Rfile, which is usual for analyses automation.
3 The R console: This panel helps in working directly with R. It has no separate tabs
1)When you happen to get stuck in an unfinished command, you can always press Esc to exit.
4 The file, help, package, and plots panel: This panel is used for browsing files, viewing help, searching, and package (un)loading and installation
三、使用
1 Create a new project, click on the Project.
Technically, an RStudio project is just a directory with the name of the project and a few files and folders created by RStudio for internal purposes. It typically holds your scripts, data, and reports, which you may manage through RStudio's file manager tab or through your operating system's file manager. The project directory can also contain subdirectories. A commonly-used subdivision is to put all files of a certain type in the same directory, for example:
• R: The directory that holds scripts or files with custom functions
• data: All data needed for the analysis
• doc: Articles or other documents related to the analyses
• reports: A directory with generated reports from the analysis
Use paths relative to your project directory in your scripts. That way, the whole project can be moved or copied and all the scripts will still run.
2 Editing R scripts
1)To start a new R script file, click on the new file button and select R Script.
2)To open an existing file, use the Open file button
3)自动补全
a) Tab completion allows you to forget most of a function's name, and most of its arguments.
RStudio shows a pop-up menu with possible completion options that may include variables from the workspace or names of (possibly self-defined) functions. You can scroll through the options using the up and down arrow keys. Pressing Tab again (or Enter or Right) completes the command and closes the pop-up screen.
Once a function name is completed, type an opening bracket "(" and hit Tab. RStudio opens a popup with the function arguments and their descriptions from the function's help file. Pressing Tab (or Enter or right arrow key) copies the selected argument and equals symbol to the command line and closes the popup.
The Tab completion functionality attempts to complete a non-finished command in any way possible, including names of objects and functions defined by the user in R's workspace. Moreover, for objects that allow R's dollar operator, tab expansion of subobjects is available as well.
b) Enter a single or double quote at the command line and hit Tab. A popup with file and directory names in RStudio's current working directory is shown.
c) RStudio automatically completes round, square, and curly brackets with the closing bracket as soon as the opening bracket is typed. The cursor is immediately placed between the brackets. For single and double quotes, RStudio has the same behavior.
4)排版
a) A script can be re-indented by RStudio by selecting the code and choosing Code | Reindent (Ctrl + I).
b) It can be useful to comment and uncomment lines of code. In RStudio this can be done by selecting code and choosing Code | Comment/Uncomment lines (Ctrl+Shift+C).
c) RStudio can reformat comment lines with Ctrl+Shift+/. Do not reformat comments on commented code, as the inserted newline characters may break or change the working of your code after you uncomment it.
d) All the blocks with curly brackets ({}) and code sections (see the following code snippets) can be folded.
e) The syntax for a section is as follows: # <sectionname> - - - Here <sectionname> is the name that you want to assign to a section. A section can also be inserted from the RStudio menu: Code | Insert Section (Ctrl + Shift+ R). RStudio will ask you to name your section and insert the comment with the section name. In RStudio you can use sections to jump to parts of your code or to fold/unfold your code.
5)查找
a) By default searching for the texts is case insensitive, but this can be changed by selecting Match Case. It is also possible to use regular expressions (Regex) for searching and replacing your texts.
b) With Ctrl+Shift+F it is possible to search in multiple files. By default RStudio searches in the current working directory and its subdirectories, but this can be specified. Searching in multiple files results in an extra tab in the console panel named Find in Files. This panel lists all the occurrences of the search string. Clicking on an occurrence opens the file at the right location in the script editor.
c) With Code | Jump To... (Alt+Shift+J) it is possible to jump to functions and code sections within the current file. RStudio shows the available destinations at the bottom of the window. A related navigation feature is hitting the F2 key by selecting the name of a function. RStudio will open up the file with the function definition. This even works for functions from base R and R extension packages. Functions definitions without curly braces will not be found bythe jump-to function.
d) Even more useful is the Code/Go to File/Function... (Ctrl+.) option. It helps to quickly locate and load functions in your script files. RStudio will show all the available functions and files in the current working directory and its subdirectories that start with the characters you type. Behind function names is the script file where it is located.
e) RStudio allows you to navigate between files using Back (Ctrl+F9) and Forward (Ctrl+F10). RStudio remembers the positions where edits were made and facilitates jumping between them.
6)运行
To execute the current line or selection use Ctrl+Enter. The previous command can be rerun using Ctrl+Shift+P. RStudio makes it easy to execute (or source) all the lines of a script file with Ctrl+Shift+Enter. This will copy all the lines to the console and execute them. The output of the script is printed in the console windows. Note that RStudio treats the execution of all the lines as one statement.