Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

rstudio

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.

like image 809
Joshua Rosenberg Avatar asked Jan 12 '18 22:01

Joshua Rosenberg


People also ask

How do I view data in an R package?

Simply check the checkbox next to the package name to load the package and gain access to the datasets. You can also click on the package name and RStudio will open a help file describing the datasets in this package. and press ENTER.

How can you view the code used inside a function in R?

If you want to view the code built-in to the R interpreter, you will need to download/unpack the R sources; or you can view the sources online via the R Subversion repository or Winston Chang's github mirror. Uwe Ligges's R news article (PDF) (p. 43) is a good general reference of how to view the source code for .

How do I view a function in RStudio?

You can also access go to function definition: Using the F2 keyboard shortcut. Using Ctrl+Click with the mouse. From either the source editor or the console.


1 Answers

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.

like image 154
user2554330 Avatar answered Oct 21 '22 09:10

user2554330