1

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:

Overlapping Columns 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).

Community
  • 1
  • 1
chris_lee
  • 27
  • 8
  • Do you want to use your own function? This could be achieved more easily by using for example `xtable()` from the package of the same name. – Alex Jun 15 '16 at 09:06

1 Answers1

1

The way you can control the size of the columns is over the - signs. To make it clear:

ASIN|NewPrice USD|CurrentPrice USD|FBAfees USD|AddFees USD|Cost USD|AllFees USD|NewProfit USD|NewProfit CAD|CurrentProfit CAD|New % Profit|Current % Profit|SalesRank
-|-|-|-|-|-|-|-|-|-|-|-|-
B0000004Y8|34.77|43.50|8.72|4.80|20.78|34.30|0.47|0.60|3.27|2.25|22.22|10153
B000000OQI|27.61|35.98|7.56|3.82|14.63|26.01|1.60|2.05|1.48|10.93|8.22|4809
B000000XB8|21.49|24.98|6.68|2.97|10.09|19.74|1.75|2.24|1.81|17.32|14.55|550
B0000017CI|14.13|12.98|5.53|1.96|6.48|13.97|0.16|0.21|1.53|2.53|18.43|13569
B000001A3H|16.49|19.98|5.88|2.28|6.95|15.11|1.38|1.77|1.56|19.87|18.22|6647
B000001ELB|14.61|19.98|5.60|2.01|5.30|12.91|1.70|2.18|0.52|32.11|7.91|5164

enter image description here

ASIN|NewPrice USD|CurrentPrice USD|FBAfees USD|AddFees USD|Cost USD|AllFees USD|NewProfit USD|NewProfit CAD|CurrentProfit CAD|New % Profit|Current % Profit|SalesRank
-------------|-|-|-|-|-|-|-|-|-|-|-|-
B0000004Y8|34.77|43.50|8.72|4.80|20.78|34.30|0.47|0.60|3.27|2.25|22.22|10153
B000000OQI|27.61|35.98|7.56|3.82|14.63|26.01|1.60|2.05|1.48|10.93|8.22|4809
B000000XB8|21.49|24.98|6.68|2.97|10.09|19.74|1.75|2.24|1.81|17.32|14.55|550
B0000017CI|14.13|12.98|5.53|1.96|6.48|13.97|0.16|0.21|1.53|2.53|18.43|13569
B000001A3H|16.49|19.98|5.88|2.28|6.95|15.11|1.38|1.77|1.56|19.87|18.22|6647
B000001ELB|14.61|19.98|5.60|2.01|5.30|12.91|1.70|2.18|0.52|32.11|7.91|5164

enter image description here

I adjusted your function a bit:

rmarkdownTable <- function(df, x){
  cat(paste(names(df), collapse = "|"))
  cat("\n")
  col_length <- function(x) paste(rep('-', x), collapse =  '')
  cat(paste(sapply(x,col_length), collapse = "|"))
  cat("\n")

  for(i in 1:nrow(df)){
    cat(paste(df[i,], collapse = "|"))
    cat("\n")
  }
invisible(NULL)
}

The Vector x, which should have the length of the number of columns you have, tells the function how many - it shall include in each column.

Remark: It seems that if your table is to big to fit on one page the ratio of the number of - signs is important. So if each column has 1000 - than it will look the same as if each cloumn has only 1 -.

Further Remark: I recommend to use a package like xtable. It does a pretty good job and is less work for you.

Alex
  • 4,925
  • 2
  • 32
  • 48
  • Great, thorough answer: I appreciate the edits to the code and will definitely be looking at `xtable()` for future use. Thank you! – chris_lee Jun 15 '16 at 13:12