%20%E5%81%9C%E6%AD%A2%E6%B8%B2%E6%9F%93%20LaTeX%20%E8%A1%A8.png)
我有一個數據分析 Rmarkdown 文件,我將用它來為我的論文編寫 .pdf 結果部分。今天早上,當我在 Rstudio 中使用 knit 按鈕將 pdf 編織在一起時,我得到了漂亮的工作 LaTeX 表代碼。然而,我似乎已經打破了它。我不斷收到以下錯誤和輸出:error in as.string(y) : Cannot coerce argument to a string
,Calls: <Anonymous> ... print -> print.xtable -> +.string -> paste -> as.string
我的資料幀class(df)
讀取[1] "data.table" "data.frame"
,並且在調用我的範例後收到錯誤sum_table
。
我嘗試過的:
- 將我對 xtable() 和 print() 函數的新增一一刪除回極其簡單的形式。
- 逐行運行程式碼,而不是作為 Rmd 區塊運行。
- 向 xtable() 新增顯示參數xtable文檔
- 卸載並重新安裝 xtable 和 R。
booktabs=TRUE
為 print()增加了參數xtable 範例
關於此錯誤或故障排除有什麼想法嗎?
簡單的 Rmd 區塊範例:
---
output:
pdf_document:
fig_caption: yes
indent: true
---
# Rmd chunk settings
# results = 'asis' to output LaTex code that is recognized as code
{R Summary_Table, echo = FALSE, results = 'asis'}
library(knitr)
library(tinytex)
library(xtable)
options(xtable.timestamp = FALSE)
df <- data.frame(A = c(1:10),
B = c(11:20),
C = c(21:30))
sum_table <- xtable(df)
sum_table # Error thrown here "as.string(y) : Cannot coerce argument to a string"
print.xtable(sum_table)
# try another print function
print(sum_table)
# try adding booktabs argument
print(sum_table, booktabs = TRUE)
#Tried adding display argument to xtable(), extra for xtable's automatic
# number column. Thought maybe num variables couldn't be turned into a string?
sum_table2 <- xtable(df, display = c("s","f","f","f"))
sum_table2