Public-Health-Scotland / source-linkage-files

This repo is for the syntax used for the PHS Source Linkage File project
https://public-health-scotland.github.io/source-linkage-files/
Other
4 stars 2 forks source link

Bug: warning type does not work properly as expected in data pipeline magrittr `%>%` #1010

Open lizihao-anu opened 5 days ago

lizihao-anu commented 5 days ago

Warning type fucntions include base::warning() and package cli, like cli::cli_alert_info().

lizihao-anu commented 5 days ago

There are three examples to demonstrate how cli::cli_alert_info() works with magrittr %>%. It is also the same case with other warning type functions.

Code

Example 1 Example 2 Example 3
```R library(dplyr) fn_a = function(data){ data+2 cli::cli_alert_info("A {Sys.time()}") Sys.sleep(1) return(data) } fn_b = function(data){ data-1 cli::cli_alert_info("B {Sys.time()}") Sys.sleep(5) return(data) } fn_c = function(data){ data*2 cli::cli_alert_info("C {Sys.time()}") Sys.sleep(5) return(data) } data = data.frame( x=1, y=1 ) data |> fn_a() |> fn_b() |> fn_c() # Output > data |> + fn_a() |> + fn_b() |> + fn_c() ℹ A 2024-09-25 16:44:49 ℹ B 2024-09-25 16:44:50 ℹ C 2024-09-25 16:44:55 x y 1 1 1 ``` ```R library(dplyr) fn_a = function(data){ cli::cli_alert_info("A {Sys.time()}") data = data+2 Sys.sleep(5) return(data) } fn_b = function(data){ cli::cli_alert_info("B {Sys.time()}") data = data-1 Sys.sleep(5) return(data) } fn_c = function(data){ cli::cli_alert_info("C {Sys.time()}") data = data*2 Sys.sleep(5) return(data) } data = data.frame( x=1, y=1 ) data |> fn_a() |> fn_b() |> fn_c() # Output > data |> + fn_a() |> + fn_b() |> + fn_c() ℹ C 2024-09-25 16:45:47 ℹ B 2024-09-25 16:45:47 ℹ A 2024-09-25 16:45:47 x y 1 4 4 ``` ```R library(dplyr) fn_a = function(data){ 1 cli::cli_alert_info("A {Sys.time()}") Sys.sleep(1) return(data+2) } fn_b = function(data){ 1 cli::cli_alert_info("B {Sys.time()}") Sys.sleep(5) return(data-1) } fn_c = function(data){ 1 cli::cli_alert_info("C {Sys.time()}") Sys.sleep(5) return(data*2) } data = data.frame( x=1, y=1 ) data |> fn_a() |> fn_b() |> fn_c() # Output > data |> + fn_a() |> + fn_b() |> + fn_c() ℹ C 2024-09-25 16:46:28 ℹ B 2024-09-25 16:46:33 ℹ A 2024-09-25 16:46:38 x y 1 4 4 ```

From the trials above, we may say if we want cli::cli_alert_info() to work as we expect, we need to "activate" the variable we want first.

lizihao-anu commented 4 days ago

Or, instead, we can put cli_alert_info() to the end before return.