CrumpLab / vertical

R workflow for sharing psychological research
https://crumplab.github.io/vertical/
Other
35 stars 2 forks source link

vertical restores previously open Supplemental_1.Rmd #20

Closed mvuorre closed 4 years ago

mvuorre commented 4 years ago

There is something wrong in vertical_project(), leading to the following strange behavior:

It most definitely has to do with the fact that the supplemental file is created with usethis::use_article(), whereas the other .Rmd docs are created with rmarkdown::draft(). I have disabled restoring all documents and data from previous sessions so this is undesirable behavior.

> sessioninfo::session_info()
─ Session info ────────────────────────────────────────────────────────────────────────────────────────
 setting  value                       
 version  R version 3.6.1 (2019-07-05)
 os       macOS Catalina 10.15.2      
 system   x86_64, darwin15.6.0        
 ui       RStudio                     
 language (EN)                        
 collate  en_US.UTF-8                 
 ctype    en_US.UTF-8                 
 tz       America/New_York            
 date     2019-12-13                  

─ Packages ────────────────────────────────────────────────────────────────────────────────────────────
 package     * version    date       lib source                      
 assertthat    0.2.1      2019-03-21 [1] CRAN (R 3.6.0)              
 cli           2.0.0      2019-12-09 [1] CRAN (R 3.6.1)              
 crayon        1.3.4      2017-09-16 [1] CRAN (R 3.6.0)              
 fansi         0.4.0      2018-10-05 [1] CRAN (R 3.6.0)              
 fs            1.3.1      2019-05-06 [1] CRAN (R 3.6.0)              
 glue          1.3.1      2019-03-12 [1] CRAN (R 3.6.0)              
 packrat       0.5.0      2018-11-14 [1] CRAN (R 3.6.0)              
 Rcpp          1.0.3      2019-11-08 [1] CRAN (R 3.6.0)              
 rlang         0.4.2.9000 2019-12-13 [1] Github (r-lib/rlang@ec7c1ed)
 rstudioapi    0.10       2019-03-19 [1] CRAN (R 3.6.0)              
 sessioninfo   1.1.1      2018-11-05 [1] CRAN (R 3.6.0)              
 usethis       1.5.1      2019-07-04 [1] CRAN (R 3.6.0)              
 vertical    * 0.0.0.9100 2019-12-13 [1] local                       
 withr         2.1.2      2018-03-15 [1] CRAN (R 3.6.0)              

[1] /Library/Frameworks/R.framework/Versions/3.6/Resources/library
CrumpLab commented 4 years ago

Interesting, I'll take a look and see if I can spot anything. Just about to email you re: the manuscript

mvuorre commented 4 years ago

