I am using Rmarkdown for a project for the first time and one thing that is bugging me is the speed at which cells which are just function definitions run.

Below is an MWE function with lots of lines defined in a cell. It takes about a second to run when I click "run cell" in the rmarkdown viewer. Why? R parses very fast when reading from a script, and there is no evaluation happening here.

The parsing speed is worse for some larger functions I have written. Is there any way to have faster parsing?

```{r, echo = FALSE}
test_fun = function(){
df = tibble(a = runif(1000), b = runif(1000))
df = df %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b)
}
```

I'm pretty sure this is because in markdown its submitting through the console, which takes time in traditional console work, its different from sourcing the code direct. (i.e. this is not a markdown issue specifically. its more a interactive-console/batch-script difference. presumably you could do the same my putting source() in your markdown chunks.
I use tictoc library for timing

library(tictoc)
tic()
test_fun = function(){
df = tibble(a = runif(1000), b = runif(1000))
dfg <<- df %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b)
}
toc()
mytempfile <- paste0(tempfile(),".R")
writeLines(text = "test_fun = function(){
df = tibble(a = runif(1000), b = runif(1000))
dfg <<- df %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b) %>%
mutate(c = a + b)
}",con = mytempfile)
mys <- readLines(mytempfile)
tic()
source(mytempfile)
toc()

The fact that each line is entered into the REPL individually definitely bugs me as well, since it makes it hard to use the up arrows to scroll through the history.

I might explore using .R instead of .Rmd and using jupyterlab's terminal instead.