Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[write] add params argument to wb_add_data_table() #1126

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

JanMarvin
Copy link
Owner

@JanMarvin JanMarvin commented Sep 1, 2024

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 JanMarvin added the help wanted 🙏 Extra attention is needed label Sep 1, 2024
@JanMarvin
Copy link
Owner Author

This PR requires testing. Especially how it works in comparison with spreadsheet software. How is does this work compared to spreadsheet software and how does this work in comparison with other strange things thrown at it? Like custom filters.

autofilter_ref <- ref
}
xml_node_create(xml_name = "autoFilter", xml_attributes = c(ref = autofilter_ref))
}
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is autofilter if not TRUE? NULL? Something else?

@JanMarvin
Copy link
Owner Author

Maybe it is better to not use params but choose directly, but I'm not gonna change it right now

@JanMarvin JanMarvin force-pushed the custom_autofilter branch 2 times, most recently from 9328c79 to 461b815 Compare September 1, 2024 21:51
…o filter a table with `choose`. Similar to how it is done in `wb_add_pivot_table()`.
@JanMarvin
Copy link
Owner Author

In openxml it is possible to have at least two conditions per column filter. This is not covered by this PR and wont be included.

This

choose <- c(
  cyl = "x > 14 & x <= 20",
  am  = "x == 3 | x == 5"
)

should result in this

 <autoFilter ref="A1:K33">
  <filterColumn colId="0">
   <customFilters and="1">
    <customFilter operator="greaterThan" val="14" />
    <customFilter operator="lessThanOrEqual" val="20" />
   </customFilters>
  </filterColumn>
  <filterColumn colId="9">
   <filters>
    <filter val="3" />
    <filter val="5" />
   </filters>
  </filterColumn>
 </autoFilter>

Conditions with & are connected with and="1". Probably custom filter needs an operator "equal" for cases like x == 0 | x > 1. This is not implemented and would require improvements to create_conditions() and to reverse_conditions().

@JanMarvin JanMarvin added enhancement 😀 New feature or request question ❓ Further information is requested labels Sep 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement 😀 New feature or request help wanted 🙏 Extra attention is needed question ❓ Further information is requested
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant