openxlsx2 icon indicating copy to clipboard operation
openxlsx2 copied to clipboard

[write] add `params` argument to `wb_add_data_table()`

Open JanMarvin opened this issue 6 months ago • 3 comments

[!TIP] This pull request is not gonna get merged unless somebody tries it. It can cause surprising behavior and I'm not gonna gamble here. If you want it included. Give it a try. Create a test parkour to check openxlsx2 vs Excel behavior. Let me know what works and what not.

Which allows to filter a table with choose. Similar to how it is done in wb_add_pivot_table().

library(openxlsx2)

choose <- c(cyl = "x %in% c(4, 8)", am = c("x != 1"))
  
# create workbook
wb <- wb_workbook() %>%
  wb_add_worksheet() %>%
  wb_add_data_table(x = mtcars, params = list(choose = choose))
wb_to_df(wb, skip_hidden_rows = TRUE)
#>     mpg cyl  disp  hp drat    wt  qsec vs am gear carb
#> 6  18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
#> 8  14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
#> 9  24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
#> 10 22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
#> 13 16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3
#> 14 17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3
#> 15 15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3
#> 16 10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4
#> 17 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4
#> 18 14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4
#> 22 21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1
#> 23 15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2
#> 24 15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2
#> 25 13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4
#> 26 19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2

# write_xlsx
wb <- write_xlsx(x = mtcars, as_table = TRUE, params = list(choose = choose))
wb_to_df(wb, skip_hidden_rows = TRUE)
#>     mpg cyl  disp  hp drat    wt  qsec vs am gear carb
#> 6  18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
#> 8  14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
#> 9  24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
#> 10 22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
#> 13 16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3
#> 14 17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3
#> 15 15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3
#> 16 10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4
#> 17 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4
#> 18 14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4
#> 22 21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1
#> 23 15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2
#> 24 15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2
#> 25 13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4
#> 26 19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2



wb <- write_xlsx(
  x = list(mtcars, esoph),
  as_table = TRUE, 
  params = 
    list(
      choose = list(
        c(cyl = "x %in% c(4, 8)", am = c("x != 1")),
        c(agegp = "x >'25-34'")
      )
    )
)
wb_to_df(wb, skip_hidden_rows = TRUE)

JanMarvin avatar Sep 01 '24 17:09 JanMarvin