Exporting tables and plots

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

finalfit makes it easy to export final results tables and plots from RStudio to Microsoft Word and PDF.

Make sure you are on the most up-to-date version of finalfit.

install.packages("finalfit")

What follows is for demonstration purposes and is not meant to illustrate model building. We will ask, does a particular characteristic of a tumour (differentiation) predict 5-year survival?

Explore data

First explore variable of interest (exposure) by making it the dependent.

library(finalfit)
library(dplyr)

dependent = "differ.factor"

# Specify explanatory variables of interest
explanatory = c("age", "sex.factor", 
  "extent.factor", "obstruct.factor", 
  "nodes")

Note this useful alternative way of specifying explanatory variable lists:

colon_s %>% 
  select(age, sex.factor, extent.factor, obstruct.factor, nodes) %>% 
  names() -> explanatory

Check data.

colon_s %>% 
  ff_glimpse(dependent, explanatory)

Demographics table

Look at associations between our exposure and other explanatory variables. Include missing data.

colon_s %>% 
  summary_factorlist(dependent, explanatory, 
  p=TRUE, na_include=TRUE)
colon_s %>% 
  summary_factorlist(dependent, explanatory, 
  p=TRUE, na_include=TRUE) %>% 
  knitr::kable(row.names=FALSE, align=c("l", "l", "r", "r", "r", "r"))

Note missing data in obstruct.factor. See a full description of options in the forthcoming missing data vignette.

We will drop this variable for now. Also see that nodes has not been labelled. There are small numbers in some variables generating chisq.test warnings (predicted less than 5 in any cell). Generate final table.

explanatory = c("age", "sex.factor", 
  "extent.factor", "nodes")

colon_s %>% 
    mutate(
        nodes = ff_label(nodes, "Lymph nodes involved")
    ) %>% 
  summary_factorlist(dependent, explanatory, 
    p=TRUE, na_include=TRUE, 
    add_dependent_label=TRUE) -> table 1
table1
explanatory = c("age", "sex.factor", 
  "extent.factor", "nodes")

colon_s %>% 
  mutate(
    nodes = ff_label(nodes, "Lymph nodes involved")) %>% 
  summary_factorlist(dependent, explanatory, 
    p=TRUE, na_include=TRUE, 
    add_dependent_label=TRUE) %>% 
    knitr::kable(row.names=FALSE, align=c("l", "l", "r", "r", "r", "r"))

Logistic regression table

Now examine explanatory variables against outcome. Check plot runs ok.

explanatory = c("age", "sex.factor", 
  "extent.factor", "nodes", "differ.factor")
dependent = "mort_5yr"
colon_s %>% 
  finalfit(dependent, explanatory, 
  dependent_label_prefix = "") -> table2
table2
explanatory = c("age", "sex.factor", 
  "extent.factor", "nodes", "differ.factor")
dependent = "mort_5yr"
colon_s %>% 
  finalfit(dependent, explanatory, 
  dependent_label_prefix = "") %>% 
    knitr::kable(row.names=FALSE, align=c("l", "l", "r", "r", "r", "r"))

Odds ratio plot

colon_s %>% 
  or_plot(dependent, explanatory, 
  breaks = c(0.5, 1, 5, 10, 20, 30))

MS Word via knitr/R Markdown

Important. In most R Markdown set-ups, environment objects require to be saved and loaded to R Markdown document.

# Save objects for knitr/markdown
save(table1, table2, dependent, explanatory, file = "out.rda")

We use RStudio Server Pro set-up on Ubuntu. But these instructions should work fine for most/all RStudio/Markdown default set-ups.

In RStudio, select File > New File > R Markdown.

A useful template file is produced by default. Try hitting knit to Word on the knitr button at the top of the .Rmd script window.

Now paste this into the file:

---
title: "Example knitr/R Markdown document"
author: "Ewen Harrison"
date: "22/5/2018"
output:
  word_document: default
---
```r
# Load data into global environment. 
library(finalfit)
library(dplyr)
library(knitr)
load("out.rda")

wzxhzdk:13


## Table 2 - Association between tumour factors and 5 year mortality
```r
kable(table2, row.names=FALSE, align=c("l", "l", "r", "r", "r", "r"))

wzxhzdk:14


## Table 1 - Demographics
```r
kable(table1, row.names=FALSE, align=c("l", "l", "r", "r", "r", "r"))

wzxhzdk:15


## Figure 1 - Association between tumour factors and 5 year mortality
```r
colon_s %>% 
  or_plot(dependent, explanatory)

wzxhzdk:16


## Table 1 - Demographics
```r
kable(table1, row.names=FALSE, align=c("l", "l", "r", "r", "r", "r"))

wzxhzdk:17


## Figure 1 - Association between tumour factors and 5 year mortality
```r
colon_s %>% 
  or_plot(dependent, explanatory)

wzxhzdk:18


## Table 1 - Demographics
```r
kable(table1, row.names=FALSE, align=c("l", "l", "r", "r", "r", "r"),
                        booktabs=TRUE)

wzxhzdk:19


## Figure 1 - Association between tumour factors and 5 year mortality
```r
colon_s %>% 
  or_plot(dependent, explanatory)

This is now looking pretty good for me as well.

There you have it. A pretty quick workflow to get final results into Word and a PDF.



Try the finalfit package in your browser

Any scripts or data that you put into this service are public.

finalfit documentation built on Nov. 17, 2023, 1:09 a.m.