Jeromy Anglim
Jeromy Anglim

Reputation: 34937

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

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

Upvotes: 10

Views: 12458

Answers (3)

daroczig
daroczig

Reputation: 28642

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.

Upvotes: 8

Jeromy Anglim
Jeromy Anglim

Reputation: 34937

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)))

Upvotes: 8

phoxis
phoxis

Reputation: 61940

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

Upvotes: 0

Related Questions