Search code examples
rknitrr-markdownsweave

write within R function a latex chunk using knitR


I have the following problem; I use knitR within Rstudio to generate pdf. In the main file which is called: "master.Rnw" I call a long function called "trigono.R" :

<<trigono_R, echo=FALSE, warning=FALSE, cache=FALSE, eval=TRUE>>=

source("./trigono.R")  
@

now the question: is there any possibility to call within this R function a latex-chunk. That means within the following function:

trigono<- function(con = con, run.type = NULL){

normal R commands

open chunk Latex(

end chunk Latex)

normal R commands
}

have a Latex command or leave the R chunk <<... >>= ... @ and go back into Latex to write some sentences.

thanks a lot


Solution

  • I found by myself the answer and give it here although I cannot understand why I earned negative vote sometimes here is really frustrating!

    At first one needs 2 libraries:

    library(knitr)

    library(printr)

    you must use a child which contains your function. Let say within master.Rnw you call a child named trigono.Rnw

    <<childIntroduc, child='trigono.Rnw',results = "asis">>=
    @
    

    trigono.Rnw looks like:

    << myfunctions, echo=FALSE, eval=TRUE>>=
    normal R commands
    
    .
    .
    cat(" \\newline \\emph{The text which appears as a normal text in Latex and Pdf \\color{red} 2.5.6} \\newline")
    .
    .
    @