十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
这篇文章将为大家详细讲解有关Sublime Text 2中如何使用R语言,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:空间域名、网页空间、营销软件、网站建设、道县网站维护、网站推广。
Posted on May 17, 2012 by Tom Schenk Jr
My R interface has been pretty basic in the last few years. I have usually stuck to the R console. Yes, I’ve tried Emacs with ESS; a staple, but it is so unbearably antiquated that I always gave up on its significant learning curve. GUI packages–especially Rstudio–offer viable alternatives, but I feel the GUI lets me lose focus of the code. I have been envious of TextMate for Mac, but alas, I’m not a Mac user. Recently, though, I’ve moved to Sublime Text 2. With some nudging, I have been able to mimic the typical R console environment in the more-powerful Sublime Text program.
Want to skip to the basic instructions without the narrative? Just read the block quotes for specific instructions.
Sublime Text 2 offers a similar experience as TextMate (e.g., autocompleting parenthesis and quotation marks) and integrates well with R and other programming languages. R users aren’t the primary target and it takes a little kick to get the environment perfected for the R community. This is a little tutorial to give R users a similar experience with Sublime Text without ruining the program’s versatility. That is, you can have a familiar R experience and easily use Sublime Text with programming languages (e.g., JavaScript, C++) and typesetting programs (e.g., LaTeX, Markdown, MultiMarkdown).
This setup will let you
Browse R code with syntax highlighting
Use the familiar R console within Sublime Text
Send R code from a script to the R control (equivalent to Ctrl + R)
Organizing the layout so the script and console can be viewed simultaneously.
The first and last items are natively supported, but we’ll need to make some additional adjustments for the second and third item.
I will presume some user knowledge here and will focus on the Windows user. Setup is very easy. Their website has links to executable autoinstallers, you won’t need to unzip and manual install nor are you depending on a third party to graciously compile code on your behalf.
Follow the instructions and install using the default settings.
Before proceeding, take a look at the R interface. Similar to other programs (e.g., Notepad++), Sublime Text offers native syntax highlighting for R code. Load one of your codes or try a sample script. If you don’t see any highlighting, you can always tell Sublime Text by clicking View > Syntax and choosing R. Otherwise you can look in the lower right-hand corner as is shown in the image.
A nifty feature is autocompleting parenthesis and other special characters. Type “write.csv(” and it’ll auto-insert the right paren “)“. Little underlines help you match each left parenthesis with the right parenthesis. It also works with “{“, “[", single, and double quotation marks.
Comments have a lighter tone. So does the ubiquitous "<-". Functions and special characters also have highlighting. If you don't like the color scheme, you can change it under Preferences > Color Scheme. The default is Monokai, Twilight is a very dark theme while iPlastic is a very white theme.
Once you type a variable or function names, you can easily reference it later for autocompletion. Load the data into a variable called "sampleData" and you only need to type "sa" and hit tab to complete the name. If there are multiple possible matches, then you can use arrow keys to choose. The limitation of this, however, is that Sublime Text treats "sample.data" as two separate names. To counter this, I've started to use underscores, which is friendlier. Also, Sublime Text is using a simple approach--it simply refers to what you've typed before, it is not remembering variables in your memory the same way the GUI interfaces do.
Click on Preferences > Key Bindings - User, then copy and paste the following (thanks G-Force!)
There are plenty of shortcuts. Select a few lines of code and press Ctrl + /to toggle comments. Ctrl + Lwill select the entire line based on your cursor. Ctrl + G, type in a number and you’ll jump to that line of code. Type Ctrl + :and type a variable name, it’ll find all instances. If you have multiple tabs, type Ctrl + Pand you can jump to specific tabs.
At this point, however, you can’t execute or “compile” code. It’s just pretty font, helpful quirks, and shortcuts.
Our next goal is to be able use the R console and do calculations within Sublime Text. We’re going to use the SublimeREPL package to accomplish this. Don’t worry, this isn’t a rarefied thing that will clutter your RAM just for R; you’ll probably use this package for other things too.
Visit the Sublime Package Control website and follow the instructions to install the package manager on your computer.
To access Package Manager type “Ctrl + Shift + P” to bring up a text box. Without using your mouse, begin to type “Install package” (without quotation marks). You may see a error related to git.exe, but you can ignore it at the moment.
Then type “SublimeREPL” and hit enter when it’s highlighted.
SublimeREPL essentially allows Sublime Text to access compilers or other programs. You can write in Sublime Text and send it to R for the computing. The results are sent back to Sublime Text for display. For you, it will operate like the normal R console.
However, we need to tell SublimeREPL where R is located on our computer. We need to add R to the Windows Path.
Find where R is installed. To do this, right click on your R icon–whether it’s start menu, desktop, or taskbar. Under the “Shortcut” tab, note the file path under “Target:”. Copy and paste this to a text document, you’ll need this shortly.
Now,
Click on Preferences -> Package Settings > SublimeREPL > Settings – User. Use the screenshot or example below and paste in the path (thanks Wojciech!).
For example, you might paste:
Notice in my example that I am ignoring (disabled) some packages I have installed. In order for this to work, I needed to add a comma after the bracket on line 8 and pasted the “default_extend_env” within the curly brackets.
Now you have told where Sublime Text can look to find R.
Open the R Console by clicking Tools > SublimeREPL > R. A window will pop up with the familiar R console header.
The R console is fantastic because you can quickly execute code from a script. SublimeREPL does have this capability, but the R user will want to use Ctrl+R, or something similar. Additionally, the native shortcut to execute script has some issues because it conflicts with other shortcuts already built-in Sublime Text.
Sublime Text calls these shortcuts “keybindings”. Fortunately, they can be edited. However, you have to be careful since Sublime Text has a lot of shortcuts and it’s easy to duplicate them, causing further issues.
I have a proposed set that should be pretty R user friendly. You can select some code (multiple, partial, or single lines) and run it in R using Ctrl + Shift + R. This is similar to the shortcut in the R console (a previous conflict prevents Ctrl + R without a lot of work).
关于Sublime Text 2中如何使用R语言就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。