0

I havea tibble which looks like

library(tidyverse)

d <- tibble(
  y1 = 10:12,
  y2 = 3:5,
  n1 = rep(100, 3),
  n2 = rep(100, 3)
)

and a function which operates on each of these four columns

f <- function(y1, n1, y2, n2){
  log(y1/n2) - log(y2/n2)
}

I want to use f in another function g, which calls f like

g <- function(data, y1, n1, y2, n2){
  
  
  list(
    result_of_f = f(y1, n1, y2, n2)
  )
}


d %>% 
  g(y1, n1, y2, n2)

## Expected outout
##$result_of_f
## [1] 1.2039728 1.0116009 0.8754687

This code does not run as it is written, it needs tidy evaluation to work in the way I want it to. However, I'm a bit confused as to which tidy evaluation to use. In g, the computation of result_of_f needs a data context (something like with(data, f(y1, n1, y2, n2)). How can I use tidy evaluation to get my expected output?

EDIT: The list I've presented in g is to be used in downstream computations, it will not be the final output. I've simply asked for this output in this minimal working example to make it minimal.

Demetri Pananos
  • 6,770
  • 9
  • 42
  • 73

2 Answers2

0

The exposition operator (%$%) may be useful in this case

library(magrittr)
d %$%
  g(, y1, n2, y2, n2)
$result_of_f
[1] 1.2039728 1.0116009 0.8754687
akrun
  • 874,273
  • 37
  • 540
  • 662
0

You can use the base function with().

with(d, g(y1, n1, y2, n2))
Lionel Henry
  • 6,652
  • 27
  • 33