Finalfit, knitr and R Markdown for quick results

Thank you for the many requests to provide some extra info on how best to get finalfit results out of RStudio, and particularly into Microsoft Word.

Here is how.

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

What follows is for demonstration purposes and is not meant to illustrate model building.

Does a tumour characteristic (differentiation) predict 5-year survival?

Demographics table

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

Note this useful alternative way of specifying explanatory variable lists:

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

Note missing data in obstruct.factor. We will drop this variable for now (again, this is for demonstration only). 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.

Logistic regression table

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

Odds ratio plot

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

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:

It’s ok, but not great.

Create Word template file

Now, edit the Word template. Click on a table. The style should be compact. Right click > Modify... > font size = 9. Alter heading and text styles in the same way as desired. Save this as template.docx. Upload to your project folder. Add this reference to the .Rmd YAML heading, as below. Make sure you get the space correct.

The plot also doesn’t look quite right and it prints with warning messages. Experiment with fig.width to get it looking right.

Now paste this into your .Rmd file and run:

This is now looking good for me, and further tweaks can be made.

To PDF via knitr/R Markdown

Default settings for PDF:

Again, ok but not great.

We can fix the plot in exactly the same way. But the table is off the side of the page. For this we use the kableExtra package. Install this in the normal manner. You may also want to alter the margins of your page using geometry in the preamble.

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.

11 Replies to “Finalfit, knitr and R Markdown for quick results”

  1. Can you please specify the data source for this table? If its confidential, maybe just the first first few observations so I can make a sample data set and try out your code on my own. Thanks

    1. The data is included in the package. It should all run as is. If not, add data(colon_s).

    1. Hi,
      Yes, make sure you are on the most up-to-date version. Any ggplot argument can be passed via plot_opts. The table_text_size etc. can be adjusted directly.

      1. Regarding:
        > Any ggplot argument can be passed via plot_opts

        I’m sorry, could you post an example of passing a ggplot argument via plot_opts to or_plot()?

        I tried a few different things and was unable to alter the plot output — I assume it’s something very obvious that I could figure out by more trial-and-error, but at this point, I think it’d be quicker to just ask, and then others who find this page could also see an example.

        1. There is an example on the or_plot help page.

          colon_s %>%
          or_plot(dependent, explanatory,
          plot_opts=list(xlab("OR, 95% CI"),
          theme(axis.title = element_text(size=12))))

          1. Thank you, but when I run that code, the x-label and text sizes don’t change.

            I get the identical plot as with just colon_s %>% or_plot(dependent, explanatory)

          2. Update (can’t seem to edit my reply): code to adjust text size / axis labels worked after updating to latest version with:


            Thanks very much! Very helpful package!

  2. This package is AMAZING!!!
    It will revolutionize my PhD.
    Thank you for sharing

  3. Hi,
    Thanks for sharing this.

    However, I noticed that the “Odds Ratio” output of the table is different from the values I obtained when I run logistic regression algorithm directly in R or Stata.

    Can you kindly confirm if the “OR” in the output table is Odds Ratio? If not, how can I convert to Odds Ratio

    1. Thanks. The table output is from logistic regression run directly in R, so it will be the same.
      Please provide reproducible example if think it is not working properly.

Comments are closed.