I am also using knitr version 1.5, and the second option you offered works fine for me.
Here, the only R code in the document is stored in a named unevaluated chunk in "child.Rnw"
:
<<xref, eval=FALSE, echo=FALSE, results="hide">>=
d <- 1:10
d
@
That file and its chunk are read into the main file, "main.Rnw"
, by an initial chunk that uses the child="filename"
option. A second chunk evaluates the code:
\documentclass{article}
\begin{document}
<<child, child="child.Rnw", eval=TRUE>>=
@
<<internal-ref, eval=TRUE>>=
<<xref>>
@
\end{document}
It knits just fine, and more importantly, doing purl("main.Rnw")
produces a tangled file "main.R"
that includes all of the R code. "main.R"
looks like this:
## ----child, child="child.Rnw", eval=TRUE---------------------------------
## ----xref, eval=FALSE, echo=FALSE, results="hide"------------------------
## d <- 1:10
## d
## ----internal-ref, eval=TRUE---------------------------------------------
d <- 1:10
d
I haven't tried running this as a vignette, but since it's not missing any of the source code, it looks it should at least solve your proximal problem...