Skip underscore in R encoder

I need to call a database with underscores in table names in an R block in knitr. There are several thousand table names, and changing names will be a huge problem. Sort of:

<<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 an error that I am missing $ ("Missing $ insert") due to the underscore in "table_10_20". I played a lot with the addition of "\ $ \" and "\ $ \", you name it. Also played with cat () and paste (), as well as single quotes and double quotes. Any suggestions? Thanks in advance for your help. I start Ubuntu 11.10 and call knitr from RStudio using pdfLaTeX if that matters.

+5
source share
1 answer

Most likely you have a column name with an underscore in it.

, results='asis' as-is tex.

, :

% 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}   

knitr, " $ ".

.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}

, col_1 col_2 - ? , LaTeX (), , LaTeX ($) , .

, .

  • \begin{verbatim} results='asis' . verbatim.

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

    using verbatim

  • results='markup': , , sweave . (##); comment=NA. ( , , , , , . , echo=T).

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

    using results = markup

  • . , , xtable, data.frame (& ) ino LaTeX ( HTML). , , , . results='asis'. ( . , , ):

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

    using xtable

+11

All Articles