Skip to content

Commit

Permalink
Merge branch 'NBISweden:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
gbdias authored Oct 15, 2024
2 parents 6192781 + 9ddcd68 commit 35f7478
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 60 deletions.
32 changes: 25 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,33 @@ If you are using command line, you can install `vd` and open and edit the file `

### Docker

A docker container is used in GitHub actions to build the website. The Dockerfile contains the image definition. To update the docker image, follow the steps below:
R packages needed to build the website and run the labs are all contained in a Docker container. To run docker container locally, follow instructions below:

:exclamation: Image is about 4.8 GB!

```
# pull the container
docker pull --platform=linux/amd64 ghcr.io/nbisweden/workshop-r:latest
# render whole website
docker run --platform=linux/amd64 --rm -u $(id -u ${USER}):$(id -g ${USER}) -v ${PWD}:/rmd ghcr.io/nbisweden/workshop-r:latest Rscript -e 'rmarkdown::render_site()'
# render one file
docker run --platform=linux/amd64 --rm -u $(id -u ${USER}):$(id -g ${USER}) -v ${PWD}:/rmd ghcr.io/nbisweden/workshop-r:latest Rscript -e 'rmarkdown::render("index.Rmd")'
```

To run RStudio server and develop in the browser, run;

```
docker run --platform=linux/amd64 --rm -e PASSWORD=rstudio -p 8788:8787 -v ${PWD}:/rmd ghcr.io/nbisweden/workshop-r:latest
```

Go to [http://localhost:8788/](http://localhost:8788/) or [http://0.0.0.0:8788](http://0.0.0.0:8788). Username is `rstudio` and password is `rstudio`. Change to folder `/rmd` to see your files.

To add new packages, you need to update `Dockerfile`, rebuild the container, test it and push it to repository. Make changes to the `Dockerfile` as needed. Then to rebuild and push the docker image, follow the steps below:

:exclamation: Remember to update the version number
:exclamation: Remember to render the whole website to make sure everything works

```
# build container and add tags
Expand All @@ -50,12 +74,6 @@ docker tag ghcr.io/nbisweden/workshop-r:1.1.0 ghcr.io/nbisweden/workshop-r:lates
docker login ghcr.io
docker push ghcr.io/nbisweden/workshop-r:1.1.0
docker push ghcr.io/nbisweden/workshop-r:latest
# run container locally
# render whole website
docker run --platform=linux/amd64 --rm -u $(id -u ${USER}):$(id -g ${USER}) -v ${PWD}:/rmd ghcr.io/nbisweden/workshop-r:latest Rscript -e 'rmarkdown::render_site()'
# render one file
docker run --platform=linux/amd64 --rm -u $(id -u ${USER}):$(id -g ${USER}) -v ${PWD}:/rmd ghcr.io/nbisweden/workshop-r:latest Rscript -e 'rmarkdown::render("index.Rmd")'
```

---
Expand Down
Binary file added data/slide_intro/num_pkgs.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 4 additions & 3 deletions home_content.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ This page contains links to different lectures (slides) and practical exercises
* [Intro to R (Slides)](slide_r_intro.html)
* [Intro to R environment (Slides)](slide_r_environment.html)
* [Intro to programming in R (Slides)](slide_r_programming_1.html)
* [Variables and Operators (Slides)](slide_elements_1.pdf)
* [Variables and Operators (Slides)](slide_r_elements_1.html)
* [Data types (Lab)](lab_datatypes.html)
* [Vectors and Strings (Slides)](slide_elements_2.pdf)
* [Matrices, Lists and Dataframes (Slides)](slide_elements_3.pdf)
* [Vectors and Strings (Slides)](slide_r_elements_2.html)
* [Matrices, Lists and Dataframes (Slides)](slide_r_elements_3.html)
* [Working with Vectors (Lab)](lab_vectors.html)
* [Dataframes (Lab)](lab_dataframes.html)
* [Loops and functions (Slides)](slide_r_elements_4.html)

**Data wrangling**

Expand Down
25 changes: 15 additions & 10 deletions home_precourse.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,26 @@ RStudio provides you with tools like code editor with highlighting, project mana

Extra R packages used in the workshop exercises (if any) are listed below. It is recommended that you install this in advance. Simply copy and paste the code into R.

```{r,eval=TRUE,chunk.title=NULL,echo=FALSE,comment="",class.output="r"}
# this code block reads package names from '_site.yml' and prints them as installation instruction.
```{r include=FALSE}
# this first chunk runs through the root directory, finds the installed packages across the files and prints them as installation instruction.
pkg <- yaml::read_yaml("_site.yml")
#Add to the pkg_discard object the packages you want to discard from the list
pkg<-unique(renv::dependencies()$Package)
pkg_discard<-c("mkteachr")
pkg_list<-pkg[!pkg %in% pkg_discard]
```

```{r echo=FALSE, warning=FALSE, chunk.title=NULL, class.output="r", comment="", r,eval=TRUE}
if(!is.null(pkg$packages$packages_cran_student)) {
cat("# install from cran\n")
cat(paste0("install.packages(c('",paste(pkg$packages$packages_cran_student,sep="",collapse="','"),"'))"))
cat(paste0("install.packages(c('",paste(pkg_list,sep="",collapse="','"),"'))"))
cat("\n")
}
if(!is.null(pkg$packages$packages_bioc_student)) {
cat("# install from bioconductor\n")
cat(paste0("BiocManager::install(c('",paste(pkg$packages$packages_bioc_student,sep="",collapse="','"),"'))"))
}
```

