John Miyamoto - University of Washington



John Miyamoto, 1/9/02

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

* MacUsers have told me that this template does not work on a Mac. I suspect that an enterprising MacUser could adapt this template so that it would work on a Mac, but I don't know how to do this.

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 1: 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.

WARNING 2: Sometimes Word will not let you use the macros stored in R.DOT because the security settings on Word forbid the use of macros that originate with other users. If this happens, there is a solution. You can create your own R.DOT template as follows.

▪ Use File Open to open your own NORMAL.DOT. Save this file to the name, R.DOT, and then close this file. You may have to destroy (overwrite) the R.DOT file that I have provided to you - if you don't want to do this, store a copy of the original R.DOT file somewhere before destroying it (or overwriting it).

▪ Use File New to open a new document. Use Tools Templates and Add-Ins to change the attached template to R.DOT (the template you just created). Save this file to any name, e.g., Temporary RDOT File.doc. Leave this file open.

▪ You will need the file, Rmacros.txt. This file can be downloaded from ; download the zip file containing John Miyamoto's R notes, and expand this file. Rmacros.txt is contained within the Rnotes.zip file. Once you have Rnotes.txt, open this file in Word. Copy all of the Visual Basic code to the clipboard.

▪ Switch windows to the file, Temporary RDOT File.doc (or whatever you called it). Access the macros dialog box by choosing Tools Macros Macros from the menu. Change the Macros In field to R.DOT (template). Type "dummy" into the Macro Name field, and click on the Create button. (I'm assuming a macro named "dummy" doesn't already exist; if it does, choose any other novel name for this macro.) This causes you to access the macro editor of Word.

▪ At this point, you should see:

Sub dummy()

'

' dummy Macro

' Macro created by

'

End Sub

▪ Delete the dummy macro, i.e., everything from "Sub dummy()" to "End Sub", and then paste all of the Visual Basic code into the macro editor (the screen that you should be seeing immediately after deleting the dummy macro).

▪ Choose File Save R from the menu for the macro editor, and then close the macro editor.

▪ Finally close the file, Temporary RDOT File.doc. If you are prompted to save R.DOT, do so. You should now have an R.DOT template that you can attach to Word files as described above. The macros will be accessible to you because they were created on the computer that you are working on.

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 {}. |

| |Run MP. MP will ask you in which direction to search. If you reply with "1", it will search down the file from the|

| |current cursor position. If you reply with "0", it will search up the file from the current cursor position. |

| |MP will try to match the first demarcator that it encounters, i.e., if [ is the first demarcator that it encounters |

| |going down the file, then it will attempt to find the matching ]. If ] is the first demarcator that it encounters |

| |going up the file, then it will attempt to find the matching [. |

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

Assigning a Short Cut Key to the Macros Menu: Up to now, we have accessed the macros menu by choosing Tools Macros Macros from the menu. If you like using macros, it is much easier (faster) to access the macros by assigning a short cut key to the macros menu. On some computers, Ctrl-M, automatically jumps you to the macros menu, i.e., holding down the control key and simultaneously typing "m" is the equivalent of choosing Tools Macros Macros from the menu. Ctrl-M is a fast way to get to the macros menu. If Ctrl-M is not assigned to the macros menu on you computer, you can create this keyboard short cut as follows:

▪ Choose Tools Customize from the menu.

▪ Click on the Keyboard button.

▪ Change the Save Changes in field to R.DOT or NORMAL.DOT or whatever template you plan to use with your documents (probably you want R.DOT if you are going to be writing code for R).

▪ Choose Tools in the Categories field. Choose ToolsMacro in the Commands field.

▪ Put the cursor in the Press New Short Cut Key field, i.e., click in this field.

Then type Ctrl-M.

▪ Click on the Assign button. The expression "Ctrl-M" should jump from the Press New Short Cut Key field to the Current Keys field.

▪ Close the dialog box.

▪ Save the template when Word prompts you to do so (usually when you close the document). Henceforth, Ctrl-M should jump you into the macros menu. This makes it easier to run macros.

This is enough for now. There are a few other tricks available in R.DOT, but I won't go into them here.

-----------------------------------------------------------

Source Code for Macros: The Visual Basic code for the macros that are described in this document can be found in a text document Rmacros.txt.

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download