9

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.

9
  • Can you give some examples of what you would like to accomplish? Dummy code please. Jan 12, 2018 at 22:33
  • Sure, something like View(readr::read_lines(script_filename)) Jan 12, 2018 at 22:33
  • Here, I'm trying to make it easier to view a script which is automatically generated by a function (and then run). The idea is folks may want to peak under the hood easily. Jan 12, 2018 at 22:34
  • That's an interesting question of utils::View() vs View() I wouldn't know why, will have to see.... Jan 12, 2018 at 22:36
  • 1
    It may be that utils::View in Rstudio is different that that of base R. you should see some thing like <environment: namespace:utils> at the end of the print out when you type View in R console. As an aside, suggesting that the user use the View() function to view feels like over-optimization. Personally, I'd only go as far as to provide a print method and call it a day.
    – Jthorpe
    Jan 12, 2018 at 23:22

1 Answer 1

9

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.