Skip to content

Commit

Permalink
use base pipe
Browse files Browse the repository at this point in the history
  • Loading branch information
sheridar committed Dec 8, 2023
1 parent 1bd6772 commit 9e32861
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 87 deletions.
50 changes: 25 additions & 25 deletions _posts/2023-12-13-class-11-programming-pt-1/programming-pt1.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,10 @@ brauer_gene_exp <- read_csv("data/brauer_gene_exp.csv.gz")
```

```{r, fig.width = 6, fig.height = 4.5, echo = F}
gg_data <- brauer_gene_exp %>%
gg_data <- brauer_gene_exp |>
filter(systematic_name == "YDL104C") # Filter for gene of interest
gg_data %>%
gg_data |>
ggplot(aes(rate, expression, color = nutrient)) + # Create scatter plot
geom_point(size = 2) +
facet_wrap(~ nutrient) + # Create separate plot for each nutrient
Expand All @@ -213,10 +213,10 @@ plot_expr <- function(input, sys_name = "YNL049C") {

```{r}
plot_expr <- function(input, sys_name = "YNL049C") {
gg_data <- input %>%
gg_data <- input |>
filter(systematic_name == sys_name)
gg_data %>%
gg_data |>
ggplot(aes(rate, expression, color = nutrient)) +
geom_point(size = 2) +
facet_wrap(~ nutrient) +
Expand All @@ -234,8 +234,8 @@ p <- plot_expr(
sys_name = "YDL104C"
)
# You can also use the %>% pipe with your custom functions
p <- brauer_gene_exp %>%
# You can also use the |> pipe with your custom functions
p <- brauer_gene_exp |>
plot_expr(sys_name = "YDL104C")
p
Expand All @@ -250,13 +250,13 @@ p

```{r}
plot_expr <- function(input, sys_name) {
gg_data <- input %>%
gg_data <- input |>
filter(systematic_name == sys_name)
plot_title <- gg_data$name[1]
plot_sub <- gg_data$MF[1]
gg_data %>%
gg_data |>
ggplot(aes(rate, expression, color = nutrient)) +
geom_point(size = 2) +
labs(title = plot_title, subtitle = plot_sub) +
Expand All @@ -271,7 +271,7 @@ plot_expr <- function(input, sys_name) {
<br>

```{r, fig.width = 6, fig.height = 4.5}
brauer_gene_exp %>%
brauer_gene_exp |>
plot_expr("YDL104C")
```

Expand Down Expand Up @@ -349,15 +349,15 @@ x %in% y | x is present in y

```{r, eval = F}
plot_expr <- function(input, sys_name) {
gg_data <- input %>%
gg_data <- input |>
filter(systematic_name == sys_name)
plot_title <- gg_data$name[1]
plot_sub <- gg_data$MF[1]
????
gg_data %>%
gg_data |>
ggplot(aes(rate, expression, color = nutrient)) +
geom_point(size = 2) +
labs(title = plot_title, subtitle = plot_sub) +
Expand All @@ -371,7 +371,7 @@ plot_expr <- function(input, sys_name) {

```{r}
plot_expr <- function(input, sys_name) {
gg_data <- input %>%
gg_data <- input |>
filter(systematic_name == sys_name)
plot_title <- gg_data$name[1]
Expand All @@ -381,7 +381,7 @@ plot_expr <- function(input, sys_name) {
plot_title <- sys_name
}
gg_data %>%
gg_data |>
ggplot(aes(rate, expression, color = nutrient)) +
geom_point(size = 2) +
labs(title = plot_title, subtitle = plot_sub) +
Expand All @@ -395,7 +395,7 @@ plot_expr <- function(input, sys_name) {
<br>

```{r, fig.width = 6, fig.height = 4.5}
brauer_gene_exp %>%
brauer_gene_exp |>
plot_expr("YNL095C")
```

Expand Down Expand Up @@ -469,7 +469,7 @@ plot_expr <- function(input, sys_name) {
stop("sys_name must be a string!")
}
gg_data <- input %>%
gg_data <- input |>
filter(systematic_name == sys_name)
plot_title <- gg_data$name[1]
Expand All @@ -479,15 +479,15 @@ plot_expr <- function(input, sys_name) {
plot_title <- sys_name
}
gg_data %>%
gg_data |>
ggplot(aes(rate, expression, color = nutrient)) +
geom_point(size = 2) +
labs(title = plot_title, subtitle = plot_sub) +
facet_wrap(~ nutrient) +
theme(legend.position = "none")
}
brauer_gene_exp %>%
brauer_gene_exp |>
plot_expr("YDL104C")
```

Expand All @@ -506,7 +506,7 @@ plot_expr <- function(input, sys_name) {
stop( ???? )
}
gg_data <- input %>%
gg_data <- input |>
filter(systematic_name == sys_name)
plot_title <- gg_data$name[1]
Expand All @@ -516,7 +516,7 @@ plot_expr <- function(input, sys_name) {
plot_title <- sys_name
}
gg_data %>%
gg_data |>
ggplot(aes(rate, expression, color = nutrient)) +
geom_point(size = 2) +
labs(title = plot_title, subtitle = plot_sub) +
Expand All @@ -539,7 +539,7 @@ plot_expr <- function(input, sys_name) {
stop("sys_name not found in input data!")
}
gg_data <- input %>%
gg_data <- input |>
filter(systematic_name == sys_name)
plot_title <- gg_data$name[1]
Expand All @@ -549,7 +549,7 @@ plot_expr <- function(input, sys_name) {
plot_title <- sys_name
}
gg_data %>%
gg_data |>
ggplot(aes(rate, expression, color = nutrient)) +
geom_point(size = 2) +
labs(title = plot_title, subtitle = plot_sub) +
Expand Down Expand Up @@ -588,8 +588,8 @@ rescale_vec(a, na.rm = T)
```{r, fig.width = 6, fig.height = 4.5}
# A cumbersome way
plot_expr <- function(input, sys_name, pt_size = 2, pt_shape = 1, pt_alpha = 1) {
input %>%
filter(systematic_name == sys_name) %>%
input |>
filter(systematic_name == sys_name) |>
ggplot(aes(rate, expression, color = nutrient)) +
geom_point(size = pt_size, shape = pt_shape, alpha = pt_alpha) +
facet_wrap(~ nutrient) +
Expand All @@ -598,8 +598,8 @@ plot_expr <- function(input, sys_name, pt_size = 2, pt_shape = 1, pt_alpha = 1)
# With the ellipsis
plot_expr <- function(input, sys_name, ...) {
input %>%
filter(systematic_name == sys_name) %>%
input |>
filter(systematic_name == sys_name) |>
ggplot(aes(rate, expression, color = nutrient)) +
geom_point(...) +
facet_wrap(~ nutrient) +
Expand Down
Loading

0 comments on commit 9e32861

Please sign in to comment.