Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use non-default browser?

Tags:

r

I have an R package that triggers Oauth2 flow in the browser (via httr). A user is in the odd situation that their IT department requires a specific system default browser (in this case, it's IE + Windows). But the user needs to do the OAuth in a different browser.

I am aware of the R_BROWSER environment variable and would like to temporarily set it to the browser of need. (And then hope that will be honoured in the OAuth flow...)

I am fiddling with this on a Mac and my default browser is Safari. My usual situation wrt R_BROWSER is this

Sys.getenv("R_BROWSER")
#> [1] "/usr/bin/open"

I know how to force the use of, e.g., Chrome from the shell or, from R, with a system() call:

system("/usr/bin/open -a '/Applications/Google Chrome.app' 'http://slate.com'")

But how do I transfer that knowledge to an appropriate setting for R_BROWSER? This does not work:

Sys.setenv("R_BROWSER" = "/usr/bin/open -a '/Applications/Google Chrome.app'")

When I subsequently browseURL(), the usual Safari default gets used. I suspect the answer differs by OS. For Mac OS, it seems desirable to avoid using open and somehow specify the browser directly.

like image 334
jennybryan Avatar asked Jun 24 '16 05:06

jennybryan


1 Answers

First, you should also save the previous values of both browser option and R_BROWSER so that you can restore the previous state of the session:

old_R_BROWSER <- Sys.getenv("R_BROWSER")
old_browser <- options()$browser

Then you can achieve desired behaviour by re-running the command @Hack-R posted after setting R_BROWSER.

Sys.setenv("R_BROWSER" = "/usr/bin/open -a '/Applications/Safari.app'")
options(browser = as.vector(Sys.getenv("R_BROWSER")))
browseURL("http://www.google.com") # opens in Safari, though my default is Chrome

(You can also just directly set options(browser = "/usr/bin/open -a '/Applications/Safari.app'") and browseURL works.)

Finally, restore the system state

Sys.setenv("R_BROWSER" = old_R_BROWSER)
options(browser = old_browser)
like image 86
jaimedash Avatar answered Oct 18 '22 03:10

jaimedash