Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set wd rather than using root= in vignettes #117

Merged
merged 4 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 3 additions & 28 deletions vignettes/collaboration.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,8 @@ vignette: >
---

```{r, include = FALSE}
dir_tree <- function(path, sub = ".", ...) {
withr::with_dir(path, fs::dir_tree(sub, ...))
}

lang_output <- function(x, lang) {
writeLines(c(sprintf("```%s", lang), x, "```"))
}
r_output <- function(x) lang_output(x, "r")
yaml_output <- function(x) lang_output(x, "yaml")
plain_output <- function(x) lang_output(x, "plain")
orderly_file <- function(...) {
system.file(..., package = "orderly2", mustWork = TRUE)
}
source("common.R")

knitr::opts_chunk$set(
collapse = TRUE)

path <- tempfile()
fs::dir_create(path)
path_git <- file.path(path, "git")
Expand All @@ -43,7 +28,7 @@ sha <- gert::git_commit("initial", author = user, committer = user,
if (gert::git_branch(repo = path_git) == "master") {
gert::git_branch_move("master", "main", repo = path_git)
}

gert::git_clone(path_git, path_alice)
gert::git_clone(path_git, path_bob)
gert::git_clone(path_git, path_server)
Expand All @@ -54,16 +39,6 @@ knitr::opts_hooks$set(as = function(options) {
options$class.output <- options$as
options
})

.here <- getwd()
knitr::knit_hooks$set(inwd = function(before, options) {
if (before) {
setwd(options$inwd)
} else {
setwd(.here)
}
invisible()
})
```

<!--
Expand Down Expand Up @@ -197,7 +172,7 @@ Bob can now query for packets available on the server:

```{r, as = "bob", inwd = path_bob}
orderly2::orderly_metadata_extract(
name = "data",
name = "data",
options = list(allow_remote = TRUE, pull_metadata = TRUE))
```

Expand Down
50 changes: 50 additions & 0 deletions vignettes/common.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
## Common support code for vignettes. This will not be echoed to the
## user, so be sure not to define functions here that they might want
## to use.
##
## Typically, include this in the Rmd within a block like:
##
## ```{r, include = FALSE}
## ...
## ```

dir_tree <- function(path, sub = ".", ...) {
withr::with_dir(path, fs::dir_tree(sub, ...))
}

lang_output <- function(x, lang) {
writeLines(c(sprintf("```%s", lang), x, "```"))
}
r_output <- function(x) {
lang_output(x, "r")
}
yaml_output <- function(x) {
lang_output(x, "yaml")
}
json_output <- function(x) {
lang_output(x, "json")
}
plain_output <- function(x) {
lang_output(x, "plain")
}
orderly_file <- function(...) {
system.file(..., package = "orderly2", mustWork = TRUE)
}

inline <- function(x) {
sprintf("`%s`", format(x))
}

knitr::opts_chunk$set(
collapse = TRUE)

.here <- getwd()
knitr::knit_hooks$set(inwd = function(before, options) {
if (before) {
setwd(options$inwd)
} else {
setwd(.here)
}
invisible()
}
)
72 changes: 26 additions & 46 deletions vignettes/dependencies.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,7 @@ vignette: >
---

```{r, include = FALSE}
dir_tree <- function(path, sub = ".", ...) {
withr::with_dir(path, fs::dir_tree(sub, ...))
}

lang_output <- function(x, lang) {
writeLines(c(sprintf("```%s", lang), x, "```"))
}
r_output <- function(x) lang_output(x, "r")
yaml_output <- function(x) lang_output(x, "yaml")
plain_output <- function(x) lang_output(x, "plain")
orderly_file <- function(...) {
system.file(..., package = "orderly2", mustWork = TRUE)
}

inline <- function(x) {
sprintf("`%s`", format(x))
}

knitr::opts_chunk$set(
collapse = TRUE)
source("common.R")
```

One of the core aims of `orderly2` is to allow collaborative analysis; to do this the end of one piece of work is an input for another piece of work, perhaps someone else's. To make this work in practice, one `orderly2` report can "depend" on some completed packet (or several completed packets) in order to pull in files as inputs.
Expand Down Expand Up @@ -70,7 +51,7 @@ writeLines(c(
'd <- readRDS("data.rds")',
'png("analysis.png")',
"plot(y ~ x, d)",
'dev.off()'),
"dev.off()"),
file.path(path, "src", "analysis", "orderly.R"))
```

Expand All @@ -87,15 +68,15 @@ r_output(readLines(file.path(path, "src/analysis/orderly.R")))

Here, we've used `orderly2::orderly_dependency()` to pull in the file `data.rds` from the most recent version (`latest()`) of the `data` packet, then we've used that file as normal to make a plot, which we've saved as `analysis.png` (this is very similar to the example from `vignette("introduction")`, to get us started).

```{r}
id1 <- orderly2::orderly_run("data", root = path)
id2 <- orderly2::orderly_run("analysis", root = path)
```{r, inwd = path}
id1 <- orderly2::orderly_run("data")
id2 <- orderly2::orderly_run("analysis")
```

