0

I want to make a demo tool to load a file up for R users that is independent of OS. On a Windows system I use shell.exec but know this isn't the same for mac and Linux. I believe they use system but have no idea how as I never have had the need to use anything but shell.exec. Here's the function thus far:

open_file <- function(file.name) {
    if (Sys.info()["sysname"] == "Windows") {
        shell.exec(file.name)
    } else {
#insert linux and mac equiv here (I think they're the same)                                   
    }
}  

What could I put into the inset Linux and Mac OS X... part to make it work on these machines as well?

EDIT: in my function shell.exec opens a file that happens to be docx and it uses MS Word, but I'm wanting this to be generic to open txt csv xlsx files as well.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
  • That's not a question yet. What is shell.exec supposed to do? Launch an editor? Fetch a cold one from the fridge? – Dirk Eddelbuettel Sep 05 '12 at 01:26
  • 3
    http://svn.r-project.org/R/trunk/src/library/base/man/windows/shell.exec.Rd ... maybe the `open` command works on MacOS? (I think `system(paste("open",filename))` works.) See also http://stackoverflow.com/questions/264395/linux-equivalent-of-the-mac-os-x-open-command – Ben Bolker Sep 05 '12 at 01:36
  • @Thanks for the feedback. Sorry in my mind it's clear because I think shell.exec behaviors in a very specific and easy way and I'm getting the feeling this is not the case on mac/linux. Please see my edits above. – Tyler Rinker Sep 05 '12 at 01:37
  • Voting to close. Tyler is confused, and knows too little about cross-platform programming. "Hope alone" is not a strategy. – Dirk Eddelbuettel Sep 05 '12 at 01:40
  • 2
    Don't agree. It might be off-topic as an R question, but I think it could be done via `open` on MacOS and `xdg-open` on Linux ... – Ben Bolker Sep 05 '12 at 01:41
  • @Dirk I can respect that position but how am I going to learn. At least point me in the right direction. – Tyler Rinker Sep 05 '12 at 01:41
  • MSWord and linux would be strange bedfellows. Perhaps markdown (text / csv) or html would be easier to make system and program independent. – mnel Sep 05 '12 at 01:46
  • 2
    @TylerRinker You simply _cannot assume_ every user on every OS has software installed to open docx file. Limit yourself to Windows and be done. If this was a slam-dunk, R and other apps would already do it. – Dirk Eddelbuettel Sep 05 '12 at 01:48
  • @TylerRinker The closest Linux equivalent is `xdg-open`, and it looks like for Mac, it's just `open`, see [here](http://superuser.com/q/38984/125800) – Marius Sep 05 '12 at 01:55
  • I agree MSword and linux are strange bed fellows but there's open office etc. The function was more for demo purposes and truthfully my audiences would not be Linux users. I'd be happy with a mac solution. – Tyler Rinker Sep 05 '12 at 01:57
  • @Dirk this audience are my university people so I know that a mac user has free access to MS Office software. – Tyler Rinker Sep 05 '12 at 02:02

1 Answers1

2

Note that shell.exec() is a function available in the Windows version of R, but not in the MAC version. You can use the following code to get the functionality you want:

shell.exec  <- function(x)
{
      # replacement for shell.exe (doesn't exist on MAC)
      if (exists("shell.exec",where = "package:base"))
            return(base::shell.exec(x))
      comm <- paste("open",x)
      return(system(comm))
}

`

The nice thing about this is approach is that it is transparent - code that worked on a Windows machine will continue to work, and now will also run on a Mac machine, as long as the filetype is something that the OS knows how to open. It should be simple to get this to work on Unix as well.

jafelds
  • 894
  • 8
  • 12
  • I forgot I asked this but essentially this works (on files too not just directories as the answer was intended for): http://stackoverflow.com/a/12135823/1000343 – Tyler Rinker Jan 27 '16 at 17:46