I am writing a conference abstract booklet using R/Sweave. I have already made the program booklet for printing that contains the id, author, title only.
Now I want to modify it to include the abstract (not for print). But abstracts are lengthy. My thought is to take the cell with the abstract info, and have it display below the row with the author info - expanded across the full width of the page.
ID--author--------title--------------------------------
abstract-----------------------------------------------
So every other row has only one column spanning the width of the entire table.
Is there a way to add multicolmn{x} to every other row?
If a solution can't be figured out, advice for how to print full abstracts in a nice way would be welcome. (Something other than "just use landscape" or "adjust column widths")
Also, it doesn't have to be PDF. I could switch to markdown/html - and make it look closer to real conference program schedules that have full abstracts on them. Again, one I figure out how to print a table where every other row has only one column that is the width of the entire table.
If you want to try - Here is a complete MWE for what I have working now. Note that it uses the R package lipsum which has to be installed via devtools/github.
\documentclass{article}
\usepackage{booktabs, multicol, array}
\usepackage[margin=0.75in]{geometry}
%%%%%%%%%%% Let tables to span entire page
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
<<echo=FALSE, warning=FALSE, message=FALSE>>=
# devtools::install_github("coolbutuseless/lipsum")
library(lipsum)
library(xtable)
knitr::opts_chunk$set(echo = FALSE, warning=FALSE, message=FALSE)
options(xtable.comment = FALSE)
tblalign <- "lL{0.5cm}|L{4cm}L{6cm}L{8cm}"
# fake data setup
dat <- data.frame(ID = c(1:3), author = substr(lipsum[1:3], 1, 40),
title = substr(lipsum[4:6], 1, 100),
abstract = lipsum[7:9])
names(dat)=c("\\multicolumn{1}{c}{\\textbf{\\large{ID}}}",
"\\multicolumn{1}{c}{\\textbf{\\large{Author List}}}",
"\\multicolumn{1}{c}{\\textbf{\\large{Title}}}",
"\\multicolumn{1}{c}{\\textbf{\\large{Abstract}}}")
#
\begin{document}
<<results='asis'>>=
print(
xtable(x = dat
, align = tblalign)
, table.placement = "H"
, sanitize.colnames.function=function(x){x}
, include.rownames = FALSE
, include.colnames = TRUE
, size = "small"
, floating = FALSE
, hline.after = c(0,1:nrow(dat))
)
#
\end{document}
Split data from abstract manually
out <- dat[,-4]
ab.list <- dat$abstract
then add.to.row
, add.to.row = list(pos = as.list(1:nrow(out)),
command = paste0("\\multicolumn{3}{L{15cm}}{\\textbf{Abstract: }", ab.list, "} \\\\"))
One approach using my package huxtable. I couldn't install lipsum for some reason, so just hacked it. This is in a .Rmd file with output pdf_document.
```{r, results = 'asis'}
lipsum <- rep(do.call(paste, list(rep('blah ', 100), collapse = '')), 10)
dat <- data.frame(ID = c(1:3), author = substr(lipsum[1:3], 1, 40),
title = substr(lipsum[4:6], 1, 100),
abstract = lipsum[7:9], stringsAsFactors = FALSE)
library(huxtable)
# shape data
datmat <- matrix(NA_character_, nrow(dat) * 2, 3)
datmat[seq(1, nrow(datmat), 2), ] <- as.matrix(dat[, c('ID', 'author', 'title')])
datmat[seq(2, nrow(datmat), 2), 1] <- dat$abstract
# print as PDF
ht <- as_huxtable(datmat)
colspan(ht)[seq(2, nrow(ht), 2), 1] <- 3
wrap(ht) <- TRUE
col_width(ht) <- c(.2, .2, .6)
number_format(ht) <- 0
ht
```
Related
I am creating a custom code chunk which rewrites the expressions that a user passes into the code block into valid R code, and then execute the analysis. Aside from the rewriting of the R expression that the user inputs, the goal is for the code chunk to work as a regular code chunk
I then execute the result and store it in a variable, which I am trying to output. I am running into two issues here:
It prints invisible variables
knit_print and engine_output do not seem to work with each other i.e. I want the output such as dataframes to be printed in the table format but I am not able to get it to work.
An example of what I am trying to do would be something like:
```{r}
custom_engine <- function(options) {
c = options$code
pasted <- paste(c, collapse = "\n")
code <- parse(text = pasted)
result = lapply(code, eval, envir = .GlobalEnv)
knit_print(result)
engine_output( options, code = c, out = NULL )
}
knitr::knit_engines$set(mmm = custom_engine)
```
```{r}
speed_data <- data.frame(
speed = rlnorm(100, log(200), 1),
device = sample(c("smartphone", "laptop", "tablet"), 100, TRUE, prob= c(0.1, 0.65, 0.25))
)
median = median(speed_data$speed)
iqr = IQR(speed_data$speed, na.rm=TRUE)
```
```{mmm}
result_analysis <- speed_data %>%
filter(speed < median + 3 * iqr & speed > 10) %>%
filter(device != "smartphone")
result_analysis
```
I would like the code above to print the table once, and in the default knit_print() format that RMarkdown does.
Any suggestions would be much appreciated.
Im trying to inculde the output of the mediate function from mediation package in my RMarkdown (PDF) document. Using summary it gives me the table with the results from the bootstrapping analysis when I knitr the RMarkdown to a PDF document, but:
I don't like the look of the output and wuld like to have a more shiny table.
I can't lable this table with a caption and it's not included in the autmatic nummeration of RMarkdown (and as a consequence I can't reference it in the text).
I tried to use kable or xtabs with the output of the 'mediate` function but it won't work since both functions don't accept the class ("summary.mediate" "mediate") of the output.
This is how the code chunk in my RMarkdown document loks like:
```{r med.y1.z6.z7.c, echo = F, comment = "", strip.white = T, fig.cap="test"}
regDFM <- na.omit(as.data.frame(cbind(Y1, X1, Z1, Z6, Z7)))
regFIT1.C.medY <- lm(Y1 ~ X1+Z1+Z6+Z7+X1:Z1, data = regDFM)
regFIT1.C.medM <- lm(Z7 ~ X1+Z1+Z6+X1:Z1, data = regDFM)
fitMED <- mediation::mediate(regFIT1.C.medM, regFIT1.C.medY,
boot = T, sims = 10, treat="Z6", mediator="Z7")
summary(fitMED)
```
Any help or ideas are very appreciated!
With kable and the elements of the mediate function I finally created a nice RMarkdown > PDF output.
First I created a data.frame using the relevant elements of the mediate function (are the called 'elements'? what would yu call them?). Then just passing the data.frame to kable.
Thanks to #henrik_ibsen for the hint.
Here is my code:
regDFM <- na.omit(as.data.frame(cbind(Y1, X1, Z1, Z6, Z7)))
regFIT1.C.medY <- lm(Y1 ~ X1+Z1+Z6+Z7+X1:Z1, data = regDFM)
regFIT1.C.medM <- lm(Z7 ~ X1+Z1+Z6+X1:Z1, data = regDFM)
fitMED <- mediation::mediate(regFIT1.C.medM, regFIT1.C.medY,
boot = T, sims = 10, treat="Z6", mediator="Z7")
bt_effect <- c("Indirekter Effekt", "Direkter Effekt", "Gesamt Effekt",
"Anteil direkter Effekt")
bt_est <- c(fitMED$d1, fitMED$z1, fitMED$tau.coef, fitMED$n1)
#bt_p <- format.pval(c(fitMED$d1.p, fitMED$z1.p, fitMED$tau.p, fitMED$n1.p))
bt_p <- c(fitMED$d1.p, fitMED$z1.p, fitMED$tau.p, fitMED$n1.p)
bt_stars <- c(stars.pval(fitMED$d1.p), stars.pval(fitMED$z1.p),
stars.pval(fitMED$tau.p), stars.pval(fitMED$n1.p))
bt_DF <- data.frame(row.names = bt_effect, format(bt_est, digits = 2),
format(bt_p, nsmall = 3), bt_stars)
colnames(bt_DF) <- c("Koeffizienten", "p-Werte", "")
kable(bt_DF, booktabs = T, align = "c",
caption = "Bootstraping-Analyse für Mediation") %>%
footnote(general = c("Simulationen: 1000", "Signifikanzniveaus: ∗ p<0.05;
∗∗ p<0.01; ∗∗∗ p<0.001"),
general_title = "Anmerkungen:")
Given a data.frame A, how can I use subscripted rows and columns names? Eventually I want produce a table through kable() in rmarkdown (output: word document).
A <- data.frame(round(replicate(3, runif(2)),2))
rownames(A) <- c("Hola123", "Hola234")
A
X1 X2 X3
Hola123 0.47 0.55 0.66
Hola234 0.89 0.45 0.20
How could I make all numbers from row and column names subscripted when creating a table through kable(A)?
I have tried:
rownames(A) <- c(expression(Hola["123"]), expression(Hola["234"]))
names(A) <- c(expression(X["1"]), expression(X["2"]), expression(X["3"]))
But it does not appears subscripted when creating the table through kable() in the .rmd file.
To add subscripts in a rmarkdown document, you can embed your text between two tilde: text~sub~.
When using function kable, any text in the table is recognized as markdown syntax. So that your rmarkdown code should be:
```{r}
A <- data.frame(round(replicate(3, runif(2)),2))
rownames(A) <- c("Hola~123~", "Hola~234~")
names(A) <- c("X~1~", "X~2~", "X~3~")
knitr::kable(A)
```
Just one note about bamphe's response is that the correct code is misspelled. It should be \\textsubscript{}. It is missing the second "t".
And completing the answer, you might choose to use the arguments row.names and col.names inside kable, in this way:
A <- data.frame(round(replicate(3, runif(2)),2))
rownames(A) <- c("Hola\\textsubscript{123}", "Hola\\textsubscript{234}")
knitr::kable(A,
row.names = T,
col.names = c("X\\textsubscript{1}", "X\\textsubscript{2}", "X\\textsubscript{3}"),
escape = F)
I, too, was looking for a method that would allow for subscript and superscript in both html and pdf formats in markdown tables with kable. After a bit of searching, I finally found the text reference method explained here by #yihui-xie : bookdownguide
(ref:foo) H~2~O where foo is the reference and H~2~O the text.
My code example shows how the text reference can be used. Make sure to follow the cardinal rules:
The reference needs to be unique throughout the document
The reference should not have a white space following the "to be inserted stuff"
The reference needs to be in its own paragraph and have an empty line both above and below it
Note that only the referenced "foo" and "fo" will give the subscripts while the ~[]~ method will only work in html but not pdf.
(ref:foo) CO~2~/CO~2~
(ref:fo) CO~2~
```{r chunk-to-show-the-text-reference-method, echo = FALSE }
library(dplyr)
library(knitr)
library(kableExtra)
# Make lists
dtmin_name <- c("ref/ref","refrigerant/CO2","(ref:foo)",paste0("ground/","(ref:fo)"),"ground/water","air/refrigerant","water/refrigerant","water/CO2")
temp_diff <- c( 2.3, 1.4, 0.8, 6.8, 14, 6, 4, 3.46)
# Make dataframe and column names
dtmin_df <- data.frame(dtmin_name,temp_diff, stringsAsFactors = FALSE)
colnames <- data.frame("Interface Type ", "dT~min~ Interval [K]", stringsAsFactors = FALSE)
colnames(dtmin_df) <- colnames
# Make Table
kable(dtmin_df, caption = "Typical dT~min~ Temperature Intervals", booktabs = TRUE, format.args = list(big.mark = ",")) %>%
kable_styling(bootstrap_options = c("striped", "hover"),latex_options = c("striped","scale_down"))```
I am looking for a help to generate a 'rtf' report from R (dataframe).
I am trying output data with many columns into a 'rtf' file using following code
library(rtf)
inp.data <- cbind(ChickWeight,ChickWeight,ChickWeight)
outputFileName = "test.out"
rtf<-RTF(paste(".../",outputFileName,".rtf"), width=11,height=8.5,font.size=10,omi=c(.5,.5,.5,.5))
addTable(rtf,inp.data,row.names=F,NA.string="-",col.widths=rep(1,12),header.col.justify=rep("C",12))
done(rtf)
The problem I face is, some of the columns are getting hide (as you can see last 2 columns are getting hide). I am expecting these columns to print in next page (without reducing column width).
Can anyone suggest packages/techniques for this scenario?
Thanks
Six years later, there is finally a package that can do exactly what you wanted. It is called reporter (small "r", no "s"). It will wrap columns to the next page if they exceed the available content width.
library(reporter)
library(magrittr)
# Prepare sample data
inp.data <- cbind(ChickWeight,ChickWeight,ChickWeight)
# Make unique column names
nm <- c("weight", "Time", "Chick", "Diet")
nms <- paste0(nm, c(rep(1, 4), rep(2, 4), rep(3, 4)))
names(inp.data) <- nms
# Create table
tbl <- create_table(inp.data) %>%
column_defaults(width = 1, align = "center")
# Create report and add table to report
rpt <- create_report("test.rtf", output_type = "RTF", missing = "-") %>%
set_margins(left = .5, right = .5) %>%
add_content(tbl)
# Write the report
write_report(rpt)
Only thing is you need unique columns names. So I added a bit of code to do that.
If docx format can replace rtf format, use package ReporteRs.
library( ReporteRs )
inp.data <- cbind(ChickWeight,ChickWeight,ChickWeight)
doc = docx( )
# uncomment addSection blocks if you want to change page
# orientation to landscape
# doc = addSection(doc, landscape = TRUE )
doc = addFlexTable( doc, vanilla.table( inp.data ) )
# doc = addSection(doc, landscape = FALSE )
writeDoc( doc, file = "inp.data.docx" )
I have a simple data structure: cases are countries and for each country I have a couple of numeric variables. Like so:
dat <- data.frame(country = c("Belgium", "Germany", "Holland", "Ireland"), Var1 = 1:4, Var2 = 11:14)
print(dat, row.names = FALSE)
country Var1 Var2
Belgium 1 11
Germany 2 12
Holland 3 13
Ireland 4 14
The table needs to be formatted still, with headings in bold, and rows colored in grey or white, alternatingly.
Now, what I want is to add two additional columns, in between "country" and "Var1". The first new column is called "flag" and should contain the country's flag. The second new column is called "flagged" and contains an image of a red flag is the country scores very bad on a particular human rights issue, an orange flag if it scores mediocre and nothing elsewise.
How can I create an object that prints that way in R? How to combine images with data in such a layout?
(eventually this will part of a larger document created with knitr)
If you are using knitr with the rmarkdown package, it is pretty easy -- just use the Markdown syntax ![]() to include images, e.g.
---
title: "Flags"
author: "Yihui Xie"
date: "2014/08/03"
output: html_document
---
```{r results='asis'}
dat <- data.frame(
country = c('Canada', 'United Kindom'),
abbr = c('ca', 'gb'),
var1 = c(1, 2),
var2 = rnorm(2)
)
dat$flag <- sprintf('![](http://flagpedia.net/data/flags/mini/%s.png)', dat$abbr)
library(knitr)
kable(dat)
```
If you need LaTeX/PDF output, you have to download these images by yourself. Here is an example:
---
title: "Flags"
author: "Yihui Xie"
date: "2014/08/03"
output: html_document
---
```{r}
dat <- data.frame(
country = c('Canada', 'United Kindom'),
abbr = c('ca', 'gb'),
var1 = c(1, 2),
var2 = rnorm(2)
)
dat$file <- paste0(dat$abbr, '.png')
dat$link <- paste0('http://flagpedia.net/data/flags/mini/', dat$file)
dat$flag <- sprintf('![](%s)', dat$file)
for (i in seq_len(nrow(dat))) {
if (!file.exists(dat$file[i])) xfun::download_file(dat$link[i])
}
knitr::kable(dat[, -c(5, 6)])
```
With this experimental fork of gtable, you can do,
require(gtable)
dat <- data.frame(country = c("Belgium", "Germany", "Holland", "Ireland"), Var1 = 1:4, Var2 = 11:14)
g <- gtable_table(dat)
library(png)
# pirate-land flag for illustration
img <- readPNG(system.file("img", "Rlogo.png", package="png"), native = FALSE)
imgRgb <- rgb(img[,,1],img[,,2],img[,,3])
dim(imgRgb) <- dim(img)[1:2]
flags <- replicate(nrow(g), rasterGrob(imgRgb), simplify = FALSE)
g <- gtable_add_cols(g, unit(1,"cm"), 0)
g <- gtable_add_grob(g, flags, t = seq_len(nrow(g)), l=1, r=1, z=1)
grid.newpage()
grid.draw(g)
formatting options described here
The question was asked with pdf as the output, here is an answer using knitr, and something more appropriate than just \includegraphics
The trick is to use the adjustbox package in latex, with the following arguments:
the height (later used as the argument to the R function get_picture_code) is the height of the picture.
the valign argument (which default here to valign=m), will perform vertical adjustment according to the text,
the margin here defined as 1ex surrounding the picture allows to separate the flags.
So we just use this function
get_picture_code <- function(path,height,col=NULL)
{
paste0("\\adjustimage{height=",height,",valign=m,margin*=1ex}{",path,"}")
}
To get a vector of images added to the table.
Finally we use xtable with argument sanitize.text.function = identity to print the tex code :
\documentclass{article}
\usepackage{adjustbox}
\begin{document}
<<load_libraries, echo = FALSE, eval = TRUE, results ="hide">>=
library(knitr)
library(xtable)
#
<<include_images, echo = FALSE, eval = TRUE, results ="hide">>=
get_picture_code <- function(path,height,col=NULL){
paste0("\\adjustimage{height=",height,",valign=m,margin*=1ex}{",path,"}")
}
#
<<test, echo = FALSE, eval = TRUE, results ="hide">>=
dat <- data.frame(country = c("Belgium", "Germany", "Holland", "Ireland"),
Var1 = 1:4, Var2 = 11:14)
mypath <- paste0("images/",dat$country,".png")
dat$flag <- get_picture_code(path=mypath,height="0.8cm")
dat$test <-NA
dat$test[2:3] <-get_picture_code(path="images/orange_flag",height="0.6cm")
print(xtable(dat,
align = c("l","l","l","l","c","c"),
caption = "Example with flags"),
sanitize.text.function = identity,
file="table_with_images.tex")
#
\input{table_with_images.tex}
\end{document}
The adjustbox documentation contains many other options, including background colors, trim, horizonal alignment which will allow you to do some very fine adjustment of the position of the images...
There is also a nice example of the use of this package in TeX-Latex stackexchange