stargazer 将 data.frame 数据解释为乳胶代码是否构成错误或这是故意的?
Does stargazer interpreting data.frame data as latex code constitute a bug or is this intended?
我遇到了一个问题,即 Stargazer 函数将我 data.frame 中的数据解释为乳胶命令。我想找到一种方法来抑制 stargazer 的这个特性。见下文。
z <- c("Bank of America Corp",
"Citigroup Inc",
"JPMorgan Chase & Co",
"Morgan Stanley",
"Wells Fargo & Co")
s <- data.frame(z=z, l= c(100000, 25, 10000, 24, 100000))
library(stargazer)
stargazer(s, type = "text", summary = FALSE)
# prints out
==============================
z l
------------------------------
1 Bank of America Corp 100,000
2 Citigroup Inc 25
3 JPMorgan Chase Co
4 Morgan Stanley 24
5 Wells Fargo Co
------------------------------
这里的 & 符号由于其在乳胶中的含义而导致假定一个新列。我确认了这一点,因为将 & 替换为 and 会导致 table 正确打印出来。
z <- c("Bank of America Corp",
"Citigroup Inc",
"JPMorgan Chase and Co",
"Morgan Stanley",
"Wells Fargo and Co")
s <- data.frame(z=z, l= c(100000, 25, 10000, 24, 100000))
library(stargazer)
stargazer(s, type = "text", summary = FALSE)
# prints out
===============================
z l
-------------------------------
1 Bank of America Corp 100,000
2 Citigroup Inc 25
3 JPMorgan Chase and Co 10,000
4 Morgan Stanley 24
5 Wells Fargo and Co 100,000
-------------------------------
我可以在 stargazer 函数中调用任何选项来防止这种行为吗?
当前版本的 stargazer 似乎没有此选项。如果您查看源代码,您会发现以下代码块(第 4704 行):
############## TEXT AND html MODE ##############
.split.line <- # split line of a LaTeX table into constituent parts separated by &
function(s) {
# remove the "\\"
s <- gsub("\\", "", s, fixed=TRUE)
s <- paste(" ",s," ", sep="")
return(.trim(strsplit(s, " &", fixed=TRUE)[[1]]))
}
所以,显然,这似乎被硬编码到 stargazer 格式化 table 输出的方式中,并且没有被命令的任何选项捕获。
如果您只想格式化类似于您在示例中发布的数据框,我会使用 xtable
包中的 print.xtable()
,它具有正确处理和号的清理功能字符串并为建议的示例数据框生成正确的 LaTeX 和 HTML 输出。
我遇到了一个问题,即 Stargazer 函数将我 data.frame 中的数据解释为乳胶命令。我想找到一种方法来抑制 stargazer 的这个特性。见下文。
z <- c("Bank of America Corp",
"Citigroup Inc",
"JPMorgan Chase & Co",
"Morgan Stanley",
"Wells Fargo & Co")
s <- data.frame(z=z, l= c(100000, 25, 10000, 24, 100000))
library(stargazer)
stargazer(s, type = "text", summary = FALSE)
# prints out
==============================
z l
------------------------------
1 Bank of America Corp 100,000
2 Citigroup Inc 25
3 JPMorgan Chase Co
4 Morgan Stanley 24
5 Wells Fargo Co
------------------------------
这里的 & 符号由于其在乳胶中的含义而导致假定一个新列。我确认了这一点,因为将 & 替换为 and 会导致 table 正确打印出来。
z <- c("Bank of America Corp",
"Citigroup Inc",
"JPMorgan Chase and Co",
"Morgan Stanley",
"Wells Fargo and Co")
s <- data.frame(z=z, l= c(100000, 25, 10000, 24, 100000))
library(stargazer)
stargazer(s, type = "text", summary = FALSE)
# prints out
===============================
z l
-------------------------------
1 Bank of America Corp 100,000
2 Citigroup Inc 25
3 JPMorgan Chase and Co 10,000
4 Morgan Stanley 24
5 Wells Fargo and Co 100,000
-------------------------------
我可以在 stargazer 函数中调用任何选项来防止这种行为吗?
当前版本的 stargazer 似乎没有此选项。如果您查看源代码,您会发现以下代码块(第 4704 行):
############## TEXT AND html MODE ##############
.split.line <- # split line of a LaTeX table into constituent parts separated by &
function(s) {
# remove the "\\"
s <- gsub("\\", "", s, fixed=TRUE)
s <- paste(" ",s," ", sep="")
return(.trim(strsplit(s, " &", fixed=TRUE)[[1]]))
}
所以,显然,这似乎被硬编码到 stargazer 格式化 table 输出的方式中,并且没有被命令的任何选项捕获。
如果您只想格式化类似于您在示例中发布的数据框,我会使用 xtable
包中的 print.xtable()
,它具有正确处理和号的清理功能字符串并为建议的示例数据框生成正确的 LaTeX 和 HTML 输出。