Lab session 6
Publication Practical
Heather Turner
15 September 2017
Setup
This practical uses the following packages: dplyr, knitr, htmlTable
Main Practical
-
Create a new folder for this practical. Download
aspirin.csvfrom the course website and put it in this folder. In RStudio, create a new project from the folder you created. Create a new (empty) R markdown file in RStudio and save as “Outputs.Rmd” in the project folder.Set the options for knitr as follows, so that code, warnings and messages do not show in your output.
knitr::opts_chunk$set(echo = FALSE, warn = FALSE, message = FALSE) -
In an R chunk, import the
aspirin.csvdata. This data is the same as that shown in the lecture, but with the levels ofsexandconditionalready tidied up.Use functions from dplyr to create a tibble named
xwith the count, mean and standard deviation of value bysexandcondition(as in the simple summary table on slide 19 or 20). Useungroupto ungroup the tibble at the end.Use
kablefrom knitr to create a table fromx, using thedigitsandcol.namesarguments to customise the table as in the slides.Knit the document to create an HTML document containing just this table.
-
We will use the
htmlTablefunction from htmlTable to create booktabs style tables that we can later copy to Word. The following code creates a basichtmlTablebased on the data framex.library(htmlTable) lr <- c("l", "l", "r", "r", "r") htmlTable(x, rnames = FALSE, align = lr, align.header = lr, header = c("Sex", "Condition", "N", "Mean", "SD"), caption = "Table 1: A htmlTable table.")The argument
rnames = FALSEmeans that row numbers are not included in the table.Look at the help for
txtRoundfrom the htmlTable package to find out how to formatxso that the columns have the same number of decimal places as thekabletable in question 2.Add this table to your markdown file and knit your document to see the new table.
-
htmlTablecan also create tables with grouped rows, grouped columns and/or a formatted total row. For example, the rows could be grouped bysexas follows:lr <- c("l", "r", "r", "r") htmlTable(txtRound(select(x, -sex), digits = c(rep(0, 2), 1, 2)), align = lr, rgroup = c("Female", "Male"), n.rgroup = c(2, 2))Here we use
selectto remove thesexvariable fromx- note this will not work ifxis grouped bysex.Add this table to your document, updating the header row and the caption.
-
Now we will add a figure to the document. Create a boxplot of value by sex and condition. Suppress the x axis with
xaxt = "n"and set the y axis label to “Value”.Add a custom axis as follows
axis(1, at = 1:4, labels = c("Female\nAspirin", "Male\nAspirin", "Female\nPlacebo", "Male\nPlacebo"), line = 0.5, tick = FALSE)This adds a new line in the x axis labels and puts them a little lower down than the default.
Add the options
dev = "png", dpi = 600,to the R chunk creating the plot, so that it will be saved as a high resolution .png. Look up the chunk optionfig.height,fig.width,out.heightandout.widthon https://yihui.name/knitr/options/#chunk_options and experiment with setting these to different values. -
Change the YAML of your markdown to
--- title: title output: Gmisc::docx_document ---Note this requires the Gmisc package. Re-knit your document to see the difference it makes.
-
Open
Outputs.htmlin RStudio. Using Edit > Find search and replace all instances of the word “grey” with “black”. Click the “Preview” button to preview the html file. -
Download
template.docxfrom the website. This is a Word template for the journal “Europhysics Letters”. Open this file in Word.Copy an
htmlTableincluding its caption from the RStudio preview into the Word document. Change the font of the table and caption to match the body text. Put your cursor inside the table, go to the Layout menu and select .Within Word, use to insert the
.pngcreated when knitting your.Rmd. It will be in theOutputs_filessubdirectory of the directory where you have savedOutputs.Rmd.
Extra time exercise
-
Download
references.bibandepl.cslfrom the course website. In the YAML header ofOutputs.Rmdadd the linesbibliography: references.bib csl: epl.cslThe in the body of the
.Rmdand the lines@Agre02 @AitS04 # ReferencesThis cites two references from the BibTeX
references.bibfile. Re-knitOutputs.Rmdto obtain the references laid out in the journal style. The reference section can be copied to the Word document. -
Download the files
epl2.cls,latex_example.Rmd, andheader.tex. Thelatex_example.Rmdshows how a document class supplied by a journal can be used in a markdown file. The fileheader.texcontains the preamble from the example.texfile supplied by the journal - this contains fields such as\author{}that need to be filled in. This is also where packages such asbooktabscan be loaded. In the body of the text LaTex is used as necessary, e.g. for referencing figures and tables. Otherwise, markdown can be used as normal.Try compiling
latex_example.Rmdto produce the example PDF.