{"id":512,"date":"2010-11-29T19:27:06","date_gmt":"2010-11-30T00:27:06","guid":{"rendered":"http:\/\/lukemiller.org\/?p=512"},"modified":"2011-05-11T09:54:41","modified_gmt":"2011-05-11T13:54:41","slug":"sorting-out-sweave-in-eclipsestatet","status":"publish","type":"post","link":"https:\/\/lukemiller.org\/index.php\/2010\/11\/sorting-out-sweave-in-eclipsestatet\/","title":{"rendered":"Sorting out Sweave in Eclipse\/StatET"},"content":{"rendered":"<p>Using Sweave to produce pretty-looking documentation for R is awfully handy. It takes a little tweaking to get set up in Eclipse and StatET though. I followed the information in<a href=\"http:\/\/jeromyanglim.blogspot.com\/2010\/02\/getting-started-with-sweave-r-latex.html\"> Jeromy Anglim&#8217;s webpage<\/a> to originally get Sweave set up. The following is my experience getting it working with Eclipse 3.5, StatET  0.9.1.b201011060900E35sw, Sweave Add-on for StatET 0.9.0.b201011060900E35sw, MiKTeK 2.9, and R 2.12.0 i386 on Windows 7 x64 circa November 2010.<\/p>\n<p>I initially wrote a .Rnw file to be run through Sweave and converted to a pdf. I then set up Eclipse to process the .Rnw file using Sweave. I went to Run&gt;External Tools&gt;External Tool Configurations&#8230;<br \/>\nThere I created a new configuration under the Sweave Document Processing tree item, called Sweave2 in the image below.<\/p>\n<figure id=\"attachment_513\" aria-describedby=\"caption-attachment-513\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/11\/sweave1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-513\" title=\"sweave1\" src=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/11\/sweave1-300x205.png\" alt=\"\" width=\"300\" height=\"205\" srcset=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/11\/sweave1-300x205.png 300w, https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/11\/sweave1.png 845w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-513\" class=\"wp-caption-text\">External configuration. The stock settings are shown here. <\/figcaption><\/figure>\n<p>Next I used Run&gt;External Tools to choose my Sweave document processing configuration. It returned an error in the R console:<\/p>\n<figure id=\"attachment_514\" aria-describedby=\"caption-attachment-514\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/11\/Sweave_error1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-514\" title=\"Sweave_error1\" src=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/11\/Sweave_error1-300x172.png\" alt=\"\" width=\"300\" height=\"172\" srcset=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/11\/Sweave_error1-300x172.png 300w, https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/11\/Sweave_error1.png 809w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-514\" class=\"wp-caption-text\">That was a failure.<\/figcaption><\/figure>\n<p>The error reported there is<br \/>\n<code>Error:  chunk 1<br \/>\nError : '\\R' is an unrecognized escape in character string starting \"D:\\R\"<\/code><br \/>\nThis means that while processing chunk 1 in my .Rnw file, R freaked out because Sweave interpreted the first part of the path to the directory of the .Rnw file (D:\\R\\Misc_scripts\\test2.Rnw) as a special-character escape switch (&#8220;\\R&#8221;) which is meaningless in this context. Recall that R expects Windows file paths to be entered with forward slashes (D:\/R\/Misc_scripts\/test2.Rnw), because the backslash \\ is reserved for escape characters.<\/p>\n<p>This appears to be a result of the path being supplied by Eclipse to R using the backslashes (D:\\\\R\\\\Misc_scripts\\\\test2.Rnw). Normally the double backslashes should work in R, but this isn&#8217;t apparently isn&#8217;t the case with this interaction between Sweave and Eclipse. The bug in R-2.12.0 was fixed in the newer patch, R-2.12.1 though, so you could leave the original <code>Sweave(file = \"${resource_loc:{source_file_path}}\")<\/code> code in place now. However, there are several other reasons to run the Sweave command from the same working directory as your .Rnw file, particularly if you&#8217;re lazy about coding in relative path names to access data files or if you like to keep your Sweave output figures in a separate folder with a relative path name specified in the .Rnw document.<\/p>\n<p>My alteration is to modify the Eclipse external tool configuration so that Eclipse only passes the name of the file, without a path prepended onto it. This can be done in the Run&gt;External Tools&gt;External Tool Configuration window, by altering the code in the &#8220;Run command in active R Console&#8221; code so that it reads <code>Sweave(file = \"${resource_name}\")<\/code> as shown in the image:<\/p>\n<figure id=\"attachment_515\" aria-describedby=\"caption-attachment-515\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/11\/Sweave2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-515\" title=\"Sweave2\" src=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/11\/Sweave2-300x240.png\" alt=\"\" width=\"300\" height=\"240\" srcset=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/11\/Sweave2-300x240.png 300w, https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/11\/Sweave2.png 761w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-515\" class=\"wp-caption-text\">Change the command passed to R to only include the resource name.<\/figcaption><\/figure>\n<p>In doing this, you now have to have your working directory in the R console set to be the same as the directory where the .Rnw file lives that you&#8217;re trying to process.<\/p>\n<p>Having solved that minor problem, the next issue was that my .tex file was being created, but when texi2dvi was being called by Eclipse in the R console, MiKTeK could not find a Sweave.sty file to help process the .tex file. That manifested itself with this error window:<\/p>\n<figure id=\"attachment_516\" aria-describedby=\"caption-attachment-516\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/11\/Sweave_error2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-516\" title=\"Sweave_error2\" src=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/11\/Sweave_error2-300x185.png\" alt=\"\" width=\"300\" height=\"185\" srcset=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/11\/Sweave_error2-300x185.png 300w, https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/11\/Sweave_error2.png 531w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-516\" class=\"wp-caption-text\">MiKTeK can&#39;t find the Sweave.sty file, so this window pops up in Eclipse.<\/figcaption><\/figure>\n<p>One way to fix this error would be to place a copy of the Sweave.sty file in the same directory as your .Rnw file. The basic Sweave.sty file for R can typically be found in your R directory under share\/texmf\/tex\/latex\/Sweave.sty<\/p>\n<p>Alternately, you can inform MiKTeK of where your Sweave.sty file lives (in that same share\/texmf\/tex\/latex\/ directory) using the directions found here: <a href=\"http:\/\/docs.miktex.org\/manual\/localadditions.html#id573835\">http:\/\/docs.miktex.org\/manual\/localadditions.html#id573835<\/a>. My version of that fix is as follows:<\/p>\n<p>Open the MiKTeK Settings control panel by going to Start&gt;MiKTeK 2.9&gt;Maintenance&gt;Settings. Click on the Roots tab when the control panel opens.<\/p>\n<figure id=\"attachment_517\" aria-describedby=\"caption-attachment-517\" style=\"width: 248px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/11\/MikTeX_fix1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-517\" title=\"MikTeX_fix1\" src=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/11\/MikTeX_fix1-248x300.png\" alt=\"\" width=\"248\" height=\"300\" srcset=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/11\/MikTeX_fix1-248x300.png 248w, https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/11\/MikTeX_fix1.png 296w\" sizes=\"auto, (max-width: 248px) 100vw, 248px\" \/><\/a><figcaption id=\"caption-attachment-517\" class=\"wp-caption-text\">Opon the MiKTeK settings window. <\/figcaption><\/figure>\n<p>On the Roots tab, choose &#8220;Add&#8221; at the bottom. Browse to the share\/texmf\/ directory in your R installation.<\/p>\n<figure id=\"attachment_528\" aria-describedby=\"caption-attachment-528\" style=\"width: 269px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/11\/MikTeX_fix21.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-528\" title=\"MikTeX_fix2\" src=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/11\/MikTeX_fix21-269x300.png\" alt=\"\" width=\"269\" height=\"300\" srcset=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/11\/MikTeX_fix21-269x300.png 269w, https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/11\/MikTeX_fix21.png 413w\" sizes=\"auto, (max-width: 269px) 100vw, 269px\" \/><\/a><figcaption id=\"caption-attachment-528\" class=\"wp-caption-text\">Choose the \/share\/texmf directory in your current R installation folder. <\/figcaption><\/figure>\n<p>After doing this, you should see that directory path in the Roots tab:<\/p>\n<figure id=\"attachment_519\" aria-describedby=\"caption-attachment-519\" style=\"width: 268px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/11\/MikTeX_fix3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-519\" title=\"MikTeX_fix3\" src=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/11\/MikTeX_fix3-268x300.png\" alt=\"\" width=\"268\" height=\"300\" srcset=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/11\/MikTeX_fix3-268x300.png 268w, https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/11\/MikTeX_fix3.png 411w\" sizes=\"auto, (max-width: 268px) 100vw, 268px\" \/><\/a><figcaption id=\"caption-attachment-519\" class=\"wp-caption-text\">The R  \/share\/texmf directory path is now in the list of MiKTeX directories.<\/figcaption><\/figure>\n<p>Now I am able to use Eclipse to invoke Sweave to automatically create a .tex file and convert it to a pdf.<\/p>\n<figure id=\"attachment_520\" aria-describedby=\"caption-attachment-520\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/11\/Sweave_final.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-520\" title=\"Sweave_final\" src=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/11\/Sweave_final-300x272.png\" alt=\"\" width=\"300\" height=\"272\" srcset=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/11\/Sweave_final-300x272.png 300w, https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/11\/Sweave_final.png 590w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-520\" class=\"wp-caption-text\">The final output of my test file. <\/figcaption><\/figure>\n<p>It&#8217;s entirely possible that pointing MiKTeK to that R \/share\/texmf\/ directory will come back to bite me in the future, but for now it appears to work. If I upgrade to a future version of R, it will be necessary to point MiKTeK at the new \/share\/texmf\/ directory so that a current version of Sweave.sty will be in use, in case changes are made to the functioning of Sweave.<\/p>\n<p>Yet another stumbling block I&#8217;ve run into is how R and Sweave deal with R\/S expressions within the body of the text, particularly expressions that contain a $ sign. For example, if I try<br \/>\nto return the mean value from a column of a data frame &#8216;df&#8217; in the text of my .Rnw file:<\/p>\n<p>blah blah blah <code>\\Sexpr{mean(df$column, na.rm = TRUE)} <\/code>blah blah blah.<\/p>\n<p>The TeX document will often freak out because it thinks the $ sign is supposed to signal the start of a chunk of math code. This can be fixed by adding an extra option to the Sweave call that I invoke through Eclipse\/StatEt. While you have a .Rnw file active in the text editor of Eclipse, go to the menu Document&gt;Create\/Edit Profiles (alternately, you can get there by going to the menu Run&gt;External Tools&gt;External Tool Configurations). In the window that opens, click on the Sweave processing profile you&#8217;ve created (see figure below). In the &#8220;Run command in active R Console:&#8221; box, change the code snippet to read:<br \/>\n<code>Sweave(file = \"${resource_name}\", syntax = \"SweaveSyntaxNoweb\")<\/code><\/p>\n<figure id=\"attachment_682\" aria-describedby=\"caption-attachment-682\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/11\/syntax.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-682\" title=\"syntax\" src=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/11\/syntax-300x158.png\" alt=\"\" width=\"300\" height=\"158\" srcset=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/11\/syntax-300x158.png 300w, https:\/\/lukemiller.org\/wp-content\/uploads\/2010\/11\/syntax.png 775w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-682\" class=\"wp-caption-text\">Add in the syntax = &quot;SweaveSyntaxNoweb&quot; option to streamline the interpretation of R code chunks in the text of your .Rnw document.<\/figcaption><\/figure>\n<p>Making that change should allow the whole R\/Sweave\/Eclipse\/MikTeX\/StatET rigamarole work correctly.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using Sweave to produce pretty-looking documentation for R is awfully handy. It takes a little tweaking to get set up in Eclipse and StatET though. I followed the information in Jeromy Anglim&#8217;s webpage to originally get Sweave set up. The following is my experience getting it working with Eclipse 3.5, StatET 0.9.1.b201011060900E35sw, Sweave Add-on for [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[218],"tags":[14,42,7,58,15,38],"class_list":["post-512","post","type-post","status-publish","format-standard","hentry","category-r-project","tag-eclipse","tag-latex","tag-r","tag-r-project","tag-statet","tag-sweave"],"_links":{"self":[{"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/posts\/512","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/comments?post=512"}],"version-history":[{"count":19,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/posts\/512\/revisions"}],"predecessor-version":[{"id":906,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/posts\/512\/revisions\/906"}],"wp:attachment":[{"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/media?parent=512"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/categories?post=512"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/tags?post=512"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}