Running your R and LaTeX Infrastructure from a portable USB Drive

On my road to eventually running all of my programs off an USB device I’ve gotten a little bit closer yesterday thanks to input from Duncan Murdoch and Yihui Xie.

Eventually running the entire Windows OS off a USB drive would be nice (not talking about running it as a Virtual Machine, but to have a bootable Windows on USB), so: any hints appreciated 😉

My current R devel infrastructure consists of

Thanks to everyone providing and maintaing those tools!

When I say ‘set up as portable’, that means I simply took the regular installer, but instead of installing to “C:\Program Files\”, I choose a directory on my USB device. This works (at least for me) surprisingly often. I guess the crucial part is if the respective application requires some MS Windows registry entry or not. If not, then you’re fine with running it in “portable mode”.

Kind of the most important aspect when running portable apps is that you need to turn absolute paths into relative paths for crucial system components.

Regarding Eclipse, R, MikTeX and LyX, this is not a problem in case you are fine with a few initial tweaks.

    • You need to change ‘<LYXDIR>/Ressource/lyxrc.dist’ in order to account for MikTeX’s portable nature. Here’s my last line of that file:
      \path_prefix “$LyXDir\bin;$LyXDir\python;$LyXDir\imagemagick;$LyXDir\ghostscript;$LyXDir\..\..\MiKTeX\2.9\miktex\bin”
    • You need to/should write some wrapper functions that give you the relative paths to portable R’s library and destdir folder (which you probably want to be on your USB device as well; per default R installs packages to your harddrive, thus they are “lost” if you switch computers)

Yet, I’ve struggled with Rtools quite a while because I didn’t know how to alter the Windows System PATH based on some script, preferably an R script. Duncan Murdoch sent me some information about the MS Windows registry key that needs to be altered and AFAIU that’s how it’s done when you run the Rools Installer on Windows and opt that you’re fine with the installer altering your path. However, I didn’t feel up to the task of tweaking the registry. In case anyone is interested, this is what Duncan told me:

“We use the Inno Setup installer; it has a function for this.  Here’s the code used:

Root: HKLM; Subkey: SYSTEM\CurrentControlSet\Control\Session Manager\Environment; ValueType: expandsz; ValueName: PATH; ValueData: “{code:getNewPath}”; Tasks: setPath ”

Now, yesterday while checking out the new knitr package from Yihui (very nice work by the way! And while where at it: props to Tal Galili for providing; great source of info, go check it out!), I came across his little R script add-R-path-win.R that does exactly what I was looking for. I adapted it a little to fit my needs. You can find it at my GitHub repo:

12 thoughts on “Running your R and LaTeX Infrastructure from a portable USB Drive

  1. You might be able to use Rgui.bat from which temporarily sets the path for you so you don’t have explicitly modify it. It looks for R in %R_HOME%\bin\%R_ARCH%\Rgui.exe, for Rtools in R_TOOLS, for MiKTeX in R_MIKTEX and looks in the registry and standard locations if these are not set. It also reads R_BATCHFILES_RC (or rbatchfilesrc.bat if not found or neither if neither found) which can be used to set these variables. There is also Rcmd.bat and other scripts.

  2. If you put this on GitHub, I will be happy to make some inline comments, since I see at least the first if statement can be improved in a neater way using match.arg(several.ok=TRUE); also there are “cats” running everywhere, and generally I hate “cats” in R code 🙂

    1. Sure, improvements are always welcome 🙂 I’m still a newbie regarding GitHub, but I’ll try to get this into my repo and get back to you on Monday.

      Just out of curiosity: if cats aren’t your thing, what do you use instead? In my code, cats usually only kick in if an arg like ‘do.verbose = TRUE’.

      1. cat() is often used by people for writing messages on screen, which is really really bad because it is hard (at least not natural) to suppress the messages from cats; message() should be the only correct choice if you want messages (extremely obvious, isn’t it?), and suppressMessages() can eat the messages from message(). I find it very annoying due to the same reason that several authors uses cat() to write package startup messages. In your case you should have used select.list(), or at least readline(), for the list of choices, so there is no need to use cat() anywhere.

        Forget about Facebook, and go, go GitHub! That is where a modern and social R programmer should be! :mrgreen: It might be a little bit difficult to get started, but you will be more and more efficient by making use of the social power. Neither R-Forge nor a personal blog is a good place to publish R code (I regret very much now).

      2. Nothing to regret, thanks for the pointer to message(). Must have missed it as so many people use cat(). Yeah, GitHub seems to be really cool, I’ll dive into it.

        A propos: installed the portable version of and tried changing my home directory in “/etc/profile” with “HOME = ‘r:/apps/git/some/directory'”. This works, but I need to provide an absolute path. Do you know of a way of changing that to a relative path? I’ve read about ‘SET HOME = “/some/directory”, but it didn’t work for me. Also tried that in the .bat file, but then everything just changed to drive “C:/”.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s