When we look at the metadata for the packet created from the `analysis` report, we can see it has used `r inline(id1)` as its dependency:

```{r}
orderly2::orderly_metadata(id2, root = path)$depends
```{r, inwd = path}
orderly2::orderly_metadata(id2)$depends
```

(indeed it had to, there is only one copy of the `data` packet to pick from).
Expand All @@ -110,22 +91,22 @@ orderly2::orderly_init(path)

fs::dir_create(file.path(path, "src", "data"))
writeLines(c(
'orderly2::orderly_parameters(cyl = NULL)',
'd <- mtcars[mtcars$cyl == cyl, ]',
"orderly2::orderly_parameters(cyl = NULL)",
"d <- mtcars[mtcars$cyl == cyl, ]",
'saveRDS(d, "data.rds")'),
file.path(path, "src", "data", "orderly.R"))

fs::dir_create(file.path(path, "src", "analysis"))
writeLines(c(
'orderly2::orderly_parameters(cyl = NULL)',
'orderly2::orderly_dependency(',
"orderly2::orderly_parameters(cyl = NULL)",
"orderly2::orderly_dependency(",
' "data",',
' "latest(parameter:cyl == this:cyl)",',
' "data.rds")',
'd <- readRDS("data.rds")',
'png("analysis.png")',
"plot(mpg ~ disp, d)",
'dev.off()'),
"dev.off()"),
file.path(path, "src", "analysis", "orderly.R"))
```

Expand All @@ -141,10 +122,10 @@ r_output(readLines(file.path(path, "src/data/orderly.R")))

We can run this for several values of `cyl`:

```{r}
orderly2::orderly_run("data", list(cyl = 4), root = path)
orderly2::orderly_run("data", list(cyl = 6), root = path)
orderly2::orderly_run("data", list(cyl = 8), root = path)
```{r, inwd = path}
orderly2::orderly_run("data", list(cyl = 4))
orderly2::orderly_run("data", list(cyl = 6))
orderly2::orderly_run("data", list(cyl = 8))
```

Our follow-on analysis contains:
Expand All @@ -155,16 +136,16 @@ r_output(readLines(file.path(path, "src/analysis/orderly.R")))

Here the query `latest(parameter:cyl == this:cyl)` says "find the most recent packet where it's parameter "cyl" (`parameter:cyl`) is the same as the parameter in the currently running report (`this:cyl`).

```{r}
orderly2::orderly_run("analysis", list(cyl = 4), root = path)
```{r, inwd = path}
orderly2::orderly_run("analysis", list(cyl = 4))
```

## Interpreting errors

If your query fails to resolve a candidate it will error:

```{r, error = TRUE}
orderly2::orderly_run("analysis", list(cyl = 9000), root = path)
```{r, error = TRUE, inwd = path}
orderly2::orderly_run("analysis", list(cyl = 9000))
```

The error message here tries to be fairly self explanatory; we have failed to find a packet that satisfies our query` latest(parameter:cyl == this:cyl && name == "data")`; note that the report name `data` has become part of this query, so there are two conditions being matched on.
Expand All @@ -181,28 +162,27 @@ This tells you that your query can be decomposed into two subqueries `A` (the ma

You can also ask `orderly2` to explain any query for you:

```{r}
```{r, inwd = path}
orderly2::orderly_query_explain(
quote(latest(parameter:cyl == 9000)), name = "data", root = path)
quote(latest(parameter:cyl == 9000)), name = "data")
```

If you save this object you can explore it in more detail:

```{r}
```{r, inwd = path}
explanation <- orderly2::orderly_query_explain(
quote(latest(parameter:cyl == 9000)), name = "data", root = path)
quote(latest(parameter:cyl == 9000)), name = "data")
explanation$parts$B
```

(this would have worked with `rlang::last_error()$explanation$parts$A` too).

You can also use `orderly2::orderly_metadata_extract` to work out what values you might have looked for:

```{r}
```{r, inwd = path}
orderly2::orderly_metadata_extract(
name = "data",
extract = c(cyl = "parameters.cyl is number"),
root = path)
extract = c(cyl = "parameters.cyl is number"))
```

## Filtering candidates in other ways
Expand Down
Loading