There are also some hacks in the build process that should be removed (e.g. it uses setwd() because create_package() doesn't otherwise know how to get into the newly created directory to add the other components.) There's some RStudio magic involved that I don't currently understand enough to fix it.

mvuorre commented 4 years ago

It gets even stranger. I just created a new vertical project, closed RStudio, and opened RStudio again. This time there was no problem. I then closed RStudio, deleted the directory of the new project and reopened RStudio. It now opened with the deleted Supplemental_1.Rmd file active, with the message "The file doesn't exist...".

CrumpLab commented 4 years ago

Am trying to reproduce the error this morning. I had RStudio set to load previous project and files, and with those settings, didn't have a problem. I unchecked both settings, then followed your steps and was able to reproduce the error. Reloading Rstudio opened a "none" project, with the supplementary file loaded...very strange, will see if I can track down the issue.

CrumpLab commented 4 years ago

Eliminating some possibilities:

Checked to see if this was purely a usethis::use_article() problem, doesn't seem to be.

  1. I made a new R package project from R studio template
  2. added a vignette using usethis::use_article()
  3. Closed R studio, and re-opened it.
  4. I got a blank R studio in a none project with no vignette loaded (as it should)

Suggests something about our build process

CrumpLab commented 4 years ago

No problem with vertical::init_vertical_project()...

  1. I created a new project from R studio (just a blank project)
  2. used vertical::init_vertical_project() to initialize a vertical project
  3. It worked properly, AND it did automatically load Supplemental_1.Rmd to the editor pane.
  4. However, closing and reopening R studio worked properly, Supplemental_1.Rmd was not loaded.

Suggests something about usethis::use_article() opening the vignettes in the editor window...almost as if it tries to open the file in the "none" project or something when building from RStudio template...

CrumpLab commented 4 years ago

Here's a link to the source code for usethis::use_article()

https://github.com/r-lib/usethis/blob/master/R/vignette.R

My hunch from above is the problem is related to usethis::use_article() attempting to open the new vignette in the editor window. This is hard coded, the code for use_article copied from the link above:

use_article <- function(name, title = name) {
  check_is_package("use_article()")

  path <- use_vignette_template("article.Rmd", name, title)
  use_build_ignore("vignettes/articles")

  invisible()
}

use_vignette_template <- function(template, name, title) {
  stopifnot(is_string(name))
  stopifnot(is_string(title))

  use_directory("vignettes")
  use_git_ignore(c("*.html", "*.R"), directory = "vignettes")
  use_dependency("rmarkdown", "Suggests")

  path <- path("vignettes", asciify(name), ext = "Rmd")

  data <- project_data()
  data$vignette_title <- title
  data$braced_vignette_title <- glue::glue("{{{title}}}")

  use_template(template,
    save_as = path,
    data = data,
    open = TRUE
  )

  path
}

The culprit appears to be use_template, which tries to open the vignette. My guess is our bug would be solved if we prevented the opening behavior in the first place. It makes sense to do this because in general there is no good reason to open this particular file for editing on creation of a vertical project.

We could copy these functions and provide modified versions in vertical with the desired behavior (only used on vertical project creation)...I'll also look into devtools to see if there is a function that creates a vignette template with option not to load it in the editor.

mvuorre commented 4 years ago

Great, I can look into this further later on as well. Just a brief comment: I don't think it's a good idea to copy code from another package. It will make maintaining vertical more problematic in the long run.

CrumpLab commented 4 years ago

Agreed on not copying code...regarding de-bugging I've been spinning and trying various things that didn't solve all the problems. I haven't pushed anything yet but have a few proposals:

  1. We can establish the path to the new project at the beginning:
path <- file.path(getwd(), path)

Then, create_package() makes the new folder and adds the R package skeleton

usethis::create_package(path, open = FALSE)

We can do the following to set the path for usethis functions:

usethis::proj_set(path)

At this point, use_git_ignore(), and use_data_raw() work properly. But, writing _pkgdown.yml does not (since the working directory wasn't set, the current method instead writes it to the parent directory of the new project, e.g., Desktop).

  1. I noticed that usethis has a method to include template files from a templates folder. I added a templates folder to try it out, so this works to write _pkgdown.yml to the new folder:
usethis::use_template(template = "_pkgdown.yml",
                        package = "vertical")

However, the remaining init_ functions don't work as intended. They write content to the parent folder, not to the project folder (again because setwd() was removed).

I guess for now my suggestion is to keep the setwd() hack in place, and rewrite it as:

setwd(path)

I haven't figured out a different method to let R studio know to write to the new folder. We could try writing everything to a temporary folder, and then copy all of that to a new project folder, but for now I don't really see the advantage of doing that.

  1. For the ghosted Supplementary_1.Rmd issue, I'm pretty confident this is caused by usethis, which has no way to prevent the new vignette from opening. Usually you would want it to open for editing, just not in this case. Rather than hacking their code we could submit an issue about this. Basically, using usethis::usearticle() in the context of setting up an R project template produces unintended behavior, and they could fix that function by allowing an option to prevent the vignette from being loaded in the editor.

edit, spoke to soon, I think we can get 3 done using use_template(), I have a working example, just need to see if I can fit it all together

CrumpLab commented 4 years ago

I just pushed a branch called "testbuild"

I think I solved the Supplementary.Rmd issue. I used usethis::use_template() instead, and specified some parameters to name the file, give it a title, etc. The result is the same as before except the file is not opened in the editor. I've tried closing and reopening RStudio, and that file is not loaded.

I also added the changes mentioned above, but kept the setwd(path) hack in place.

EDIT the branch had some bugs that should be fixed now

mvuorre commented 4 years ago

I can't check this right now but there is a function, on.exit() that could be placed somewhere in the function that initializes a vertical project, that could solve some of the problems.

CrumpLab commented 4 years ago

I think this problem is solved now, we can reopen this issue or create a new one if further change is needed.