Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access/copy the View function?

Tags:

r

rstudio

I'm currently working on an add in for RStudio which opens an "enchanced" view of a data frame in Shiny, allows you to filter and select columns, then passes that code to the command line wrapped in View().

One feature which was suggested to me was to replace the default View in RStudio when the package is loaded to always load my new viewer.

I wanted this, but also to preserve the ability to use RStudio's View if you wanted. So I tried code like this:

View <- function(data_in, replace = T){
    if (replace){
        print('example')
    }else{
        utils::View(data_in)
}

This does not work, however, as utils::View is not the same as the View in RStudio. However, a search for ?View only gets the utils version. I'm assuming RStudio is overwriting View when loading, but I have no idea how to access it. I'd be happy to copy the function into something called save_view (or similar) but don't know how to access it! Entering View into the command line gives me the following code

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

But copying that to a new function will just give me something that errors (I'm wondering if it's something to do with the environment the function exists on)

like image 679
Kieran Martin Avatar asked Oct 26 '17 11:10

Kieran Martin


2 Answers

RStudio replaces the internal View function with the one you saw. You can get it using

RStudioView <- as.environment("package:utils")$View

If you call that one, it should do what RStudio does.

You get the original one using utils::View.

like image 102
user2554330 Avatar answered Nov 01 '22 19:11

user2554330


This seems to be the source of the View() (from utils):

function (x, title) 
{
  check <- Sys.getenv("_R_CHECK_SCREEN_DEVICE_", "")
  msg <- "View() should not be used in examples etc"
  if (identical(check, "stop")) 
    stop(msg, domain = NA)
  else if (identical(check, "warn")) 
    warning(msg, immediate. = TRUE, noBreaks. = TRUE, domain = NA)
  if (missing(title)) 
    title <- paste("Data:", deparse(substitute(x))[1])
  as.num.or.char <- function(x) {
    if (is.character(x)) 
      x
    else if (is.numeric(x)) {
      storage.mode(x) <- "double"
      x
    }
    else as.character(x)
  }
  x0 <- as.data.frame(x)
  x <- as.list(format.data.frame(x0))
  rn <- row.names(x0)
  if (any(rn != seq_along(rn))) 
    x <- c(list(row.names = rn), x)
  if (!is.list(x) || !length(x) || !all(sapply(x, is.atomic)) || 
    !max(lengths(x))) 
    stop("invalid 'x' argument")
  if (grepl("darwin", R.version$os)) 
    check_for_XQuartz()
  invisible(.External2(C_dataviewer, x, title))
}

And very clearly it's calling the C_dataviwer and this is the dataviewer https://support.rstudio.com/hc/en-us/articles/205175388-Using-the-Data-Viewer#starting-the-viewer

Edit:

Here's the actual code of dataviewer https://github.com/rstudio/rstudio/blob/5719361179d1020dc3157c4e24b21bcd17c483e6/src/cpp/session/modules/data/DataViewer.cpp

like image 30
amrrs Avatar answered Nov 01 '22 20:11

amrrs