The pipe_cat() function allows messages to be output to the console or to an external file without the need to break out of a pipeline to do so.

pipe_cat(
  data,
  ...,
  file = "",
  sep = " ",
  fill = FALSE,
  labels = NULL,
  append = FALSE
)

Arguments

data

the data being passed through the pipeline

...

arguments to be passed to the cat() function. Arguments will be evaluated in the context of data before being passed to cat()

file, sep, fill, labels, append

See the cat() documentation for more information regarding these arguments.

Examples

sample(100, 1) %>% runif() %>% pipe_cat("Current length: ", length(.), "\n") %>% pipe_cat("Current average: ", mean(.), "\n") %>% pipe_cat("Current standard error: ", sd(.) / length(.), "\n") %>% pipe_cat("Returning mean:\n") %>% mean()
#> Current length: 87 #> Current average: 0.4873048 #> Current standard error: 0.003279136 #> Returning mean:
#> [1] 0.4873048
tibble::tibble( x = runif(10), y = runif(10) ) %>% pipe_cat("Average x: ", mean(x), "\n") %>% pipe_cat("Current number of rows: ", nrow, "\n") %>% dplyr::mutate(z = x + y)
#> Average x: 0.4680811 #> Current number of rows: 10
#> # A tibble: 10 x 3 #> x y z #> <dbl> <dbl> <dbl> #> 1 0.927 0.928 1.86 #> 2 0.894 0.0266 0.921 #> 3 0.204 0.559 0.763 #> 4 0.257 0.854 1.11 #> 5 0.614 0.0759 0.690 #> 6 0.441 0.206 0.647 #> 7 0.316 0.496 0.811 #> 8 0.101 0.262 0.363 #> 9 0.273 0.416 0.689 #> 10 0.654 0.439 1.09
palmerpenguins::penguins %>% dplyr::mutate(species = as.character(species)) %>% dplyr::filter(!is.na(bill_length_mm)) %>% pipe_cat("Total average Culmen Length: ", mean(bill_length_mm), "\n") %>% dplyr::group_by(species) %>% pipe_cat(species, " average Culmen Length: ", mean(bill_length_mm), "\n")
#> Total average Culmen Length: 43.92193 #> Adelie average Culmen Length: 38.79139 #> Chinstrap average Culmen Length: 48.83382 #> Gentoo average Culmen Length: 47.50488
#> # A tibble: 342 x 8 #> # Groups: species [3] #> species island bill_length_mm bill_depth_mm flipper_length_… body_mass_g #> <chr> <fct> <dbl> <dbl> <int> <int> #> 1 Adelie Torge… 39.1 18.7 181 3750 #> 2 Adelie Torge… 39.5 17.4 186 3800 #> 3 Adelie Torge… 40.3 18 195 3250 #> 4 Adelie Torge… 36.7 19.3 193 3450 #> 5 Adelie Torge… 39.3 20.6 190 3650 #> 6 Adelie Torge… 38.9 17.8 181 3625 #> 7 Adelie Torge… 39.2 19.6 195 4675 #> 8 Adelie Torge… 34.1 18.1 193 3475 #> 9 Adelie Torge… 42 20.2 190 4250 #> 10 Adelie Torge… 37.8 17.1 186 3300 #> # … with 332 more rows, and 2 more variables: sex <fct>, year <int>