I need to create documents containing lots of small bits of R code, which are not to be sent to R via knitr. Ideally, it should work very much like code is formatted here at Stackexchange. It should be set in ttfamily and not break across lines. I'm not sure that a gray background is necessary. I'd prefer not to use \begin{environment} and \end{environment}, because that's cumbersome when the code is just a couple words.
It seems like the trickiest part might be that some special characters should loose their specialness. Specifically, "$" and "_", which are commonly used in R code shouldn't have to be escaped.
There is a solution to a similar question here, but that's taking the \begin{}\end{} approach, so it won't work here.
Something like \code{my code} would work better. I have the beginnings of a macro with this:
\newcommand{\nbcode}[1]{\mbox{\texttt{#1}}}
It'd be great if the solution to this is a package I haven't heard of.