I need to call a database that has underscores in the table names in an R chunk in knitr. 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”. 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. Any suggestions? Thanks in advance for your help. I am running Ubuntu 11.10, and calling knitr from RStudio with pdfLaTeX, if that matters.
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.For example, this is a reproducible example of your problem:
If I run this through
knitrI get the “Missing$inserted”.If I look at the .tex file that gets produced, I see:
See how the column names
col_1andcol_2are just dumped as-is into the file? 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.In your case, you have a few options depending on what you want for your output.
Use
\begin{verbatim}withresults='asis'to protect the underscores. This will dump your output into averbatimenvironment.Use
results='markup': this is like a verbatim environment except sweave colours the output. By default it’ll put a comment mark (##) in front of every line; to remove this usecomment=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 useecho=T).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 adata.frame(& similar) ino proper LaTeX (or HTML) markup. I think there are other packages that can do this too but for the moment they escape me. You useresults='asis'here. (See the documentation for more details, you really can control every aspect of what gets printed in the table and how):