编织R Markdown到PDF的重叠列

时间:2016-06-14 14:09:51

标签: r rstudio knitr r-markdown

我目前正在使用以下由用户Carlos Cinelli启发的代码块/功能将R markdown文件编织为PDF。自定义降价功能如下:

```{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是一个data.frame,其中包含一个字符类列(ID)和数字类列。我已经使用此函数将其他数据框渲染为PDF,html和Word,没有任何问题。

但是,在CurrentTableData上运行时,输出表会被刷新,列/列标题都会重叠。我已经打印出以下内容来演示我的数据(dput用于重现性)以及我遇到的问题:

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")

示例有问题的输出:

Overlapping Columns Output

值得一提的是,上面有问题的输出包装了列名,而我之前的输出没有(不一定是坏事,但是我注意到了 - 没有对markdown函数进行任何更改,列名称是其他输出相同)。我尝试使用选项(width = #some number)以及输出中的大小调整:pdf_document:维度希望它可能有助于在页面上填充/分隔列,但没有运气。

我在R版本3.3.0(2016-05-03)并运行x86_64-apple-darwin13.4.0(64位)。

1 个答案:

答案 0 :(得分:1)

您可以控制列大小的方式是-符号。说清楚:

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

我调整了你的功能:

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)
}

Vector x应该具有您拥有的列数的长度,它告诉函数每列中应包含多少-

备注:似乎如果你的桌子很大,以适应一页,-标志的数量比例很重要。因此,如果每列有- 1000,那么它看起来就像每个列只有1 -一样。

进一步说明:我建议使用像xtable这样的软件包。它做得很好,对你的工作也少了。