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.