Skip to content

Commit

Permalink
Add quarto-webr to publish.yml. Add tutorials pages.
Browse files Browse the repository at this point in the history
  • Loading branch information
admash committed Jun 18, 2024
1 parent d2f8a81 commit a21be58
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ jobs:
- name: Set up Quarto
uses: quarto-dev/quarto-actions/setup@v2

- name: Install quarto-webr
run: quarto add coatless/quarto-webr

- name: Render and Publish
uses: quarto-dev/quarto-actions/publish@v2
with:
Expand Down
2 changes: 2 additions & 0 deletions _quarto.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ website:
- href: events.qmd
text: Events
- resources.qmd
- href: tutorials.qmd
text: Tutorials



Expand Down
95 changes: 95 additions & 0 deletions functions.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
---
title: "Figuring out functions"
format: html
engine: knitr
filters:
- webr
---
**Note: You can run selected lines of R code by pressing CTRL-Enter after selecting them.**

# Quick Answers

::: panel-tabset

# How do I make a function in R?

Notes:

* Replace anything starting with `my` with names meaningful to your analysis.
* You can have zero or more arguments(inputs) to your function
* To return more than one object, put them in a list then return the list.

```{webr-r}
myFunction <- function(myArg1, myArg2){
# Do something with myArg1 and myArg2
# and the result of interest in an object
# (e.g. vector, list or data frame)
myResult <- myArg1 * myArg2
# Return the result object
return(myResult)
}
x <- 5
y <- 10
z <- myFunction(x,y)
print(z)
```

:::

# What is a function?

A function is block of R code can be stored in an object, and can then be run using the name of the object. Functions can be given zero or more R objects to use when running the code block.

Running a function is often referred to as "calling the function". E.g. "You can perform linear regression by calling the lm() function."

Examples of common functions in R include `mean()`, `lm()`, and `summary()`.

## Parts of a function
A function has two core parts: its *body*, its argument list (technically called its *formals*).

- The body of a function contains the code that the function will run. This is usually one or more R expressions, typically followed by a return() statement.

- The argument list contains a list of objects (usually data) that will be made available to the function when it is called.

Here are three examples:

::: panel-tabset

# mySum()

```{webr-r}
# a,b is function's argument list
mySum <- function(a, b){
# the next two lines are the function's body
c <- a + b
return(c)
}
```

# greeting()
```{webr-r}
# This function has an empty argument list
greeting <- function(){
# The function body contains only the next line. There is no return() statement.
print("Greetings!")
}
```

# mean()
```{webr-r}
# This function takes a single argument, x, a numeric vector
runRegression <- function(dataset){
# The body of this function performs linear regression
# and returns the adjusted R-squared value.
fit <- lm(y ~ x, data=dataset)
fit.sum <- fit |> summary()
return(fit.sum$adj.r.squared)
}
```

:::


7 changes: 7 additions & 0 deletions tutorials.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
title: "RTromsø Tutorials"
---

Here is a list of tutorials provided by RTromsø:

- [Figuring out functions](functions.qmd) - Introduction to using and writing functions in R.

0 comments on commit a21be58

Please sign in to comment.