'r'에 해당되는 글 2건

  1. 2011.06.20 SYNTAX EXAMPLES FOR R STATISTICAL PROGRAM
  2. 2011.06.20 R in Emacs

SYNTAX EXAMPLES FOR R STATISTICAL PROGRAM

OSX 2011. 6. 20. 16:42
:

R in Emacs

OSX 2011. 6. 20. 16:24

USING 64-BIT R FROM WITHIN AQUAMACS


1) Aquamacs is terrific because it comes bundled with about everything you need to start using R with it. No need to separately download ESS (emacs speaks statistics) or mess with the .ess-lisp files. Download Aquamacs from http://aquamacs.org/ and install it.

2) Now any file with an “.R” extension that is opened with aquamacs will automatically have syntax highlighting AND will allow you to start 64-bit R. I highly recommend checking out this page, http://ess.r-project.org/, and downloading the ESS reference card therein if you are new. Emacs has a bit of a learning curve, but you’ll begin loving its capabilities as an aid to programming very soon.

3) To start 64-bit R from within aquamacs hit <Control>-u then <Alt>-x then “R” then <Return>. Aquamacs will ask you for “Starting Args”. Here’s where you tell it you want to run 64-bit R. Type “--arch=x86_64” then <Return>. A new window will open up that is the R session.

4) That’s almost it. Just one more issue: graphing will crash (something to do with the new R and X11). Aquamacs has X11 as the default (null) graphics device. To change this, type in R: “options(device=”quartz”)”. Now your null graphics device is quartz and it should all work out. If you really want to use X11 to write graphics, you can use x11(type="Xlib")' for each new graph. What I do is to set “options(device=”quartz”) within my Rprofile.site file.


USING EMACS TO LOG IN TO A REMOTE SERVER & RUN R INTERACTIVELY

It can be very helpful to run an interactive session of R on a remote server, which may have faster CPUs and more memory than the computer you’re on, or may have datasets on it you don’t have access to otherwise. This is simple to do using aquamacs. Here’ how:

1) Open up your *.R script you’d like to use

2) Open a shell inside Emacs by typing “M-x shell”

3) From within this shell, ssh to the server you want to use. When doing this, you need to make sure to specify two important ssh options: compression (which compress data coming to you, making the connection seem *much* faster) and X11 forwarding (which allows you to use interactive graphing features via X11). E.g.:
ssh -XC username@servername.colorado.edu

4) You should now be logged into the server, just as you wold be if you’d used terminal rather than emacs. Now open up R as you usually would on that server. E.g.:
R --arch=x86_64

5) You should be in R now. To allow this R session to be linked to your *.R script, use this command in the remote R session:
M-x ess-remote
In the Emacs mini-buffer prompt, type:
r

6) Now you should be able to send code from your *.R script to the remote R session as you normally would (e.g., C-c C-j).

7) Last, you need to change the options in your remote R session to graph using X11 rather than whatever default driver is being used. To do this in R, type:
options(device=’x11’)

8) That’s it. Make sure it all works by typing something like:
hist(rnorm(50)) #which should return a histogram of rnorm to your screen!

NEW PACKAGES

$ which R
/usr/bin/R

Install the package:

$ sudo R CMD INSTALL plotrix_2.6-1.tgz
Password:
* Installing to library '/Library/Frameworks/R.framework/Resources/library'
* Installing *binary* package 'plotrix' ...
* DONE (plotrix)

Check that everything is happy:

$ ls -l /Library/Frameworks/R.framework/Resources/library/


BASIC COMMANDS

Basic ESS commands to get started using R/S-Plus under Emacs ( C-c means press the Ctrl key down and then enter c. )

  • Esc-p Previous Command (at command line)
  • Esc-n Next Command (at command line)
  • C-c C-v Get help on R/S object (enter name in
  • C-c C-d Dump object or function to a Emacs buffer for editing
  • C-c C-l Load file from buffer into R
  • C-c C-f submit a Function in current buffer to R
  • C-c C-j submit the current line in buffer to R
  • C-c C-r submit the highlignted or marked region to R
  • C-c TAB complete object/file name
  • C-h m for help on ESS mode
  • More help is available under the iESS menu in emacs:
    • about (Emacs Info system for on-line help for ESS)
    • describe (for Control/Escape sequences and definitions for ESS)


: