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

Question about knitting vs. running chunks ("Run All") #4

Closed
pat-s opened this issue Mar 10, 2017 · 8 comments
Closed

Question about knitting vs. running chunks ("Run All") #4

pat-s opened this issue Mar 10, 2017 · 8 comments

Comments

@pat-s
Copy link

pat-s commented Mar 10, 2017

When running all chunks of my .Rmd file ("Run All"), here() is set to the R-project root dir. All paths are relative to this directory. Let's call it "main-dir".

The .Rmd file is placed one dir below the project root, let's say in "main-dir/sub-dir". When I knit the .Rmd file, I get .local object errors because knitr uses "sub-dir" as root dir when knitting.

Currently my workaround is to use a second R-project in "sub-dir" to ensure that both knitting and running all chunks works with the same relativ paths (and that my folder structure does not get messy).

I wonder what I'm doing wrong here so that here() is not adjusting paths correctly in my case. I initially expected that here() is setting the working directory to the project root when knitting. Did I get this wrong? 😅

@pat-s pat-s changed the title Question about knitting vs. running chunks Question about knitting vs. running chunks ("Run All") Mar 10, 2017
@krlmlr
Copy link
Member

krlmlr commented Mar 10, 2017

Thanks. Could you please point me to your project, or attach an archive?

@pat-s
Copy link
Author

pat-s commented Mar 12, 2017

In this example "Run All" chunks works. Knitting does not.

When changing here("spring-school-hsdar/data") to here("data"), i.e. making path relatives to the .Rmd file, knitting works but "Run All" does not work anymore (because paths to .Rproj files are wrong).

I also played around with knitr::opts_knit$set(root.dir = here()) but did not succeed. Even setting knitr::opts_knit$set(root.dir = "/Users/pjs/Desktop/test") (i.e. to the fixed path of the .Rproj dir) does not work.

You will need devtools::install_github("humburg/reportmd") for the example.

(Maybe its also a simple knitr problem and does not apply to here)

Archive.zip

@krlmlr
Copy link
Member

krlmlr commented Mar 12, 2017

Thanks. I'm seeing correct behavior with a simple document that is rendered with rmarkdown::html_document, but I can't get your example to run. I've installed the reportMD package, but knitting complains that a package "prompt" is missing, which I can't find.

Here's the minimal file I'm using in my tests:

---
title: "Part 1: Data investigation & preprocessing"
author: "Patrick Schratz"
date: "`r format(Sys.time(), '%a %d %b %Y')`"
output:
  reportMD::multi_document:
    toc: true
    fig_caption: true
    highlight: dracula 
    css: R.css
    use_namespace: false
params:
  version: !r 
---

```{r loadPackages, include=FALSE, cache=FALSE}
## load additional packages in this chunk
library(here)
```

```{r}
here()
```

Can you confirm that it works correctly with the following file:

---
title: "Part 1: Data investigation & preprocessing"
author: "Patrick Schratz"
date: "`r format(Sys.time(), '%a %d %b %Y')`"
output:
  rmarkdown::html_document
params:
  version: !r 
---

```{r loadPackages, include=FALSE, cache=FALSE}
## load additional packages in this chunk
library(here)
```

```{r}
here()
```

@pat-s
Copy link
Author

pat-s commented Mar 12, 2017

Thanks for looking into it.
It should work if you delete 01_preprocessing_cache- there are some leftovers from my run in it. prompt can be found on Github.

I get the following error with here("spring-school-hsdar/data") using html_document:

Quitting from lines 76-78 (01_preprocessing.Rmd)
Error in if (x == "" | x == ".") { :
missing value where TRUE/FALSE needed
Calls: ... -> -> .rasterObjectFromFile

Using here("data") works. So same problem with html_document as with reportmd->

  • root dir of here() when knitting = .Rmd dir
  • root dir of here() when running chunks = .Rproj dir

Setting knitr::opts_knit$set(root.dir = foo) does not work.

@krlmlr
Copy link
Member

krlmlr commented Mar 12, 2017

Does my example file work for you? After deleting the cache I can run the first example, it works as expected both for "Run all" and "knit".

@pat-s
Copy link
Author

pat-s commented Mar 12, 2017

It works and it also works using reportMD::multi_document. I refer to the example I sent you.

However, I'm kind of lost right now. What does not work is the original source from which the example, which I sent to you, was extracted. It has the same files, the same code and the same directory structure.

Knitting there, I get

Quitting from lines 75-77 (01_preprocessing.Rmd)
Error in if (x == "" | x == ".") { :
missing value where TRUE/FALSE needed
Calls: ... -> -> .rasterObjectFromFile
In addition: Warning messages:
1: code chunks must not depend on the uncached chunk "listfiles"
2: code chunks must not depend on the uncached chunk "makeraster"
Execution halted

for both reportMD::multi_document or html_document. It again refers to

# list files
file <- here("spring-school-hsdar/data") %>% list.files(pattern = ".tif$", full.names = TRUE)

and knitting only works when using here("data").

You proofed that it works correctly and I can reproduce it on my local copy. So I assume that there is some nasty stuff going on in my source...(which I do not want you to bother with).

Thanks for your time checking the files etc! Much appreciated! I'll let you know if I'll ever find out what's going on here.

@pat-s pat-s closed this as completed Mar 12, 2017
@krlmlr
Copy link
Member

krlmlr commented Mar 12, 2017

I'm getting consistent results with the following example, too:

---
title: "Part 1: Data investigation & preprocessing"
author: "Patrick Schratz"
date: "`r format(Sys.time(), '%a %d %b %Y')`"
output:
  reportMD::multi_document:
    toc: true
    fig_caption: true
    highlight: dracula 
    css: R.css
    use_namespace: false
params:
  version: !r 
---

```{r loadPackages, include=FALSE, cache=FALSE}
## load additional packages in this chunk
library(here)
library(magrittr)
```

```{r listfiles}
# list files
file <- here("spring-school-hsdar/data") %>% list.files(pattern = ".tif$", full.names = TRUE)
```

Next, we will read in these files as 'raster bricks' into R. 

```{r makeraster, dependson='listfiles'}
raster <- raster::brick(file[1])
raster
```

I'd suggest you try to remove code from your document step by step until the problem disappears. And I'd definitely be interested in the cause of this weird problem!

@github-actions
Copy link

github-actions bot commented Dec 8, 2020

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary.

@github-actions github-actions bot locked and limited conversation to collaborators Dec 8, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants