Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

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.
like image 862
Jeromy Anglim Avatar asked Jun 10 '12 13:06

Jeromy Anglim


2 Answers

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.

like image 130
daroczig Avatar answered Sep 25 '22 16:09

daroczig


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)))
like image 43
Jeromy Anglim Avatar answered Sep 23 '22 16:09

Jeromy Anglim