Joshua Rosenberg
Joshua Rosenberg

Reputation: 4226

How to use R Studio View() function programatically / in a package

I am trying to use the R Studio View() function programatically / in a package.

When I use utils::View(), a different viewer than the R Studio viewer (it appears to be the one built-in to R) is used, but if I use View() (without specifying where the function is exported from), issues come up during R CMD CHECK.

I checked the R Studio cheatsheet, but this did not show whether / from where the R Studio View() is exported.

Upvotes: 10

Views: 2785

Answers (1)

user2554330
user2554330

Reputation: 44877

RStudio replaces the utils::View function with their own function when it starts up. Their source is

function (...) 
.rs.callAs(name, hook, original, ...)
<environment: 0x1036a6dc0>

You can't just copy this into your package, because it depends on things in that environment, and there's no way for your package to get it.

However, you can do this:

myView <- function(x, title)
  get("View", envir = as.environment("package:utils"))(x, title)

and export myView from your package. If you run this in RStudio, you'll get their function, if you run it anywhere else, you'll get the regular one.

Upvotes: 11

Related Questions