This might be an edge case, but I'm trying to create a beamer presentation with knitr where I want to display a code chunk using different formulas as arguments for a function. I found that when using overlays, the tilde in the code chunks disappear. Is there a way to get them to display?
Here's a minimal reproducible example:
\documentclass{beamer}
\begin{document}
\begin{frame}[fragile]
\frametitle{Slide with overlay}
\only<1>{
<<notilde, eval = FALSE>>=
myfunction(data, formula = ~ x)
@
}
\only<2>{
<<notilde2, eval = FALSE>>=
myfunction(data, formula = y ~ x)
@
}
\end{frame}
\begin{frame}[fragile]
\frametitle{Slide without overlay}
<<tilde, eval = FALSE>>=
myfunction(data, formula = ~ x)
@
\end{frame}
\end{document}
The first frame of the presentation looks like this:

Any help is appreciated.
EDIT:
The slide without overlay looks like this:

To clarify, the syntax of <<...>> and @ is specific to the R package knitr. Anything between these symbols are interpreted by knitr as R code chunks and converted to latex to include syntax highlighting.