重叠列编织 R Markdown 到 PDF
Overlapping Columns knitting R Markdown to PDF
我目前正在使用受用户 Carlos Cinelli 启发的以下代码 chunk/function 将 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,具有一个字符 class 列 (ID),否则为数字 class 列。我已经使用此功能将其他数据框渲染为 PDF、html 和 Word,没有任何问题。
但是,当 运行 它在 CurrentTableData 上时,输出 table 被压缩并且 columns/column 标题全部重叠。我已经打印出以下内容来演示我拥有的数据(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")
有问题的输出示例:
可能值得一提的是,上面有问题的输出包含了列名,而我之前的输出没有(不一定是坏事,但我注意到了这一点 - 没有对降价函数和列进行任何更改其他输出的名称相同)。我尝试使用 options(width = #some number) 以及在输出中调整大小:pdf_document: dimensions 希望它可以帮助 fit/space 页面上的列,但没有运气。
我在 R 版本 3.3.0 (2016-05-03) 和 运行 x86_64-apple-darwin13.4.0(64 位).
您可以通过 -
标志来控制列的大小。说清楚:
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
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
我稍微调整了你的功能:
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)
}
向量 x 的长度应为您拥有的列数,它告诉函数每列应包含多少 -
。
备注:如果您的 table 太大而无法放在一页上,那么 -
符号的数量比例似乎很重要。因此,如果每列有 1000 -
,那么它看起来就像每列只有 1 -
.
进一步说明:我建议使用像 xtable
这样的包。它做得很好,对你来说工作更少。
我目前正在使用受用户 Carlos Cinelli 启发的以下代码 chunk/function 将 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,具有一个字符 class 列 (ID),否则为数字 class 列。我已经使用此功能将其他数据框渲染为 PDF、html 和 Word,没有任何问题。
但是,当 运行 它在 CurrentTableData 上时,输出 table 被压缩并且 columns/column 标题全部重叠。我已经打印出以下内容来演示我拥有的数据(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")
有问题的输出示例:
可能值得一提的是,上面有问题的输出包含了列名,而我之前的输出没有(不一定是坏事,但我注意到了这一点 - 没有对降价函数和列进行任何更改其他输出的名称相同)。我尝试使用 options(width = #some number) 以及在输出中调整大小:pdf_document: dimensions 希望它可以帮助 fit/space 页面上的列,但没有运气。
我在 R 版本 3.3.0 (2016-05-03) 和 运行 x86_64-apple-darwin13.4.0(64 位).
您可以通过 -
标志来控制列的大小。说清楚:
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
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
我稍微调整了你的功能:
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)
}
向量 x 的长度应为您拥有的列数,它告诉函数每列应包含多少 -
。
备注:如果您的 table 太大而无法放在一页上,那么 -
符号的数量比例似乎很重要。因此,如果每列有 1000 -
,那么它看起来就像每列只有 1 -
.
进一步说明:我建议使用像 xtable
这样的包。它做得很好,对你来说工作更少。