Feature comments not visible in knitr (r studio)

So, I am making a .rmd file to document the development of some of the functions that I am creating. I work in studio R. I noticed when I typed

    ```{r echo=TRUE, tidy=FALSE }
    createExamData
    ```

      

this led to this in a knitted paper

## function (directory) 
## {
##     files = list.files(directory)
##     files = files[grepl("i", files)]
##     files = substring(files[], 1, 4)
##     examData <- LoadData(directory)
##     nExams <- length(examData[[1]])
##     adjMatrixStd <- list(length = nExams)
##     for (i in 1:nExams) {
##         iExam <- examData[[1]][[i]]
##         iExam <- iExam[order(iExam[, 1]), ]
##         gExam <- examData[[2]][[i]]
##         gExam <- gExam[order(gExam[, 1]), ]
##         key <- examData[[3]][[i]]
##         adjMatrixStd <- ComputeStdAdjMatrix(gExam)
##         adjMatrixWt <- ComputeWeightedMatrix(iExam, gExam, key)
##         adjMatrixConv <- calculateConvinceMtd(iExam, gExam)
##         save(iExam, gExam, key, adjMatrixStd, adjMatrixWt, adjMatrixConv, 
##             file = paste(files[i], ".Rdata", sep = ""))
##     }
## }

      

I've already done a good job commenting out my code and really didn't want to rewrite my comments in the markdown document for every function I need to display. My question is, how do I get knitr to display my comments inside my functions if I'm doing Rmarkdown in R studio?

I should mention when I used the option to run only a single "chunk" in studio R, it was printing the function with comments, so I think it must have something to do with the fact that it was the IDE's default knitr handle.

+3


source to share


1 answer


This is not a problem with knitr, or the way you used it, or the parameters you used.

The problem is with print.function()

and does not have access to the source for the function and instead only has access to its parsed representation.

I suspect this is a feature that you have in the package you downloaded? If so, one option is to re-enable the feature and explicitly print()

. Make sure that getOptions("keep.source")

TRUE

.

If you don't want to copy the function to the workspace, you can submit it to the environment and then the print

version that is in the environment:



env <- attach(NULL, name = "myenv")
sys.source("~/work/git/permute/permute/R/shuffleSet2.R", env,
           keep.source = TRUE)
with(env, print(shuffleSet))

      

You probably want attach

to position in the search path below your package so that the package code is always called and does not cause problems.

The reason for the absence of comments in the code in the installed packages is due to the option keep.source.pkgs

, which by default is equal to FALSE

and must be TRUE

when the package is installed for it to have any effect. For details see ?options

.

+4


source







All Articles