Skip to content

Commit dd78ae9

Browse files
committed
update the reporting documentation
#57
1 parent 89dbfa5 commit dd78ae9

File tree

5 files changed

+25
-70
lines changed

5 files changed

+25
-70
lines changed

R/officer.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,8 @@ write_and_open = function(doc, docx.file){
859859
# nocov start
860860
#' Generate a macro file for autofitting
861861
#'
862-
#' 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.
863864
#'
864865
#' @section Installation:
865866
#' * In the `R` console, run `generate_autofit_macro()` to generate the file `crosstable_autofit.bas` in your working directory.
@@ -873,7 +874,7 @@ write_and_open = function(doc, docx.file){
873874
#' @author Dan Chaltiel
874875
#' @export
875876
generate_autofit_macro = function(){
876-
fileConn<-file("crosstable_autofit.bas")
877+
fileConn = file("crosstable_autofit.bas")
877878
writeLines(c(
878879
'Attribute VB_Name = "CrosstableMacros"',
879880
'Sub CrosstableAutofitAll()',

examples/vignette_officer.docx

482 Bytes
Binary file not shown.

man/figures/vignette_officer.docx

482 Bytes
Binary file not shown.

man/generate_autofit_macro.Rd

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vignettes/crosstable-report.Rmd

Lines changed: 20 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@ There are two cases to consider:
3232
* You have a lot of tables and little text? You probably should use [officer](#create-reports-with-officer).
3333
* You have a lot of text and a few tables? You probably should use [Rmarkdown](#create-reports-with-rmarkdown).
3434

35+
As I only find myself in the first case, this vignette will only talk about officer.
36+
3537
## Create reports with `officer`
3638

3739
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.
3840

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:
4042
![officer example](officer-example.png)
4143

4244
You can also download this example [here](https://github.com/DanChaltiel/crosstable/raw/main/examples/vignette_officer.docx).
@@ -92,8 +94,6 @@ doc = read_docx() %>% #default template
9294

9395
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.
9496

95-
You can find other useful functions in the [references](https://danchaltiel.github.io/crosstable/reference/index.html#section-officer-helpers).
96-
9797
### Output
9898

9999
```{r save, include=FALSE}
@@ -112,7 +112,6 @@ write_and_open(doc, "vignette_officer.docx")
112112

113113
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.
114114

115-
116115
You can also use `print(doc, "vignette_officer.docx")` if you don't want the file to open right away.
117116

118117
### Functions
@@ -121,38 +120,20 @@ Here is a brief description of the functions used in this example:
121120

122121
+ `officer::read_docx()`: creates a bare MS Word document
123122
+ `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.
125124
+ `body_add_crosstable()`: adds a crosstable
126125
+ `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()`.
127126
+ `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")`.
128127

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).
141129

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}`.
150131

151132
### Styles
152133

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 .
134+
Crosstable uses [Word styles](https://support.microsoft.com/fr-fr/office/personnaliser-ou-cr%C3%A9er-des-styles-d38d6e47-f6fc-48eb-a607-1eb120dec563) extensively.
154135

155-
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.
156137

157138
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:
158139

@@ -173,50 +154,22 @@ Note that you might sometimes encounter the error "Error: could not match any st
173154

174155
### Post-production for table/figure legends
175156

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.
184159

160+
### Autofit macro for large tables
185161

162+
Auto-fitting from outside MS Word has its limits, and large tables might still overflow your document.
186163

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.
187165

188-
## Create reports with `Rmarkdown`
189-
190-
<!-- Integration Rmd in Rmd: la coloration syntaxique RStudio est mauvaise mais le code est bon ! -->
191-
<!-- https://stackoverflow.com/questions/53226493/add-markdown-code-chunk-to-r-markdown-document#comment104859956_53226493 -->
192-
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).
208-
209-
`r ''````{r description, echo=FALSE, results='asis'}
210-
cat("<caption> (\\#tab:irisTable) Table Iris </caption> \n\r ")
211-
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:
215167

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.
217169

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.
219171

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

Comments
 (0)