Skip to contents

Given x and f, this function prints f(x) before returning the original x. It is useful in a pipe, when one wants a to print the derivative of an object in the pipe but then return or assign the original object. A common use case is printing the `summary() of an estimated model but then assigning the original model (rather than the summary object) to a variable for further processing.

Usage

zprint(x, f = NULL, ...)

Arguments

x

An object, typically in a pipe.

f

A function to be applied to x before printing.

...

Other arguments to be passed to f.

Value

The original object x.

Examples

if (getRversion() >= "4.1.0" && require("dplyr")) {

  # Print summary before assigning model to variable
  m <- lm( speed ~ dist, cars) |>
    zprint(summary) # prints summary(x)
  m                 # m is the original model object

  # Print grouped data before filtering original
  cw_subset <- chickwts |>
    zprint(count, feed, sort=TRUE) |> # prints counts by feed
    filter(feed=="soybean")
  cw_subset # cw_subset is ungrouped, but filtered by feed
}
#> 
#> Call:
#> lm(formula = speed ~ dist, data = cars)
#> 
#> Residuals:
#>     Min      1Q  Median      3Q     Max 
#> -7.5293 -2.1550  0.3615  2.4377  6.4179 
#> 
#> Coefficients:
#>             Estimate Std. Error t value Pr(>|t|)    
#> (Intercept)  8.28391    0.87438   9.474 1.44e-12 ***
#> dist         0.16557    0.01749   9.464 1.49e-12 ***
#> ---
#> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#> 
#> Residual standard error: 3.156 on 48 degrees of freedom
#> Multiple R-squared:  0.6511,	Adjusted R-squared:  0.6438 
#> F-statistic: 89.57 on 1 and 48 DF,  p-value: 1.49e-12
#> 
#>        feed  n
#> 1   soybean 14
#> 2    casein 12
#> 3   linseed 12
#> 4 sunflower 12
#> 5  meatmeal 11
#> 6 horsebean 10
#>    weight    feed
#> 1     243 soybean
#> 2     230 soybean
#> 3     248 soybean
#> 4     327 soybean
#> 5     329 soybean
#> 6     250 soybean
#> 7     193 soybean
#> 8     271 soybean
#> 9     316 soybean
#> 10    267 soybean
#> 11    199 soybean
#> 12    171 soybean
#> 13    158 soybean
#> 14    248 soybean