0

I am trying to hide some columns of a tibble (that's created using a function) from appearing in the console window in R studio. I'm not sure if this is even possible!?

Similar questions have been asked here and here. However, these examples take a data frame and output the final edited version of it to the Viewer window. I don't want to display my data frame in the Viewer window.

Below is a toy example of what Im trying to do.

In my code, I have 2 functions. The first function takes some data and does some manipulation (including adding some new helper columns to the data) and outputs a tibble. I then pass that tibble to the second function which does more data manipulation by using the helper column. Im trying to hide the helper columns that are created in the 1st function from the user (by not displaying them in the console window). But still allow them to be used by the 2nd function.

For example:

library(dplyr)

# create some data:
data <- tibble(
  x = LETTERS[1:10],
  y = c(10:1),
  z = runif(10)
)

# first function adds new columns:
testFun1 <- function(data){
  
    data <- transform(data, label = ifelse(y %% 2 == 0 , y, paste(x, round(z,2), sep = " or ")))
    data <- data %>%
      mutate(helper = 1:n())
    
    return(data)
}

# second function uses the helper column to manipulate the data some more:
testFun2 <- function(data){
  data <- data %>% 
    filter(helper %% 2 == 0)
  return(data)
}


newData <- testFun1(data)
finalData <- testFun2(newData)

What Im trying to do is hide the helper column from the output of the 1st function testfun1. I tried experimenting with using print but even if I add a print statement to testFun1, the object newData (created from testFun1) will still contain the column that I want to hide from the user.

Is what I want to achieve even possible?

ASIDE: in this toy example, I realise that all the manipulation could be achieved in one function. but in my actual code, I need to have 2 separate functions.

Electrino
  • 2,636
  • 3
  • 18
  • 40

1 Answers1

1

It depends on exactly what you mean by "hiding". You won't easily be able to have it there but completely inaccessible to the user. However, it's easy to hide it when printing, by adding a new class to it and defining a print method for that class.

For example:

hideHelper <- function (df) {
  class(df) <- c("hideHelper", class(df))
  df
}

print.hideHelper <- function(x, ...) {
  x$helper <- NULL
  NextMethod(x, ...)
}

# create some data with a column named "helper":
library(tibble)
data <- tibble(
  x = LETTERS[1:10],
  y = c(10:1),
  z = runif(10),
  helper = 1:10
)

# Tell R not to print that column
data <- hideHelper(data)
data
#> # A tibble: 10 × 3
#>    x         y      z
#>    <chr> <int>  <dbl>
#>  1 A        10 0.844 
#>  2 B         9 0.150 
#>  3 C         8 0.986 
#>  4 D         7 0.581 
#>  5 E         6 0.774 
#>  6 F         5 0.333 
#>  7 G         4 0.787 
#>  8 H         3 0.967 
#>  9 I         2 0.693 
#> 10 J         1 0.0949

# But it's still there:
names(data)
#> [1] "x"      "y"      "z"      "helper"
data$helper
#>  [1]  1  2  3  4  5  6  7  8  9 10

Created on 2022-01-30 by the reprex package (v2.0.1.9000)

Originally I said you won't be able to do it at all, but that's not true. R has several opaque object types. For example, you could put the columns you don't want the user to see into objects allocated in compiled C or C++ code and accessed with "external pointers", and the user would only be able to see whatever aspects of them you wrote code to allow them to see.

user2554330
  • 37,248
  • 4
  • 43
  • 90