10

How to open a local html file from R in an operating system independent way?

For demonstration purposes, assume that the file is called test.html and is in the working directory.

initial thoughts

  • system('gnome-open test.html')
    • This works on Ubuntu
  • browseURL(paste('file://', getwd(),'test.html', sep='/'))
    • This works on Ubuntu, but it feels like a bit of a hack and I'm not certain whether it would work on Windows.
Jeromy Anglim
  • 33,939
  • 30
  • 115
  • 173

3 Answers3

8

You might find my open.file.in.OS function useful, sources can be found here.

A short summary about what this function does:

  1. Check platform
  2. Based on platform, call:
    • shell.exec on Windows
    • open with system on Mac
    • and xdg-open with system on other Unix-like operating system
  3. Uses shQuote on the privided file

Update: See now the openFileInOS in the pander package.

library(pander)
openFileInOS("d:/del/dt/a.html")

References: this function is a forked version of David Hajage's convert function can be found here.

vasili111
  • 6,032
  • 10
  • 50
  • 80
daroczig
  • 28,004
  • 7
  • 90
  • 124
  • +1 That looks really cool. For my particular application, I'd like to minimise dependencies. Thus, I'd still prefer a built in function. – Jeromy Anglim Jun 10 '12 at 13:44
  • 2
    Well, you can just copy that function to your `.Rprofile` in your home dir without any additional dependencies - and have an OS independent function that can open up any file format in your OS. **Otherwise** just use something like `openHTML <- function(x) browseURL(paste0('file://', file.path(getwd(), x)))` – daroczig Jun 10 '12 at 13:51
  • 1
    Thanks, that's it. The combination of file.path with browseURL is probably what would meet my current needs most. – Jeromy Anglim Jun 10 '12 at 14:02
  • 1
    Hi @JeromyAnglim you can use the `openFileInOS` function of daroczig's `pander` package. – Stéphane Laurent Apr 11 '14 at 11:04
  • With pander package you can do like this: `library(htmlwidgets) openFileInOS("d:/del/dt/a.html")` – vasili111 May 14 '20 at 01:04
6

I just wanted to pull the answer given by @daroczig out of the comments and into an answer. If @darcozig wants to post this as a separate answer, I'll delete this copy.

openHTML <- function(x) browseURL(paste0('file://', file.path(getwd(), x)))
Jeromy Anglim
  • 33,939
  • 30
  • 115
  • 173
0

Use the file.path function to construct the file path.

 file.path(..., fsep = .Platform$file.sep)


 ...: character vectors.

fsep: the path separator to use.

By default it will use the current os path separator.

For example

> file.path ("", "home", "phoxis", "paragraph")
[1] "/home/phoxis/paragraph"

This generates my file "/home/phoxis/paragraph"

Note the blank string "" at the beginning. This forces to ad an extra "/" in my case to generate the absolute path. Adjust to generate absolute or relative path as per your need and have a look at ?file.path

I think this will fulfil your needs

phoxis
  • 60,131
  • 14
  • 81
  • 117
  • OP wants to run the appropriate application to launch the file, not just construct an OS-agnostic path to it. – Mark Reed Jun 10 '12 at 13:54