简体   繁体   English

在knitr R代码中传递下划线

[英]Pass underscore in knitr R code

I need to call a database that has underscores in the table names in an R chunk in knitr. 我需要调用一个数据库,该数据库在knitr中的R块中的表名中有下划线。 There are a couple thousand table names, and changing the names would be a huge hassle. 有几千个表名,更改名称将是一个巨大的麻烦。 Something like: 就像是:

<<classRun,fig=FALSE,print=FALSE,echo=FALSE>>=
getdat = function(nbr1,nbr2){
library(RODBC)
database.dsn1<-c("db")
database.user1<-c("username")
database.password1<-c("password")
channel<-odbcConnect(database.dsn1, database.user1, database.password1)
dat = sqlQuery(channel,paste("select * from table_",nbr1,"_",nbr2, sep=""))
}
@

<< results='asis', echo = FALSE>>=
dat = getdat(10,20)
print(dat)
@

I get the error that I am missing a $ ("Missing $ inserted") because of the underscore in "table_10_20". 由于“table_10_20”中的下划线,我得到错误,我错过了$(“Missing $ inserted”)。 I have played around a lot with adding in '\\$ \\', and '\\$ \\', you name it. 添加'\\ $ \\'和'\\ $ \\',我已经玩了很多,你可以命名。 Also played around with cat(), and paste(), and single quotes, and double quotes. 还使用了cat()和paste(),单引号和双引号。 Any suggestions? 有什么建议? Thanks in advance for your help. 在此先感谢您的帮助。 I am running Ubuntu 11.10, and calling knitr from RStudio with pdfLaTeX, if that matters. 我正在运行Ubuntu 11.10,并使用pdfLaTeX从RStudio调用knitr,如果这很重要的话。

Chances are you have a column name with an underscore in it. 有可能你有一个带有下划线的列名。

Recall that results='asis' just dumps all the output as-is into the tex document. 回想一下results='asis'只是将所有输出原样转储到tex文档中。

For example, this is a reproducible example of your problem: 例如,这是您的问题的可重现示例:

% test.Rnw
\documentclass[a4paper]{article}                                                
\begin{document}                                                                
<<classRun, fig=FALSE, print=FALSE, echo=FALSE>>=                               
table_10_20 <- data.frame(col_1=1:10, col_2=runif(10))                          
@                                                                               

<<results='asis', echo=F>>=                                                     
print(table_10_20)                                                              
@                                                                               
\end{document}   

If I run this through knitr I get the "Missing $ inserted". 如果我通过knitr运行这个,我得到“Missing $ inserted”。

If I look at the .tex file that gets produced, I see: 如果我查看生成的.tex文件,我会看到:

% test.Rnw
\documentclass[a4paper]{article}                                                
.... lots of tex ....
\begin{document}

   col_1   col_2
1      1 0.69699
2      2 0.12988
3      3 0.19662
4      4 0.04299
5      5 0.08750
6      6 0.72969
7      7 0.19818
8      8 0.27855
9      9 0.81806
10    10 0.56135

\end{document}

See how the column names col_1 and col_2 are just dumped as-is into the file? 查看列名称col_1col_2如何按原样转储到文件中? Well, in LaTeX an underscore has a special meaning (subscript), which is only valid in maths mode, hence the LaTeX compiler tries to put the maths mode delimiters ( $ ) around the word, giving your error. 好吧,在LaTeX中,下划线有一个特殊的含义(下标),它只在数学模式下有效,因此LaTeX编译器试图在数字周围放置数学模式分隔符( $ ),给出错误。

In your case, you have a few options depending on what you want for your output. 在您的情况下,根据您的输出需要,您有几个选项。

  1. Use \\begin{verbatim} with results='asis' to protect the underscores. 使用\\begin{verbatim}results='asis'来保护下划线。 This will dump your output into a verbatim environment. 这会将您的输出转储到verbatim环境中。

     \\begin{verbatim} <<results='asis', echo=F>>= print(table_10_20) @ \\end{verbatim} 

    使用逐字

  2. Use results='markup' : this is like a verbatim environment except sweave colours the output. 使用results='markup' :这就像一个逐字环境,除了sweave为输出着色。 By default it'll put a comment mark ( ## ) in front of every line; 默认情况下,它会在每行前面添加注释标记( ## ); to remove this use comment=NA . 删除此使用comment=NA (You can't see too well how this pic is different from the above; it is the same except it has a grey background to distinguish it from the rest of the document. It's the same markup as when you use echo=T ). (你不能太清楚这张照片与上面的不同之处;除了它有一个灰色背景以区别于文档的其余部分之外,它是相同的。它与你使用echo=T时的标记相同)。

     <<results='markup', comment=NA, echo=F>>= print(table_10_20) @ 

    使用results = markup

  3. The above two simply print your table as-is in fixed with font. 上面两个只是用字体固定打印你的表格。 If you want a proper latex table, you can use a package like xtable , which can convert a data.frame (& similar) ino proper LaTeX (or HTML) markup. 如果你想要一个合适的乳胶表,你可以使用像xtable这样的包,它可以转换data.frame (和类似的)ino正确的LaTeX(或HTML)标记。 I think there are other packages that can do this too but for the moment they escape me. 我认为还有其他一些包可以做到这一点,但目前他们逃脱了我。 You use results='asis' here. 你在这里使用results='asis' (See the documentation for more details, you really can control every aspect of what gets printed in the table and how): (有关详细信息,请参阅文档,您真的可以控制表中打印内容的各个方面以及如何):

     <<results='asis', echo=F>>= library(xtable) print(xtable(table_10_20), include.rownames=FALSE) @ 

    使用xtable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM