在旧的.Rnw文件中,我使用RODBC包读取Excel工作表并显示包含复选标记的乳胶表。

最近,我尝试更改用于读取excel文件的方式,并使用XLConnect包而不是RODBC,但是遇到了一个问题。我无法使对勾符号正确显示。请注意,我传递给xtable进行打印的对象与上一个对象相同,但结果不一样!

获取我使用的the excel file

这是有效的.Rnw。 (我知道代码绝不是最佳)。文件编码为UTF-8,我使用knitrXeLaTeX编织文件

\documentclass[a4paper]{article}
\usepackage{fontspec}
\usepackage{bbding} % checkmark symbol
\setmainfont[Scale=1.0]{Times New Roman} % Times New Roman
\setsansfont{Times New Roman}

<<eval=TRUE,echo=FALSE,warning=FALSE,message=FALSE,error=FALSE>>=
library(plyr)
library(XLConnect)
library(lubridate)
library(RODBC)
library(stringr)
library(xtable)
library(magrittr)
@

\begin{document}

\begin{center}

{\small
<<eval=TRUE,echo=FALSE,results='asis',warning=FALSE,message=FALSE,error=FALSE>>=

sheet = 'DATA-BOOK.xls'
con = odbcConnectExcel2007(sheet)
tbls = sqlTables(con)
df <- sqlQuery(con,"SELECT * FROM `r-crosstab$`",stringsAsFactors =FALSE)
close(con)


dfr <- df[,2:ncol(df)]
for (i in 1:ncol(dfr)){
  dfr[i][is.na(dfr[i])] <- ""
}

temp <- adply(dfr,1,function(x) length(x[x!=""]))

dfr <-  apply(dfr,2,function(x) gsub("\\?","\\\\Checkmark",x))

aitia <- paste0("c",1:nrow(dfr))
aitia <- unlist(sapply(aitia,function(x) if (nchar(x) == 2) {x=paste0(str_sub(x, 1, 1),"0",str_sub(x, 2, 2))} else{x} ))
names(aitia)<-NULL
dfr <- cbind(aitia,dfr,temp[ncol(temp)])

dfr1 <- t(dfr)
dfr1 <- apply(dfr1,2,as.character)
rownames(dfr1) <- c(colnames(dfr)[1:(length(colnames(dfr))-1)],"Αθρ.")
colnames(dfr1) <- dfr1[1,]
dfr1 <- dfr1[-1,]

dfr1[nrow(dfr1),] <- paste0("\\textbf{",dfr1[nrow(dfr1),],"}")

print(xtable(dfr1, align=paste0("l|",paste(rep("c|",length(aitia)),collapse="")),
             label='tab_crosstab'),
      scalebox=0.80,
      include.rownames=TRUE,
      hline.after=c(-1:nrow(dfr1)),
      sanitize.text.function = function(x){x}
)

@
}
\end{center}

\end{document}

那就是我编译时得到的(这是预期的结果)

r - 在 latex 表中显示\checkmark-LMLPHP

那是我面临问题的.Rnw。你能告诉我我需要改变吗?
\documentclass[a4paper]{article}
\usepackage{fontspec}
\usepackage{bbding} % checkmark symbol
\setmainfont[Scale=1.0]{Times New Roman} % Times New Roman
\setsansfont{Times New Roman}

<<eval=TRUE,echo=FALSE,warning=FALSE,message=FALSE,error=FALSE>>=
library(plyr)
library(XLConnect)
library(lubridate)
library(RODBC)
library(stringr)
library(xtable)
library(magrittr)
@

\begin{document}

\begin{center}

{\small
<<eval=TRUE,echo=FALSE,results='asis',warning=FALSE,message=FALSE,error=FALSE>>=

wb <- loadWorkbook('DATA-BOOK.xls', create = FALSE)
df <- readWorksheet(wb, sheet="r-crosstab", check.names=FALSE)

df %>%
dplyr::select(- dplyr::contains("ΑITIO")) %>%
names() -> vnames

df[vnames] %<>%
dplyr::mutate_if(is.factor, as.character)


dfr <- df[,2:ncol(df)]
for (i in 1:ncol(dfr)){
  dfr[i][is.na(dfr[i])] <- ""
}

temp <- adply(dfr,1,function(x) length(x[x!=""]))

dfr <-  apply(dfr,2,function(x) gsub("√","\\\\Checkmark",x)) # √   <U+221A>

aitia <- paste0("c",1:nrow(dfr))
aitia <- unlist(sapply(aitia,function(x) if (nchar(x) == 2) {x=paste0(str_sub(x, 1, 1),"0",str_sub(x, 2, 2))} else{x} ))
names(aitia)<-NULL
dfr <- cbind(aitia,dfr,temp[ncol(temp)])

dfr1 <- t(dfr)
dfr1 <- apply(dfr1,2,as.character)
rownames(dfr1) <- c(colnames(dfr)[1:(length(colnames(dfr))-1)],"Αθρ.")
colnames(dfr1) <- dfr1[1,]
dfr1 <- dfr1[-1,]

dfr1[nrow(dfr1),] <- paste0("\\textbf{",dfr1[nrow(dfr1),],"}")

print(xtable(dfr1, align=paste0("l|",paste(rep("c|",length(aitia)),collapse="")),
             label='tab_crosstab'),
      scalebox=0.80,
      include.rownames=TRUE,
      hline.after=c(-1:nrow(dfr1)),
      sanitize.text.function = function(x){x}
)
@
}
\end{center}

