I am currently working on knitting an R markdown file to PDF using the following code chunk/function inspired by user Carlos Cinelli. The custom markdown function is the following:
```{r set-options, echo = FALSE, results = 'asis'}
rmarkdownTable <- function(df){
cat(paste(names(df), collapse = "|"))
cat("\n")
cat(paste(rep("-", ncol(df)), collapse = "|"))
cat("\n")
for(i in 1:nrow(df)){
cat(paste(df[i,], collapse = "|"))
cat("\n")
}
invisible(NULL)
}
rmarkdownTable(CurrentTableData)
```
CurrentTableData is a data.frame with one character class column (ID) and numeric class columns otherwise. I have already used this function to render other data frames to PDF, html and Word, without any issues.
However, when running it on CurrentTableData, the output table gets scrunched up and columns/column titles all overlap. I’ve printed out the following to demonstrate the data I have (with dput for reproducibility) and the problems I’m running into:
CurrTableDataList <- dput(head(CurrentTableData))
structure(list(ASIN = c("B0000004Y8", "B000000OQI", "B000000XB8",
"B0000017CI", "B000001A3H", "B000001ELB"), `NewPrice USD` = c("34.77",
"27.61", "21.49", "14.13", "16.49", "14.61"), `CurrentPrice USD` = c("43.50",
"35.98", "24.98", "12.98", "19.98", "19.98"), `FBAfees USD` = c("8.72",
"7.56", "6.68", "5.53", "5.88", "5.60"), `AddFees USD` = c("4.80",
"3.82", "2.97", "1.96", "2.28", "2.01"), `Cost USD` = c("20.78",
"14.63", "10.09", "6.48", "6.95", "5.30"), `AllFees USD` = c("34.30",
"26.01", "19.74", "13.97", "15.11", "12.91"), `NewProfit USD` = c("0.47",
"1.60", "1.75", "0.16", "1.38", "1.70"), `NewProfit CAD` = c("0.60",
"2.05", "2.24", "0.21", "1.77", "2.18"), `CurrentProfit CAD` = c("3.27",
"1.48", "1.81", "1.53", "1.56", "0.52"), `New % Profit` = c("2.25",
"10.93", "17.32", "2.53", "19.87", "32.11"), `Current % Profit` = c("22.22",
"8.22", "14.55", "18.43", "18.22", "7.91"), SalesRank = c(10153,
4809, 550, 13569, 6647, 5164)), .Names = c("ASIN", "NewPrice USD",
"CurrentPrice USD", "FBAfees USD", "AddFees USD", "Cost USD",
"AllFees USD", "NewProfit USD", "NewProfit CAD", "CurrentProfit CAD",
"New % Profit", "Current % Profit", "SalesRank"), row.names = c(NA,
6L), class = "data.frame")
Sample problematic output:
As a possible worthwhile mention, the above problematic output wraps the column names, whereas my previous outputs did not (not necessarily a bad thing, but it’s something I noticed - no changes were made to the markdown function and the column names were identical for the other outputs). I have attempted resizing using options(width = #some number) as well as in the output: pdf_document: dimensions in the hopes that it might help fit/space the columns out on the page, but no luck.
I am in R version 3.3.0 (2016-05-03) and running x86_64-apple-darwin13.4.0 (64-bit).