Search code examples
rsweave

Remove brackets in Sweave output


I want to know if there is a way to print R object directly in a text (Sweave), but directly in a line of text.

I'd like to print this code

\documentclass{article}

\begin{document}
\SweaveOpts{concordance=TRUE}

YO MANNNNNNN 2+2 = 
<<here, results=tex, echo=FALSE>>=
2+2
@
YEAHHHH!

\end{document}

And I want an output similar to this:

YO MANNNNNNN 2+2 = 4 YEAHHHH! 

But, R is adding the answer brackets like this

YO MANNNNNNN 2+2 = [1] 4YEAHHHH! 

Solution

  • Code:

    \documentclass{article}
    
    \begin{document}
    % \SweaveOpts{concordance=TRUE}
    
    YO MANNNNNNN 2+2 = \Sexpr{2+2} YEAHHHH! 
    
    \end{document}
    

    n.b. For me commenting out \SweaveOpts{concordance=TRUE} worked just fine, but you made need to uncomment depending on your setup.

    Prints:

    YO MANNNNNNN 2+2 = 4 YEAHHHH!
    

    Update - extra info per comment...

    You can define an object within a 'chunk' to be later evaluated in line using \Sexpr{}

    <<somechunk, results='tex',echo=FALSE, results='hide'>>=
    x <- 2
    @
    
    Inline evaluation of x prints the number \Sexpr{x}.