Skip to contents

Motivation

Sometimes you may want to prodice a report in HTML format insead of Word. You can rewirte your analysis in RMarkdown and use the knitr package to generate a report. Or you can use the outputs from the cctu package to generate a HTML report instead of converting the Word to HTML. The HTML report has a nice layout and easy to read. It can resize the table to fit the screen size and interact with the table or figures. This will give the CI flexibility to look at the report in more detail. There is a nice R workflow tutorial by Frank E Harrell Jr on reporting.

But here we will be focusing on converting the cctu package output to HTML. The report is generated from the XML files in the Output/ directory. Under this folder, you should have Core/ and Figures/ folders. The Core/ folder contains the XML files for the tables and text. The Figures/ folder contains the figures. The report will be generated from these files. It will also relies on the meta_table.csv file in the Output/ folder. This file contains the information about the tables, text and the R script generated the corresponding tables/figures. There are templates you can use to generate the report.

  • RMarkdown
  • Quarto

Before you start, make sure you do the following in your main.R file.

write.csv(
  get_meta_table(), 
  file=file.path("Output","meta_table.csv"), row.names = FALSE
)

This is to make sure that all the meta data is saved in the meta_table.csv file. This file will be used as an input to the report. As this file includes information about the item type (tables, text or figure), item number and R scripts used to generate the item. It is important to make sure that this file is up to date.

RMarkdown report

The rmarkdown_report.Rmd file is the main file that will be used to generate the report. The table.Rmd and text.Rmd files are the templates for the tables and text respectively. The rmarkdown_report.Rmd file will call these templates to generate the report.

You can use the follwoing code to copy the templates to the current working directory.

# Copy the templates to the Progs folder under current working directory
rmd_files <- list.files(system.file("assets/rmd", package = "cctu"),
                        pattern = "*.Rmd",
                        full.names = TRUE)
file.copy(from = rmd_files, 
          to = "Progs", 
          overwrite = TRUE, 
          recursive = FALSE, 
          copy.mode = TRUE)

After copying the files, you can modify the template as you wish. Then use the following code to generate the report.

rmarkdown::render(
  # Path to the RMD template file
  file.path("Progs", "rmarkdown_report.Rmd"), 
  # Name of the output file
  output_file = "Vignette Report.html",  
  # Set the output directory to the Output/Reports folder
  output_dir = file.path("Output", "Reports"),
  # Set the working directory to the current working directory instead of the RMD file
  knit_root_dir = getwd(), 
  params = list(
    my_author = "Simon Bond",  # Name of the author
    my_title = "Vignette Report" # Title of the report
  )
)

Click here to see the example output of the report generated by the rmarkdown_report.Rmd file.

Quarto report

Quarto is a new open-source scientific and technical publishing system built on Pandoc (used by previous method). It is a great tool for creating reports, presentations, and websites. It is similar to RMarkdown but has more features and is more flexible. You can use Quarto to create a report in HTML format.

First, you need to copy yhe templates to the current working directory.

file.copy(from = system.file("assets/qmd/quarto-report.qmd", package = "cctu"), 
          to = getwd(), 
          overwrite = TRUE, 
          recursive = FALSE, 
          copy.mode = TRUE)

After copying the files, you can modify the template as you wish. Then use the following code to generate the report.

quarto::quarto_render(
  input = "quarto-report.qmd", # Path to the QMD template file
  # Name of the output file
  output_file = "Vignette Report.html", 
  # Set the output format to HTML
  output_format = "html",
  # Params to be passed to the QMD file
  execute_params = list(
    my_author = "Simon Bond",
    my_title = "Vignette Report",
    tinny_table = TRUE # Use the tinny table format for the tables
  )
)

Click here to see the example output of the report generated by the quarto-report.qmd file.

You can not set other directory for the output file with quarto::quarto_render(). You can also not be able to redirect the output to a different directory. For example, output_file = "Vignette Report.html" will not work. The quarto::quarto_render() will always generate the output file in the current working directory. You can use the file.copy() function or manully move the file to the desired directory.

You can set the tinny_table=FALSE if you want to use the flextable package to generate the tables. The Rmd uses the kableExtra package to generate the tables. The QMD uses the flextable or tinnytable package to generate the tables. The flextable is good for Word and HTML output but not so good for PDF and anyother formats. The kableExtra is built on the top kabel from knitr with complex coding. The kableExtra and flextable is more flexible than the tinnytable package but it is also more complex. The tinnytable package is a lightweight package that is easy to use and has a simple syntax with no dependencies. Please makesure you have install the relevant packages before you run the code.

Remark

You can change produce multiple types of reports using the same template. Including HTML, Word and PDF. But you should use the write_docx for the word and PDF as it will gives you a better looking output. You can save the word file as a PDF file.