Skip to content

Commit

Permalink
Merge pull request #51 from reconhub/update-pkg
Browse files Browse the repository at this point in the history
Update package
  • Loading branch information
zkamvar authored Nov 16, 2018
2 parents afd6b4e + 4d5dbdc commit 2099ffd
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 80 deletions.
6 changes: 2 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ ByteCompile: true
Imports:
knitr,
rmarkdown,
automagic,
checkpoint,
htmltools,
htmlwidgets,
snakecase,
yaml,
usethis (>= 1.3.0.9000),
usethis (>= 1.4.0),
stringr,
glue,
jsonlite,
Expand All @@ -34,5 +34,3 @@ License: MIT + file LICENSE
Suggests:
testthat,
covr
Remotes:
r-lib/usethis
6 changes: 3 additions & 3 deletions R/get_and_update_dependencies.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
#' @return Used for pure side effect
#' @export
get_and_update_dependencies <- function(dir = "content/post/") {
automagic::make_deps_file(dir)
automagic::automagic(dir)
}
pkgs <- checkpoint::scanForPackages("content/post", use.knitr = TRUE)$pkgs
install.packages(pkgs)
}
23 changes: 19 additions & 4 deletions R/render_new_rmds_to_md.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@
#'
#' @param dir Directory to search for new Rmds in. Defaults to the blog post location of Hugo.
#' @param build Selection criteria for which Rmds to convert to md, options are c("all", "old and new", "old", "new")
#' @param when `build = "old"` or `build = "old and new"`, this number will avoid re-building md files that are fewer than `tol` seconds old.
#' @param dry_run When `TRUE`, targets are printed, but not rendered. Defaults to `FALSE`.
#'
#' @return Used for pure side effect
#' @return Used for pure side effect. If `dry_run = TRUE`, then the list of targets to be rendered will be printed.
#' @export
render_new_rmds_to_md <- function(dir = "content/post",
build = "new") {
build = "new",
tol = 1,
dry_run = FALSE) {
match.arg(build, c("all", "old and new", "old", "new"))
content = dir
root <- rprojroot::has_file("DESCRIPTION")
content <- root$find_file(dir)

# get info about all files
info <- fs::dir_info(content)
Expand All @@ -29,7 +34,7 @@ render_new_rmds_to_md <- function(dir = "content/post",
# Rmd with a too old md
dplyr::left_join(rmds, mds, by = "slug",
suffix = c("_rmd", "_md")) %>%
dplyr::filter(.data$change_time_md < .data$change_time_rmd) %>%
dplyr::filter((.data$change_time_rmd - .data$change_time_md) > tol) %>%
dplyr::pull(.data$path_rmd) -> too_old

if(build == "all"){
Expand All @@ -46,13 +51,23 @@ render_new_rmds_to_md <- function(dir = "content/post",
}
pv <- rmarkdown::pandoc_version() >= package_version("2.0.0")
variant <- if(pv) "gfm" else "markdown_github"
figfile <- if (pv) "figure-gfm" else "figure-markdown_github"
# build only the ones to be built
if(length(to_build) > 0){
if (dry_run) {
return(to_build)
}
for (b in to_build) {
rmd <- b
rmarkdown::render(rmd,
rmarkdown::md_document(variant = variant,
preserve_yaml = TRUE ))
cpath <- gsub("\\.Rmd", "_files", rmd)
spath <- gsub("content", "static", cpath)
fs::file_move(fs::dir_ls(file.path(cpath, figfile)),
file.path(spath, figfile)
)

}
}else{
message("Nothing to build, all .md up-to-date")
Expand Down
12 changes: 6 additions & 6 deletions R/save_and_use_widget.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
#' @return HTML code for iframe of htmlwidget
#' @export
save_and_use_widget <- function(x, filename) {
dir.create("widgets", showWarnings = FALSE)
origwd <- getwd()
setwd("widgets")
htmlwidgets::saveWidget(x, filename, selfcontained = TRUE)
setwd(origwd)
root <- rprojroot::has_file("DESCRIPTION")
the_dir <- root$find_file("static", "post", "widgets")
the_file <- root$find_file("static", "post", "widgets", filename)
dir.create(the_dir, showWarnings = FALSE)
htmlwidgets::saveWidget(x, the_file, selfcontained = TRUE)
htmltools::tags$iframe(src = file.path("widgets", filename),
width = "100%",
height = "500px")
}
}
18 changes: 9 additions & 9 deletions content/post/tutorial-post-creation.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ If you're new to git and GitHub [check out this nifty guide](http://happygitwith

The general workflow would include the following steps:

1. **Fork the project** from the GitHub *RECON learn* project:
1. [**Fork the project**](https://github.com/reconhub/learn/fork) from the GitHub *RECON learn* project:

This will create a copy of the project in your own GitHub account. You will need to clone this archive, and make the modifications there. You `git clone` would look like:

Expand All @@ -56,22 +56,22 @@ If your GitHub user name is `johnsnow`.

## Creating posts <a name="post-creation"></a>

Practicals, tutorials, case studies are contributed as [R Markdown](http://rmarkdown.rstudio.com/) (`.Rmd`) documents and generated markdown ready for conversion as `.md` documents. They are stored in `content/post`. The best way to create a new document is to use the `create_post` function. For instance, this post was created using:
Practicals, tutorials, case studies are contributed as [R Markdown](http://rmarkdown.rstudio.com/) (`.Rmd`) documents and generated markdown ready for conversion as `.md` documents. They are stored in `content/post`. The best way to create a new document is to use the `create_post()` function. For instance, this post was created using:

```r
```{r eval = FALSE}
learn::create_post(title = "How to create a new post for the site",
slug = "post-creation",
category = "tutorials",
author = "Locke Data")
```

See `?create_post` to see further parameters. `create_post` creates and opens the Rmd file, creates a default .bib and a blank place-holder image that you can replace with your own header image.
See `?create_post()` to see further parameters. `create_post()` creates and opens the Rmd file, creates a default .bib and a blank place-holder image that you can replace with your own header image.

### Conventions

File-naming conventions are as follows:

- start with `practical` for practicals, `study` for case studies (handled by `create_post`)
- start with `practical` for practicals, `study` for case studies (handled by `create_post()`)
- use an informative slug.
- use lower case, no special characters
- be hypen-separated ("-")
Expand All @@ -86,7 +86,7 @@ For instance, for a practical using a SEIR model for influenza data:

The YAML header is the beginning of the `Rmd` document, within the `---`. For instance:

``` r
```{r eval = FALSE}
---
title: Phylogenetic tree reconstruction
author: Thibaut Jombart
Expand All @@ -110,12 +110,10 @@ Fields are mostly self-explanatory, and can be adapted to your needs. The date s

### Referencing packages

You can load packages or use functions via the typical methods `library(tidyverse)`, `require(magrittr)`, `dplyr::mutate()` and folks can run `learn::get_and_update_dependencies()` file to get all packages mentioned in the `content/posts` directory.
You can load packages or use functions via the typical methods `library("tidyverse")`, `require("magrittr")`, `dplyr::mutate()` and folks can run `learn::get_and_update_dependencies()` file to get all packages mentioned in the `content/posts` directory.

This means you do not need to have mandatory install statements - you can make these `eval=FALSE`.

NB. The `automagic` package used to get dependencies currently does not like `library("tidyverse")` so remove those speechmarks when writing your post!

### Storing images

The **image** will be the image associated with the document on the website. We try using natural, high-resolution, evocative images having a link, if only figurative, with the topic covered. These images are stored in `static/img/highres/`. Do not forget to add and push this file as well, as it will be required for your post to be successfully integrated. The path to the file provided in the header assumes `static/` as root folder (see example above), so that the right path will look like: `img/highres/your-image.jpg`.
Expand Down Expand Up @@ -166,6 +164,8 @@ Once a pull request (PR) has been made against [https://github.com/reconhub/lear

- Thibaut Jombart: precisions on cloning and PR deploys

- Zhian N. Kamvar: Clarificaton of rendering and forking

Contributions are welcome via [pull requests](https://github.com/reconhub/learn/pulls).


Expand Down
Loading

0 comments on commit 2099ffd

Please sign in to comment.