4

So I am making an .rmd file to document the development of some functions I am building. I am working in R studio. I noticed when I typed

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

it resulted in this in the knitted document

## 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 had already done a good job commenting my code and really didn't want to have 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 am making a Rmarkdown file in R studio?

I should mention when I used the option to run just the individual 'chunk' in R studio it printed the function with the comments included so I think it must have something to do with the was the IDE defaults handle knitr.

1 Answers1

5

This is not a problem with knitr or the way you were using it nor the chunk options you used.

The problem is due to print.function() and it not having access to the source for the function and instead only having access to its parsed representation.

I suspect this is a function you have in a package which you have loaded? If so, one option is to source that function again and explicitly print() it. Make sure that getOptions("keep.source") is TRUE.

If you don't want to source a copy of the function into the workspace, you can source it into an environment and then print the 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'll probably want to attach to a position on the search path below your package so that the package code always gets called and doesn't cause you problems.

The reason there are no comments in code in installed packages is due to the option keep.source.pkgs, which defaults to FALSE and needs to be TRUE when the package is installed for it to have any effect. See ?options for more details on this one.

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
  • adding adding keep.source = TRUE when I sourced the package worked. Thanks – Cody Blakeney Jun 16 '15 at 15:34
  • @CodyBlakeney Right, but you probably don't want to be sourcing a package and trying to use it as a package in your global environment. Load the pkg as normal (via `library()`) and then use the `sys.source` example to source into an environment (just attach it below you pkg; run the search path (run `search()` and find where your pkg is, usually 2 (unless you loaded other pkgs after it) and just attach the environment to a position lower than that of the pkg. – Gavin Simpson Jun 16 '15 at 15:38