This is because you're using the knit button in Rstudio which creates a new session then executes rmarkdown::render, in order to use locally defined function you have to call rmarkdown::render from the console.
Note: that this will block the console until the .Rmd file is rendered.
my.envir <- new.env(parent=baseenv()) local({ f <- function() { ... } #... }, my.envir) # OR source your file directly into my.envir source("ur.file.R", local = my.envir) rmarkdown::render(input, # path to the .Rmd file output_format = "html_document", # output type if not set it'll use the first one found in the yaml header output_file = NULL, # name of the output file by default it will be the # name of the .Rmd with the extension changed clean = TRUE, # set to FALSE if you want to keep intermediary files envir = my.envir, # this where the magic happens # if you don't want to modify the global env you can create your own environment and add the function to it quiet = FALSE # set to TRUE if you want to suppress the output )
EDIT following @KonardRudolph's comments:
It's better to source your file into the rmd itself, as the main goal of Rmarkdown is reproducible research.
```{r setup, include=FALSE} . . . source("path/to/file.R") ```