`r fa1("chevron-circle-right")` &nbsp; **Install Docker**
Expand Down
92 changes: 54 additions & 38 deletions slide_r_elements_4.Rmd
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: "Replication, Control Structures & Functions"
subtitle: "Elements of the R language"
author: "Marcin Kierczak and Nima Rafati"
author: "Marcin Kierczak, Nima Rafati, Miguel Redondo"
keywords: bioinformatics, course, scilifelab, nbis, R
output:
xaringan::moon_reader:
Expand Down Expand Up @@ -55,7 +55,7 @@ name: repeating_actions_1

# Repeating actions

Somtimes you want to repeat certain action several times.
Sometimes you want to repeat certain action several times.

There are few alternatives in R, for example:
- `for` loop
Expand All @@ -66,13 +66,19 @@ name: for_loop_0

# Repeating actions &mdash; for loop

One way to repeat an action is to use the **for-loop**
One way to repeat an action is to use the **for-loop**.

This is the general syntax:

```{r for.loop.general, eval=FALSE, echo=TRUE}
for (var in seq) {
expr
}
```
Where:
- var = variable that will take values from the sequence
- seq= sequence of values
- expr = expression to be executed

---
name: for_loop_1
Expand All @@ -82,17 +88,17 @@ Example.

```{r for.loop, echo=T}
for (i in 1:5) {
cat(paste('Performing operation no.', i), '\n')
cat(paste('Performing operation on no.', i), '\n')
}
```

--

A slight modification of the above example will skip odd indices.
A slight modification of the above example.

```{r for.loop2, echo=T}
for (i in c(2,4,6,8,10)) {
cat(paste('Performing operation no.', i), '\n')
cat(paste('Performing operation on no.', i), '\n')
}
```

Expand All @@ -106,9 +112,10 @@ Sometimes, we also want an external counter:
```{r for.loop.cnt, echo=T}
cnt <- 1
for (i in c(2,4,6,8,10)) {
cat(paste('Performing operation no.', cnt,
'on element', i), '\n')
cat(paste('Iteration', cnt,
'Performing operation on no.', i), '\n')
cnt <- cnt + 1
}
```

Expand All @@ -122,6 +129,11 @@ Say, we want to add 1 to every element of a vector:
```{r for.loop.ex1, echo=T}
vec <- c(1:5)
vec
```

```{r for.loop.ex2, echo=T}
for (i in vec) {
vec[i] <- vec[i] + 1
}
Expand Down Expand Up @@ -165,6 +177,37 @@ for (i in vec) {
proc.time() - ptm # for-loop
```


---
name: loops_avoid_growing

# Loops &mdash; avoid growing data

Avoid changing dimensions of an object inside the loop:

```{r avoid.growing, echo=T}
v <- c() # Initialize
for (i in 1:100) {
v <- c(v, i)
}
```

--

It is much better to do it like this:

```{r avoid.growing2, echo=T}
v <- rep(NA, 100) # Initialize with length
for (i in 1:100) {
v[i] <- i
}
```

--

Always try to know the size of the object you are going to create!


---
name: while_loop

Expand All @@ -174,14 +217,15 @@ There is also another type of loop in R, the **while loop** which is executed as
```{r loop.while, echo=T}
x <- 1
while (x < 5) {
cat(x, " ... ")
cat("x equals",x, "\n")
x <- x + 1
}
```

---
name: recursion

exclude: true
# Any questions so far?
<!-- # Recursion
When we explicitely repeat an action using a loop, we talk about **iteration**. We can also repeat actions by means of **recursion**, i.e. when a function calls itself. Let us implement a factorial $!$:
Expand Down Expand Up @@ -231,35 +275,7 @@ proc.time() - ptm
-->

---
name: loops_avoid_growing

# Loops &mdash; avoid growing data

Avoid changing dimensions of an object inside the loop:

```{r avoid.growing, echo=T}
v <- c() # Initialize
for (i in 1:100) {
v <- c(v, i)
}
```

--

It is much better to do it like this:

```{r avoid.growing2, echo=T}
v <- rep(NA, 100) # Initialize with length
for (i in 1:100) {
v[i] <- i
}
```

--

Always try to know the size of the object you are going to create!

---
name: if_clause

# Decisions, an if-clause
Expand Down
11 changes: 9 additions & 2 deletions slide_r_intro.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,22 @@ e.g. an agent-based simulation is a challenge
name: num_packages

# Ecosystem of R packages

<br>

```{r num_pkgs, echo=FALSE, fig.align='center', fig.height=7, fig.width=9, cache=TRUE, message=FALSE}

```{r num_pkgs, eval=FALSE, fig.align='center', fig.height=7, fig.width=9, message=FALSE, cache=TRUE, include=FALSE}
#I mute this chunk because it gives a readLines error in the 2024 course
#I add the figure instead below the chunk
#Maybe modify in the future
data <- mkteachr::update_repos_data()
gg <- mkteachr::plot_repos_data(data)
gg
```

<div style="text-align: center;">
<img src="data/slide_intro/num_pkgs.jpg" style="width:80%;">
</div>

<!-- --------------------- Do not edit this and below --------------------- -->

---
Expand Down

0 comments on commit 35f7478

Please sign in to comment.