I want to print two table (3x2 and 2x3) at the top of a PDF page next to each other. The following code prints them in the centre despite pagecentre = FALSE:
tab2 <- tableGrob(df2)
tab3 <- tableGrob(df3)
pdf("file.pdf", height = 20, width = 15, pagecentre = FALSE)
grid.arrange(tab2, tab3, ncol = 2, nrow = 1))
dev.off()
How do I fix this using layout()? I looked at the function but can't understand how the to set the matrix.
I'd also like to add table titles. Do I do this with using a data frame function or while writing to df to pdf?
Related
I am trying to use officer to add images to a Word document. I have a whole directory's worth of images that I want to loop through. The problem I am having is that I need to add the image to the document, then add the next image to the newly created document that I just created by adding the last image.
Here's a sample of the code without loops or functions:
library(magrittr)
library(officer)
read_docx() %>% # create document
body_add_img("img1.png", width = 3, height = 4) %>% # add image
body_add_img("img2.png", width = 3, height = 4) %>% # add image
body_add_img("img3.png", width = 3, height = 4) %>% # add image
print(target = "samp.docx") # write document
Using map and lapply doesn't work in this case because every iteration needs to return the object of the previous iteration. I tried writing a function with a for loop but I think I was way off. Any help and pointers would be appreciated.
I think you can use a reduce here. For example using a bit of purrr
library(purrr)
read_docx() %>%
reduce(1:3, function(docx, idx) {
docx %>% body_add_img(paste0("img", idx, ".png"), width = 3, height = 4)
}, .init=.) %>%
print(target = "samp.docx")
The reduce keeps feeding the result back into itself.
I am not sure what was your attempt with the for loop but this simple loop seem to work.
library(officer)
data <- read_docx()
image_list <- paste0('img', 1:3, '.png')
for(i in image_list) {
data <- body_add_img(data, i, width = 3, height = 4)
}
print(data, target = "samp.docx")
I am trying to read in a number of datasets (approx 300) all with similar names to the following (I am not loading them all in at the same time, but I am trying to find a generalised solution where I only change a few things at the beginning of the R file)
E:/Data/Academic/Year1/External/beer/beer_drug_1114_1165
E:/Data/Academic/Year1/External/beer/beer_groc_1114_1165
E:/Data/Academic/Year1/External/beer/beer_PANEL_DR_1114_1165.dat
E:/Data/Academic/Year1/External/beer/beer_PANEL_GR_1114_1165.dat
E:/Data/Academic/Year1/External/beer/beer_PANEL_MA_1114_1165
E:/Data/Academic/Year1/External/beer/Delivery_Stores
The only things which changes are;
Year1 in the E:/Data/Academic/Year1/External
beer in the beer/beer_drug_1114_1165
1114_1165 at the end and the extentions
So I am trying different combinations of paste0 in order to recreate the file extentions.
I have something such as the following which isnĀ“t working so well.
file <- "E:/IRI Data/Academic Dataset External/Year1/External/"
product <- "/beer"
weeks <- "_1114_1165"
paste0(file, product, product, weeks)
But I would like to change /Year1/ in the middle of the extension.
Extentions:
drug <- read.table("E:/Data/Academic/Year1/External/beer/beer_drug_1114_1165", header = TRUE)
groc <- read.table("E:/Data/Academic/Year1/External/beer/beer_groc_1114_1165", header = TRUE)
PANEL_DR <- read.delim("E:/Data/Academic/Year1/External/beer/beer_PANEL_DR_1114_1165.dat", header = TRUE)
PANEL_GR <- read.delim("E:/Data/Academic/Year1/External/beer/beer_PANEL_GR_1114_1165.dat", header = TRUE)
PANEL_MA <- read.delim("E:/Data/Academic/Year1/External/beer/beer_PANEL_MA_1114_1165.dat", header = TRUE)
Delivery_Stores <- read.fwf("E:/Data/Academic/Year1/External/beer/Delivery_Stores",
widths = c(7, 3, 9, 21, 5, 4, 5, 9))
I tried multiple way like passing style as gird, multitable and split cells but in each case, my table split in two part, can someone please help
library(pander)
l <- data.frame(id = 1, text = "Shake a tail feather!
<ed><U+00A0><U+00BD><ed><U+00B2><U+0083><ed><U+00A0><U+00BC><ed><U+00BF><U+00BC>
#whiteeeee #rrrrrrrrrr #eeeeeeeeeeeeee #
Doubletree by fadfadsfasdf
Evansville
https://fadfaffadsfafadsfadsfasdfads")
pandoc.table(l, split.cells = c(10,100))
Try setting the split.tables parameter such as this:
pandoc.table(l, split.cells = c(10,100), split.tables = Inf)
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
```
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" )