SublimeText2中如何使用R語(yǔ)言

這篇文章將為大家詳細(xì)講解有關(guān)Sublime Text 2中如何使用R語(yǔ)言,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對(duì)這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡(jiǎn)單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:空間域名、網(wǎng)頁(yè)空間、營(yíng)銷軟件、網(wǎng)站建設(shè)、道縣網(wǎng)站維護(hù)、網(wǎng)站推廣。

Using Sublime Text 2 for R

Posted on May 17, 2012 by Tom Schenk Jr

Sublime Text 2中如何使用R語(yǔ)言

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.

Initial Setup

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.

Take a look around

Sublime Text 2中如何使用R語(yǔ)言

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.

Sublime Text 2中如何使用R語(yǔ)言

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.

Sublime Text 2中如何使用R語(yǔ)言

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.

Sublime Text 2中如何使用R語(yǔ)言

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.

Package Manager

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.

Sublime Text 2中如何使用R語(yǔ)言

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.

Sublime Text 2中如何使用R語(yǔ)言

Now,

Click on Preferences -> Package Settings > SublimeREPL > Settings – User. Use the screenshot or example below and paste in the path (thanks Wojciech!).

Sublime Text 2中如何使用R語(yǔ)言

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.

Sublime Text 2中如何使用R語(yǔ)言

Further Customization: R Shortcuts

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).

關(guān)于Sublime Text 2中如何使用R語(yǔ)言就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

當(dāng)前文章:SublimeText2中如何使用R語(yǔ)言
瀏覽地址:http://www.muchs.cn/article36/ghjhpg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營(yíng)銷、虛擬主機(jī)、網(wǎng)站改版、自適應(yīng)網(wǎng)站、網(wǎng)站建設(shè)、網(wǎng)站收錄

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站