\end{document}

编译后的pdf如下所示:r - 在 latex 表中显示\checkmark-LMLPHP
> sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=Greek_Greece.1253
[2] LC_CTYPE=Greek_Greece.1253
[3] LC_MONETARY=Greek_Greece.1253
[4] LC_NUMERIC=C
[5] LC_TIME=Greek_Greece.1253

attached base packages:
[1] stats     graphics  grDevices utils     datasets
[6] methods   base

other attached packages:
 [1] magrittr_1.5         extrafont_0.17
 [3] xtable_1.8-3         stringr_1.3.1
 [5] RODBC_1.3-15         lubridate_1.7.4
 [7] XLConnect_0.2-15     XLConnectJars_0.2-15
 [9] plyr_1.8.4           tidyr_0.8.2
[11] dplyr_0.7.7

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.19     Rttf2pt1_1.3.7   rstudioapi_0.8
 [4] bindr_0.1.1      tidyselect_0.2.5 R6_2.3.0
 [7] rlang_0.3.0.1    tools_3.5.1      extrafontdb_1.0
[10] yaml_2.2.0       assertthat_0.2.0

编辑

在下面的代码片段对中,我尝试将示例最小化,而忽略了一些不相关的部分。再次,第一个是行得通的。同时,我注意到无论我用什么字符串替换gsub(在第二个示例中),结果始终是相同的!

工作代码:
\documentclass[a4paper]{article}
\usepackage{fontspec}
\usepackage{bbding} % checkmark symbol
\setmainfont[Scale=1.0]{Times New Roman} % Times New Roman
\setsansfont{Times New Roman}

<<eval=TRUE,echo=FALSE,warning=FALSE,message=FALSE,error=FALSE>>=
library(XLConnect)
library(RODBC)
library(xtable)
library(dplyr)
library(magrittr)
@

\begin{document}

{\small
<<eval=TRUE,echo=FALSE,results='asis',warning=FALSE,message=FALSE,error=FALSE>>=

sheet = 'DATA-BOOK.xls'
con = odbcConnectExcel2007(sheet)
tbls = sqlTables(con)
df <- sqlQuery(con,"SELECT * FROM `r-crosstab$`",stringsAsFactors =FALSE)
close(con)

df %>%
dplyr::select(- dplyr::contains("ΑITIO")) %>%
names() -> vnames

df[vnames] %<>%
mutate_if(is.factor, as.character) %>%
mutate_if(is.character, funs(coalesce(., "")))
names(df)[1] <- "TITLE"

dfr <-  apply(df,2,function(x) gsub("\\?","\\\\Checkmark",x))
rownames(dfr) <- NULL
dfr1 <- t(dfr)
dfr1[1,] <- paste0("c",1:19)

print(xtable(dfr1),
      scalebox=0.80,
      hline.after=c(-1:nrow(dfr1)),
      sanitize.text.function = function(x){x}
)
@
}

\end{document}

无效的代码:
\documentclass[a4paper]{article}
\usepackage{fontspec}
\usepackage{bbding} % checkmark symbol
\setmainfont[Scale=1.0]{Times New Roman} % Times New Roman
\setsansfont{Times New Roman}

<<eval=TRUE,echo=FALSE,warning=FALSE,message=FALSE,error=FALSE>>=
library(XLConnect)
library(RODBC)
library(xtable)
library(dplyr)
library(magrittr)
@

\begin{document}

{\small
<<eval=TRUE,echo=FALSE,results='asis',warning=FALSE,message=FALSE,error=FALSE>>=

wb <- loadWorkbook('DATA-BOOK.xls', create = FALSE)
df <- readWorksheet(wb, sheet="r-crosstab", check.names=FALSE)

df %>%
dplyr::select(- dplyr::contains("ΑITIO")) %>%
names() -> vnames

df[vnames] %<>%
mutate_if(is.factor, as.character) %>%
mutate_if(is.character, funs(coalesce(., "")))
names(df)[1] <- "TITLE"

dfr <-  apply(df,2,function(x) gsub("√","\\\\Checkmark",x)) # √   <U+221A>
dfr1 <- t(dfr)
dfr1[1,] <- paste0("c",1:19)

print(xtable(dfr1),
      scalebox=0.80,
      include.rownames=TRUE,
      hline.after=c(-1:nrow(dfr1)),
      sanitize.text.function = function(x){x}
)
@
}

\end{document}

最佳答案

问题是gsub行的格式。

建议的解决方案:

更换:
dfr <- apply(df,2,function(x) gsub("√","\\\\Checkmark",x)) # √ <U+221A>

dfr <- apply(dfr,2,function(x) gsub("\u221A","\\\\Checkmark",x)) # √ <U+221A>
结果:

https://www.dropbox.com/s/evzknzobfwh0ag1/53243041v2.pdf?dl=0

建立:

我在Windows 10,RStudio和R3.5.1上对此进行了测试,并使用knitr和XeLatex进行排版。

说明:

编译后的.rnw文件会创建一个中间.tex文件作为输出,然后将其转换为pdf。您必须将正确格式的代码\u221A"传递给pdf格式应用程序。

08-26 22:53