You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#' This function generates a file that can be imported into MS Word in order to use a macro for autofitting all tables in a document at once. This macro file should be imported only once per computer.
862
+
#' Autofitting using existing tools in flextable should be enough for most cases. For the others, here is a VBA macro which autofits all tables from inside MS Word.
863
+
#' This function generates a file that can be imported into MS Word in order to use this macro. The macro file should be imported only once per computer.
863
864
#'
864
865
#' @section Installation:
865
866
#' * In the `R` console, run `generate_autofit_macro()` to generate the file `crosstable_autofit.bas` in your working directory.
Copy file name to clipboardExpand all lines: vignettes/crosstable-report.Rmd
+20-67Lines changed: 20 additions & 67 deletions
Original file line number
Diff line number
Diff line change
@@ -32,11 +32,13 @@ There are two cases to consider:
32
32
* You have a lot of tables and little text? You probably should use [officer](#create-reports-with-officer).
33
33
* You have a lot of text and a few tables? You probably should use [Rmarkdown](#create-reports-with-rmarkdown).
34
34
35
+
As I only find myself in the first case, this vignette will only talk about officer.
36
+
35
37
## Create reports with `officer`
36
38
37
39
The real power of `crosstable` comes out when used with David Gohel's awesome package [`officer`](https://davidgohel.github.io/officer/), which allows to automatically create MS Word documents.
38
40
39
-
For instance, let's try to create a document like this:
41
+
For the example, let's try to create a document like this:
40
42

41
43
42
44
You can also download this example [here](https://github.com/DanChaltiel/crosstable/raw/main/examples/vignette_officer.docx).
For demonstration purposes, I tried to cover as many features as possible, so it contains multiple syntaxes for the same result. Of course, you should use whatever syntax you are most comfortable with.
94
96
95
-
You can find other useful functions in the [references](https://danchaltiel.github.io/crosstable/reference/index.html#section-officer-helpers).
While you are still working on your code, you might want to omit the name so that you open the docx file in a temporary file for a quick peek (`write_and_open(doc)`). This will prevent the errors that happen when the file is already open.
114
114
115
-
116
115
You can also use `print(doc, "vignette_officer.docx")` if you don't want the file to open right away.
117
116
118
117
### Functions
@@ -121,38 +120,20 @@ Here is a brief description of the functions used in this example:
121
120
122
121
+`officer::read_docx()`: creates a bare MS Word document
123
122
+`body_add_title()`: adds a title paragraph of any level
124
-
+`body_add_normal()`: adds a normal style paragraph. You can also incorporate variables using the syntax `{nrow(ct3)}` and references using the syntax `\\@ref(my_bookmark)`.
123
+
+`body_add_normal()`: adds a normal style paragraph. You can also incorporate variables using the syntax `{nrow(ct3)}` and references using the syntax `\\@ref(my_bookmark)`. It also support Markdown syntax for bold, italics, colored text... See `help(body_add_normal)` to see the details.
125
124
+`body_add_crosstable()`: adds a crosstable
126
125
+`body_add_figure_legend()` and `body_add_table_legend()`: adds a figure/table legend. The `bookmark` is the key that can be added elsewhere in `body_add_normal()`.
127
126
+`body_add_gg2()`: adds a ggplot. Unlike `officer::body_add_gg()`, you can change the unit using the `units` argument or the options `options(crosstable_units="cm")`.
128
127
129
-
Browse https://davidgohel.github.io/officer/ for more insight about how you can use `{officer}`.
130
-
131
-
132
-
### Autofit macro for large tables
133
-
134
-
This is great, but large tables will unfortunately overflow your document.
135
-
136
-
This is a ([known](https://ardata-fr.github.io/officeverse/faq.html#update-fields)) limitation that cannot be fixed using `R`.
137
-
138
-
You would have to use MS Word autofit tools on each table one by one (`Table Tools > Layout > AutoFit > AutoFit Window`), which can be really tedious.
139
-
140
-
But fear not! You can use a MS Word macro to do the job for you. Here is how:
128
+
`{crosstable}` comes with many `officer`-like functions to help you create your report, see the full list in the [references](https://danchaltiel.github.io/crosstable/reference/index.html#officer-helpers).
141
129
142
-
* In the R console, run `generate_autofit_macro()` to generate the file `crosstable_autofit.bas` in your working directory.
143
-
144
-
* In MS Word, press <kbd>Alt</kbd>+<kbd>F11</kbd> to open the VB Editor.
145
-
146
-
* In the Editor, go to `File` > `Import` or press `Ctrl+M` to open the import dialog, and import `crosstable_autofit.bas`. There should now be a "CrosstableMacros" module in the "Normal" project.
147
-
* Run the macro, either from the VB Editor or from `View` > `Macros` > `View Macros` > `Run`.
148
-
149
-
This process will make the macro accessible from any Word file on this computer. Note that, in the Editor, you can also drag the module to your document project to make the macro accessible only from this file. The file will have to be named with the `docm` extension though.
130
+
Also, browse https://davidgohel.github.io/officer/ for more insight about how you can use `{officer}`.
150
131
151
132
### Styles
152
133
153
-
Crosstables uses [Word styles](https://support.microsoft.com/fr-fr/office/personnaliser-ou-cr%C3%A9er-des-styles-d38d6e47-f6fc-48eb-a607-1eb120dec563)to operate at full power .
Here, I used the default template of `officer::read_docx()` that comes with default styles. In your own custom template, you can edit all styles (for instance you can make "Normal" have a bold font of size 8) and add your own.
136
+
Here, I used the default template of `officer::read_docx()` that comes with default styles. In your own custom template, you can edit all styles (for instance you can make "Normal" have a bold font of size 8) and add your own. See [the official documentation](https://ardata-fr.github.io/officeverse/office-documents-generation.html#templates) on how to use templates.
156
137
157
138
The best example here is `body_add_list()`, which is supposed to add a bullet list. Unfortunately, the default template does not come with list styles so you will have to add one to your custom template before using it:
158
139
@@ -173,50 +154,22 @@ Note that you might sometimes encounter the error "Error: could not match any st
173
154
174
155
### Post-production for table/figure legends
175
156
176
-
Depending on your version of `{officer}`, Word will ask you to update the fields
177
-
178
-
+ During the opening of the document, MS Word might ask you to "update the fields", to which you should answer "Yes".
179
-
If it does not ask or if you answer "No", the legends added with `body_add_table_legend()` or `body_add_figure_legend()` might have no actual numbers displayed.
180
-
In this case, you have to manually update the references inside MS Word: select all (\kbd{Ctrl}+\kbd{A}), then update (\kbd{F9}), sometimes twice. You might even need to do this several times. See `?body_add_legend` for more insight.
181
-
182
-
+ Be aware that you unfortunately cannot reference a bookmark more than once using this method. Writing:
183
-
`body_add_normal("Table \\@ref(iris_col1) is about flowers. I like this Table \\@ref(iris_col1).")` will prevent **all** the numbering from applying.
157
+
If you added some legends or TOC titles, MS Word might ask you to "update the fields", to which you should answer "Yes".
158
+
This will trigger the automatic numbering of tables and references.
184
159
160
+
### Autofit macro for large tables
185
161
162
+
Auto-fitting from outside MS Word has its limits, and large tables might still overflow your document.
186
163
164
+
If so, you can use MS Word inner autofit tools on each table one by one (`Table Tools > Layout > AutoFit > AutoFit Window`), which can be really tedious.
187
165
188
-
## Create reports with `Rmarkdown`
189
-
190
-
<!-- Integration Rmd in Rmd: la coloration syntaxique RStudio est mauvaise mais le code est bon ! -->
Knitting (`knitr::knit()` or via **RStudio**) this `Rmd` code also creates a MS-Word file. Here, you can use the power of `bookdown` to generate the automatic numbering of the tables.
193
-
194
-
195
-
````markdown
196
-
197
-
---
198
-
title: "Iris"
199
-
output: bookdown::word_document2
200
-
---
201
-
202
-
`r ''````{r setup, include=FALSE}
203
-
library(crosstable)
204
-
library(flextable)
205
-
```
206
-
207
-
Table iris is given in Table \@ref(tab:irisTable).
crosstable(iris, Sepal.Length, Sepal.Width, by=Species, test = TRUE, total="column") %>% as_flextable
212
-
```
213
-
214
-
````
166
+
But fear not! You can also use a MS Word macro to do the job for you. Here is how:
215
167
216
-
You can example files here: [vignette_markdown.Rmd](../examples/vignette_markdown.Rmd) and [vignette_markdown.docx](../examples/vignette_markdown.docx).
168
+
* In the R console, run `generate_autofit_macro()` to generate the file `crosstable_autofit.bas` in your working directory.
217
169
218
-
I have to admit that I don't use Rmarkdown a lot with `crosstable`, so this feature might not be as maintained as others.
170
+
* In MS Word, press <kbd>Alt</kbd>+<kbd>F11</kbd> to open the VB Editor.
219
171
220
-
```{r, include = FALSE}
221
-
options(old)
222
-
```
172
+
* In the Editor, go to `File` > `Import` or press `Ctrl+M` to open the import dialog, and import `crosstable_autofit.bas`. There should now be a "CrosstableMacros" module in the "Normal" project.
173
+
* Run the macro, either from the VB Editor or from `View` > `Macros` > `View Macros` > `Run`.
174
+
175
+
This process will make the macro accessible from any Word file on this computer. Note that, in the Editor, you can also drag the module to your document project to make the macro accessible only from this file. The file will have to be named with the `docm` extension though.
0 commit comments