Search code examples
rlatexknitrsweavestargazer

Run R code inside stargazer function


I have a code chunk in a .Rnw file like this:

<<test, results = "asis">>=

stargazer(mtcars, notes = c($n$ = \\Sexpr{nrow(mtcars)})

@

When I use knitr to evaluate the R code and convert the .Rnw file to a .tex file, how can I get nrow(mtcars) to evaluate?


Solution

  • I believe you want something like this:

    \documentclass{article}
    
    \begin{document}
    
    <<test, results = "asis">>=
    library("stargazer")
    stargazer(mtcars, notes = paste('$n$ = ',nrow(mtcars)))
    @
    
    \end{document}
    

    You can't put an Sexpr inside a code chunk.