I don't think you need to go this far 
There is already a function in knitr to extract all the R code from a Rmd file. See ?knitr::purl. It uses the tangle = TRUE in knitr.
Here is an example of how to generate a R script from Rmd's chunks.
temp_rmd <- tempfile(fileext = ".Rmd")
xfun::write_utf8(c(
"---",
"title: test",
"output: html_document",
"---",
"",
"# a title",
"",
"```{r iris-dim}",
"dim(iris)",
"```",
""
), temp_rmd)
cat(xfun::read_utf8(temp_rmd), sep = "\n")
#> ---
#> title: test
#> output: html_document
#> ---
#>
#> # a title
#>
#> ```{r iris-dim}
#> dim(iris)
#> ```
temp_r <- tempfile(fileext = ".R")
knitr::purl(temp_rmd, output = temp_r)
#> processing file: C:\Users\chris\AppData\Local\Temp\RtmpcTXwV2\filec52c50c83709.Rmd
#> output file: C:\Users\chris\AppData\Local\Temp\RtmpcTXwV2\filec52c5b727f17.R
#> [1] "C:\\Users\\chris\\AppData\\Local\\Temp\\RtmpcTXwV2\\filec52c5b727f17.R"
cat(xfun::read_utf8(temp_r), sep = "\n")
#> ## ----iris-dim-----------------------------------------------------------------
#> dim(iris)
Created on 2020-04-07 by the reprex package (v0.3.0.9001)
The currently in writing Rmarkdown cookbook has a chapter on this feature:
Historically, there was a knitr example on this
You also have on example of what purl can be useful
You can then execute the script.
Hope it helps