{"id":131,"date":"2012-12-28T09:05:04","date_gmt":"2012-12-28T14:05:04","guid":{"rendered":"http:\/\/www.clayford.net\/statistics\/?p=131"},"modified":"2024-11-17T10:50:03","modified_gmt":"2024-11-17T15:50:03","slug":"how-to-run-r-in-batch-mode-in-windows-7","status":"publish","type":"post","link":"https:\/\/www.clayford.net\/statistics\/how-to-run-r-in-batch-mode-in-windows-7\/","title":{"rendered":"How to run R in batch mode in Windows 7"},"content":{"rendered":"<p>I recently purchased <a href=\"http:\/\/www.amazon.com\/gp\/product\/1593273843\/ref=as_li_ss_tl?ie=UTF8&amp;tag=curiousanduseful&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=1593273843\">The Art of R Programming<\/a> with a gift card I got for Christmas. Three pages into chapter 1 it talks about running R in &#8220;batch mode&#8221;. I had never done this and thought I would give it a try. It seems very useful, especially for large programs. Here&#8217;s the example program the book provides:<\/p>\n<pre>pdf(\"xh.pdf\")\r\nhist(rnorm(100))\r\ndev.off()<\/pre>\n<p>You type that into a text file and save it as z.R. This program produces a histogram of 100 values drawn from a standard Normal distribution and outputs the histogram to a PDF file called xh.pdf. Now we want to submit that program to R without actually opening R. That means using the command line. But first we have to update our path if we&#8217;re using Windows. The book doesn&#8217;t mention this, hence the reason for this post. Here&#8217;s how you do it if you&#8217;re running 64-bit R on Windows 7:<\/p>\n<ol>\n<li><span style=\"line-height: 13px;\" data-mce-mark=\"1\">go to Control Panel<\/span><\/li>\n<li>click User Accounts and Family Safety<\/li>\n<li>click User Accounts<\/li>\n<li>click &#8220;Change my environment variables&#8221; in the left menu<\/li>\n<li>Highlight Path and click Edit<\/li>\n<li>Add semicolon at the end and then add\u00a0&#8220;c:\\Program Files\\R\\R-2.15.1\\bin\\x64&#8221; (without the quotes; update version if different from 2.15.1).<\/li>\n<li>Click\u00a0OK twice.<\/li>\n<li>Log out and log back in to have the new path settings take effect.<\/li>\n<\/ol>\n<p>Now go to the command prompt. Just go to Start and type &#8220;cmd&#8221;. Next go to the directory where your R program is located. If it&#8217;s in Documents, type &#8220;cd Documents&#8221;. Finally submit the following command:<\/p>\n<pre>R CMD BATCH z.R<\/pre>\n<p>That runs the R program and creates a PDF file in your working directory.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I recently purchased The Art of R Programming with a gift card I got for Christmas. Three pages into chapter&#8230; <a class=\"read-more\" href=\"https:\/\/www.clayford.net\/statistics\/how-to-run-r-in-batch-mode-in-windows-7\/\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-131","post","type-post","status-publish","format-standard","hentry","category-using-r"],"_links":{"self":[{"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/posts\/131","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/comments?post=131"}],"version-history":[{"count":3,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/posts\/131\/revisions"}],"predecessor-version":[{"id":977,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/posts\/131\/revisions\/977"}],"wp:attachment":[{"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/media?parent=131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/categories?post=131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/tags?post=131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}