Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8450099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:52:50+00:00 2026-06-10T10:52:50+00:00

I need to call a database that has underscores in the table names in

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T10:52:51+00:00Added an answer on June 10, 2026 at 10:52 am

    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:

    % 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”.

    If I look at the .tex file that gets produced, I see:

    % 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? 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.

    1. Use \begin{verbatim} with results='asis' to protect the underscores. This will dump your output into a verbatim environment.

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

      using verbatim

    2. 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 use 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).

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

      using 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. I think there are other packages that can do this too but for the moment they escape me. You use results='asis' here. (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)
      @                 
      

      using xtable

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to call a library function that sometimes won't terminate within a given
For a application with a database in C#, I need to call a function
Say you have a ServiceCall database table that records down all the service calls
I have a PHP application that has passwords stored in the database as the
I have an MVC 3 web app that has an Action which I need
There's this problem that has been bugging me all day now, and I hoped
I writing a PHP script program under Linux. In the script, I need call
Need to call a filter function on some options based on a radio selected
I need to call Ghostscript in one of our cgi to convert a PDF
I need to call a method and pass an object from my custom UITableViewClass

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.