R notebook: rerunning chunks in different parts of code - r

Is there a way to rerun chunks in R notebook?
In knitr it is possible to rerun chunks simply by referencing their name. E.g. like this below, only the second chunk would output the result (source https://campus.datacamp.com/courses/reporting-with-r-markdown/chapter-two-embedding-code?ex=7).
```{r simple_sum, results = 'hide'}
2 + 2
```
```{r ref.label='simple_sum', echo = FALSE}
```
Is there a simple way to do the same thing in the R notebook environment?

Related

profvis stalling on function within markdown on linux

I would like to profile some functions in R, using profvis(), on a linux command line (outputting html)
I have tested this with the approach here, and it works. I saved the code in that link into a file named "test.Rmd". At the moment I am simply going into an R environment in linux and typing 'rmarkdown::render("test.Rmd")', and it produces a HTML file with the code chunks and the 'profvis' chart. Fine.
This is the code & function I want to profile;
title: "Untitled"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r packages}
library(data.table)
library(terra)
```
## profvis
This is an R Markdown document using profvis.
```{r settings}
# Functions used .
source("funcs.R")
```
```{r objects}
dt_a <- fread("a_table.csv")
dt_a <- fread("a_table.csv")
r_c <- rast("c_surface.tif")
```
```{r process}
myFunctoProf(year = 2020, arg_a = dt_a, arg_b = dt_b, arg_c = r_c,
req_string = "data/lookup_stuff.csv")
```
The above runs to completion in about 5 minutes, produces a HTML file, and outputs the results onto my computer (rasters). There are 3 further functions called within the myFunctoProf() function.
However, as soon as I put the profvis() function back in, as in the link above, the markdown seems to run indefinitely, it never completes and never errors and doesn't produce raster outputs or a HTML file.
```{r process}
profvis({
myFunctoProf(year = 2020, arg_a = dt_a, arg_b = dt_b, arg_c = r_c,
req_string = "data/lookup_stuff.csv")
})
```
Is the profvis() function struggling with the nested functionality? one of the functions called within myFunctoProf() is used in 'lapply', does that matter?
edit: wondered if it might be to do with the linux cluster using cores/nodes when using lapply(subFunc) within the main function call.

r - trying to run knitr chunk - Error: unexpected input in "<<"

this is my first question here so please forgive if it is unclear/messy.
I'm trying to reproduce the example in this page, reading chunks from an .R script and running them in an .Rmd markdown using knitr. The read_chunk() command seems to execute properly, but when I try to reference the chunks with the <<some_chunk>> notation R gives me: Error: unexpected input in "<<".
I have W10x64 and I'm using R v 3.5.1 with RStudio v1.1.456
This is the content of the .R script:
# This is our external R script called example.R
# We're adding two chunks variablesXY and plotXY
## #knitr variablesXY
x<-1:100
y<-x+rnorm(100)
head(data.frame(x,y))
## #knitr plotXY
plot(x,y)
And this is inside the .Rmd markdown (I got the error when I try to call <<variablesXY>>):
```{r}
library(knitr)
```
# read chunk (does not run code)
```{r echo=FALSE}
read_chunk('example.R')
```
# run the variablesXY chunk and use the variables it creates
```{r first}
<<variablesXY>>
head(data.frame(x,y))
```
# run the plotXY chunk and create the plot
```{r second}
<<plotXY>>
```

Prevent Rmd code chunk from running by using variable when in interactive mode

When I knit this example .Rmd, the second chunk---as expected---does not evaluate but if I run all chunks interactively, the second chunk executes. What's the cleanest way to accomplish the equivalent of eval=FALSE for interactively running all chunks?
```{r}
RUN <- FALSE
```
```{r eval=RUN}
print("Code ran!")
```

Modularized R markdown structure

There are a few questions about this already, but they are either unclear or provide solutions that don't work, perhaps because they are outdated:
Proper R Markdown Code Organization
How to source R Markdown file like `source('myfile.r')`?
http://yihui.name/knitr/demo/externalization/
Modularized code structure for large projects
R Markdown/Notebook is nice, but the way it's presented, there is typically a single file that has all the text and all the code chunks. I often have projects where such a single file structure is not a good setup. Instead, I use a single .R master file that loads the other .R files in order. I'd like to replicate this structure using R Notebook i.e. such that I have a single .Rmd file that I call the code from multiple .R files from.
The nice thing about working with a project this way is that it allows for the nice normal workflow with RStudio using the .R files but also the neat output from R Notebook/Markdown without duplicating the code.
Minimal example
This is simplified to make the example as small as possible. Two .R files and one master .Rmd file.
start.R
# libs --------------------------------------------------------------------
library(pacman)
p_load(dplyr, ggplot2)
#normally load a lot of packages here
# data --------------------------------------------------------------------
d = iris
#use iris for example, but normally would load data from file
# data manipulation tasks -------------------------------------------------
#some code here to extract useful info from the data
setosa = dplyr::filter(d, Species == "setosa")
plot.R
#setosa only
ggplot(setosa, aes(Sepal.Length)) +
geom_density()
#all together
ggplot(d, aes(Sepal.Length, color = Species)) +
geom_density()
And then the notebook file:
notebook.Rmd:
---
title: "R Notebook"
output:
html_document: default
html_notebook: default
---
First we load some packages and data and do slight transformation:
```{r start}
#a command here to load the code from start.R and display it
```
```{r plot}
#a command here to load the code from plot.R and display it
```
Desired output
The desired output is that which one gets from manually copying over the code from start.R and plot.R into the code chunks in notebook.Rmd. This looks like this (some missing due to lack of screen space):
Things I've tried
source
This loads the code, but does not display it. It just displays the source command:
knitr::read_chunk
This command was mentioned here, but actually it does the same as source as far as I can tell: it loads the code but displays nothing.
How do I get the desired output?
The solution is to use knitr's chunk option code. According to knitr docs:
code: (NULL; character) if provided, it will override the code in the
current chunk; this allows us to programmatically insert code into the
current chunk; e.g. a chunk option code =
capture.output(dump('fivenum', '')) will use the source code of the
function fivenum to replace the current chunk
No example is provided, however. It sounds like one has to feed it a character vector, so let's try readLines:
```{r start, code=readLines("start.R")}
```
```{r plot, code=readLines("start.R")}
```
This produces the desired output and thus allows for a modularized project structure.
Feeding it a file directly does not work (i.e. code="start.R"), but would be a nice enhancement.
For interoperability with R Notebooks, you can use knitr's read_chunk method as described above. In a notebook, you must call read_chunk in the setup chunk; since you can run notebook chunks in any order, this ensures that the external code will always be available.
Here's a minimal example of using read_chunk to bring code from an external R script into a notebook:
example.Rmd
```{r setup}
knitr::read_chunk("example.R")
```
```{r chunk}
```
example.R
## ---- chunk
1 + 1
When you execute the empty chunk in the notebook, code from the external file will be inserted, and the results displayed inline, as though the chunk contained that code.
As per my comment above, I use the here library to work with projects in folders:
```{ r setup, echo=FALSE, message=FALSE, warning=FALSE, results='asis'}
library(here)
insert <- function(filename){
readLines(here::here("massive_report_folder", filename))
}
```
and then each chunk looks like
```{ r setup, echo=FALSE, message=FALSE, warning=FALSE,
results='asis', code=insert("extra_file.R")}
```

preventing a chunk run in rmarkdown

I am using Rmarkdown and Knitr using Rstudio.
The following both print script and output to html.
```{r}
summary(cars)
```
However the following will only print the output, that is embedded plot.
```{r, echo=FALSE}
plot(cars)
```
I have situation different than above, I want to present the script but should not run in html as this will take very long time (hours if not days) to run. So I just did was put comment sign.
```{r}
#summary(cars)
```
But I need a better way to do this - Is there any better way presenting script without running it.
eval = FALSE
Checkout The R Markdown Cheat Sheet http://blog.rstudio.org/2014/08/01/the-r-markdown-cheat-sheet/
It summarizes the options for code chunks

Resources