3/23/01 5:04 PM File = E:\r\rdot.doc1

John Miyamoto, 3/23/01

3/23/01 5:04 PM File = E:\r\rdot.doc1

Instructions for Using the R.DOT Template with Microsoft Word.

It is easier to use R if you use a programming editor like Emacs or UltraEdit while writing the code. The Word template R.Dot lets you use Microsoft Word like a programming editor.

1.Put the Word template, R.DOT, on your computer in a convenient directory.

2.Open a new document.

3.Click on /Tools /Templates and Add-Ins.

4.Click on the Attach button, and change the Document Template from NORMAL.DOC to R.DOT as the template for the document.
(Optional) If you have autotext entries or macros that you like to use that are stored in NORMAL.DOT, you can still access these items if you add NORMAL.DOT to the list of currently loaded templates (make sure it is checked before you click OK).

WARNING: In theory, Word should let you create documents with R.DOT as the template. In other words, you should be able to choose /File /New from the File menu; then choose Create New Document (this is the default); then choose R.DOT as the attached template; then click on OK. I said that you could do this "in theory" because in fact, my program hangs up when I do this. Strangely, I do not have any problems if I follow steps 1 - 4 listed above. Maybe someone can tell me what is going on. I find that the easiest way to use the R.DOT template is to create a document by means of steps 1-4, save it under the name RDEFAULT.DOC, and then close it. Whenever I want to create a document that is attached to the R.DOT template, I load RDEFAULT.DOC, immediately save it under a new name, and then proceed to create R-code in this new document. RDEFAULT.DOC always stays ready to serve as a blank R.DOT document for future R programs.

The main reason it is easier to write R code with the R.DOT template is that R.DOT contains macros that simplify writing and running R programs. For example, the RL macro copies to the clipboard the line of code that contains the cursor, then transfers control to R, and runs this line of code. For example, the MP macro finds matching pairs of parentheses "()" or brackets "[]" or curly brackets "{}". See the description of macros below for more examples.

Instructions for using the macros in R.DOT. Assuming that R.DOT is attached to your document, you can run the following macros.

Macro Name / Purpose
ip / IP inserts the markers for the beginning and end of a "paragraph" of code. These markers are "#First: ------" for the beginning and "#Last: ------" for the end of an R-code paragraph. You can now write R-code pertaining to a specific aspect of the project between these "paragraph" markers. The macro RP runs the code between any pair of "paragraph" markers. I use "section" as a larger unit of R-code, and "paragraph" as a subunit of R-code.
isec / ISEC inserts the markers for the beginning and end of a section of code. These markers are"#Section: ------" for the beginning and "#EndSection: ------" for the end of a section of code. You can now write R-code pertaining to a section of the project between these section markers. The macro RS runs the code between any pair of section markers.
mp / MP finds matching demarcators. The demarcators are (), [], and {}. Place the cursor anywhere to the left of the to-be-matched demarcator, and then run MP.
rl / RL runs the line of R-code in which the cursor is positioned. If more than one line is highlighted, then rl runs all of the code in any line that is at least partially highlighted.
rp / RP runs the current paragraph, i.e., runs all of the code between the paragraph markers in which the cursor is positioned. If more than one paragraph is currently highlighted, then rs runs all of the code in any paragraph that is at least partially highlighted.
rs / RS runs the current section, i.e., runs all of the code between the section markers in which the cursor is positioned. If more than one section is currently highlighted, then rs runs all of the code in any section that is at least partially highlighted.
Paragraph Styles in R.DOT
Paragraph Style / Purpose
normal, n / Designated either by "normal" or "n". The normal style is currently set to single space with unicode Arial font. This is the standard paragraph style for programming R.
sm / Style sm is used to put lines of code into the background where they will be less visible. It does not change the status of the code, i.e., it still runs. It simply makes the code less visibly prominent. The purpose of sm is to allow the programmer to focus on the "normal" code.

EXAMPLE: First check that R.DOT is attached to this document (click on /Tools /Templates and Ad Ins). If it isn't, then use steps 1-4 above to attach R.DOT to this document. Now let's try come macros.

#Section: ------

x _ c(1,3,5)

y _ c(2,4,6)

x + y

#EndSection: ------

The RS macro:

Put the cursor on any line between #Section and #EndSection.

Now run the RS macro (/Tools /Macros /Macros, type rs under Macro Name, and then click OK.

What RS does is copy all of the code between #Section and #EndSection to the clipboard, then transfers control to R, then runs the code. Obviously you can use the the RS macro to revise and perfect a section of code. How do you create the section boundaries, "#Section: ------" and "#EndSection: ------"?

Put the cursor on the blank line following this paragraph; then run the ISEC macro.

What you should see is that the macro automatically inserts section boundaries into this file. You can type code between these boundaries and use RS to run the code. Now let us try some other macros.

#Section: ------

mat _ matrix(c( 1, 3, 5, 2, 4, 6, 9,6, 4), ncol=3, byrow=T)

dimnames(mat) _ list(c("R1","R2","R3"), c("C1","C2","C3")

mat

#EndSection: ------

First, place the cursor within the preceding section and run RS. You should see that mat is the matrix:

C1 C2 C3

R1 1 3 5

R2 2 4 6

R3 9 6 4

Suppose that you wanted to compute mat + 10. You could change "mat" to "mat + 10" and use RS again, but there is a simpler way. Add the characters " + 10" after "mat"; leave the cursor on the line that now shows "mat + 10"; then run the RL macro.

You should see that RL runs the current line, i.e., computes mat + 10.

Here is another use of RL. Change 'c("C1","C2","C3")' to 'c("D1","D2","D3")'. Now sweep the mouse with the left button depressed so that you highlight at least part of the two lines, 'dimnames(mat) _ list(c("R1","R2","R3"), c("D1","D2","D3"))' and 'mat'. Now run the RL macro.

You should see that RL runs both line, i.e., the column names are now "D1", "D2" and "D3", and the second line computes mat + 10.

Here is another trick. Put the cursor slightly to the right of the "(" in the expression 'list(c("R1","R2","R3"), c("D1","D2","D3"))'. Now run the MP macro. It should show you what are the matching parentheses.

Try deleting the final ")" in the expression 'list(c("R1","R2","R3"), c("D1","D2","D3"))', i.e., it should look like ''list(c("R1","R2","R3"), c("D1","D2","D3")'. Now put the cursor slightly to the right of the "(" in the expression 'list(c("R1","R2","R3"), c("D1","D2","D3"))'. Now run the MP macro. You should see what happens when the macro can't find the matching parentheses.

FYI: MP is able to find matching "()", matching "[]", and matching "{}". Also, the MP macro is set to search in the Down direction by default. At the beginning of the macro, you are given the option of searching in the Up direction.

This is enough for now. There are a few other tricks available in R.DOT.