DS4PS / cpp-528-spr-2021

https://ds4ps.org/cpp-528-spr-2021/
0 stars 0 forks source link

Adding Chapters to the website #48

Open malmufre opened 3 years ago

malmufre commented 3 years ago

Hello,

I am trying to add the chapters to my website and I started by adding my chapters to the analysis folder, then I have added .YAML files for the chapters in the assets/YAML directory.

After that, I tried to run the chapters using the here::here function and I didn't change anything in the template of the 01_create_md_files.R except for the addition of the chapters. After running the 01_create_md_files.R , I seem to find the chapters in the assets/img directory but I do not find them in the posts directory.

Here is the code and the error I am getting.


FILE_INFO <- list(
      # note: all .rmd files must live in the analysis/ directory
      "rmd_files" = c(

        here::here("analysis/2021-04-09-ch03-Adding_federal_program_data.rmd")
      ),
      "yaml_files" = c(

        here::here("assets/yaml/2021-04-09-ch03-Adding_federal_program_data.yaml")
      )
{
# create render function ----
create_chapter <- function(input_file, yaml_file) {
  # export the .rmd files as .md files into the assets/img/ directory
  rmarkdown::render(input = input_file,
                    output_format = "md_document",
                    output_dir = here::here("assets/img/")
                    )

  # obtain the .rmd file name
  rmd_filename <- stringr::str_replace(input_file,
                                      # find the analysis sub string
                                      "analysis",
                                      # replace it with where the .md file lives
                                      "assets/img")

  # replace the .rmd with .md within the file name
  md_filename <- stringr::str_replace(rmd_filename,
                                      # find the .rmd sub string
                                      "rmd",
                                      # replace it with .md
                                      "md")

  # read the .md file name as a string
  md_string_raw <- readr::read_file(md_filename)

  # replace every instance of here::here() with .. to create a relative path 
  md_string_clean <- stringr::str_replace_all(md_string_raw,
                                              here::here(),
                                              "..")

  # add the .yaml file at the top of .md file
  md_string_clean_with_yaml <- paste(append(md_string_clean, 
                                            readr::read_file(yaml_file),
                                            # add .yaml contents to the top
                                            after = 0),
                                     # squish the vector into one element
                                     collapse = "\n")
  print(md_string_clean_with_yaml)

  # rewrite the .md file with the relative paths now referenced
  readr::write_file(md_string_clean_with_yaml,
                    file = md_filename)

  # move the .md file from assets/img into _posts/
  file.rename(from = md_filename,
              to = here::here("_posts", basename(md_filename)))
}

# for each file, create a chapter that the user can view on the website ----
mapply(FUN = create_chapter,
       FILE_INFO$rmd_files,
       FILE_INFO$yaml_files,
       SIMPLIFY = FALSE)

# end of script #
} 

Here is the error: image

I am not sure why this happened.

cenuno commented 3 years ago

Hi Marah,

I see you've updated your FILE_INFO object. Without seeing your .rmd and .yaml file, I'm going to have to guess at what could be causing the error.

An error like this requires even more information. Please push up your changes in a branch that have all the files that you're working with. That is how I or another person would have all the information to help you debug.

In theory, you should be able to highlight all the lines of code in this analysis/ 01_create_md_files.R file, run all lines, and your .md files should be created within _posts/. I think the problem might be a typo within your here::here("analysis/2021-04-09-ch03-Adding_federal_program_data.rmd") call.

Does your file name end in .rmd or .Rmd? Based on what I can see from your past labs, it looks like all your files end in .Rmd. Try that and let me know if that works.

Cheers, Cristian

cenuno commented 3 years ago

Hi Marah,

Upon further inspection, I believe you are trying to create a new function from the analysis/01_create_md_files.R logic. Please do not do this.

I can tell this is the case because I see extra {} characters within your copy and pasted R code that I do not see in the original file (copy and pasted below):

#
# Author:   Cristian Nuno
# Date:     March 14, 2021
# Purpose:  Create .md files from .rmd files and store them elsewhere
#

# load necessary packages ----
library(here)
library(readr)
library(rmarkdown)
library(stringr)

# load necessary constants ----
FILE_INFO <- list(
  # note: all .rmd files must live in the analysis/ directory
  "rmd_files" = c(
    here::here("analysis/2021-03-14-ch01-example_page.rmd")
  ),
  "yaml_files" = c(
    here::here("assets/yaml/2021-03-14-ch01-example_page.yaml")
  )
)

# create render function ----
create_chapter <- function(input_file, yaml_file) {
  # export the .rmd files as .md files into the assets/img/ directory
  rmarkdown::render(input = input_file,
                    output_format = "md_document",
                    output_dir = here::here("assets/img/")
                    )

  # obtain the .rmd file name
  rmd_filename <- stringr::str_replace(input_file,
                                      # find the analysis sub string
                                      "analysis",
                                      # replace it with where the .md file lives
                                      "assets/img")

  # replace the .rmd with .md within the file name
  md_filename <- stringr::str_replace(rmd_filename,
                                      # find the .rmd sub string
                                      "rmd",
                                      # replace it with .md
                                      "md")

  # read the .md file name as a string
  md_string_raw <- readr::read_file(md_filename)

  # replace every instance of here::here() with .. to create a relative path 
  md_string_clean <- stringr::str_replace_all(md_string_raw,
                                              here::here(),
                                              "..")

  # add the .yaml file at the top of .md file
  md_string_clean_with_yaml <- paste(append(md_string_clean, 
                                            readr::read_file(yaml_file),
                                            # add .yaml contents to the top
                                            after = 0),
                                     # squish the vector into one element
                                     collapse = "\n")
  print(md_string_clean_with_yaml)

  # rewrite the .md file with the relative paths now referenced
  readr::write_file(md_string_clean_with_yaml,
                    file = md_filename)

  # move the .md file from assets/img into _posts/
  file.rename(from = md_filename,
              to = here::here("_posts", basename(md_filename)))
}

# for each file, create a chapter that the user can view on the website ----
mapply(FUN = create_chapter,
       FILE_INFO$rmd_files,
       FILE_INFO$yaml_files,
       SIMPLIFY = FALSE)

# end of script #

You need the create_chapter function and the FILE_INFO object to both exist as they serve as inputs within the mapply() function. If you replace the template FILE_INFO with your own, highlight all the lines of code, and then run all, you should see your .md file populate within _posts/.

Cheers, Cristian

gzbib commented 3 years ago

Hello Sir @cenuno

I am actually getting the same error, even though I have the same code as the original file without the extra bracket. I am not sure why the input_file is not being found.

image

# load necessary packages ----
library(here)
library(readr)
library(rmarkdown)
library(stringr)

# load necessary constants ----
FILE_INFO <- list(
  # note: all .rmd files must live in the analysis/ directory
  "rmd_files" = c(here::here("analysis/lab05.rmd")
  ),
  "yaml_files" = c(
    here::here("assets/yaml/2021-03-14-ch01-example_page.yaml")
  )
)

# create render function ----
create_chapter <- function(input_file, yaml_file) {
  # export the .rmd files as .md files into the assets/img/ directory
  rmarkdown::render(input = input_file,
                    output_format = "md_document",
                    output_dir = here::here("assets/img/")
                    )

  # obtain the .rmd file name
  rmd_filename <- stringr::str_replace(input_file,
                                      # find the analysis sub string
                                      "analysis",
                                      # replace it with where the .md file lives
                                      "assets/img")

  # replace the .rmd with .md within the file name
  md_filename <- stringr::str_replace(rmd_filename,
                                      # find the .rmd sub string
                                      "rmd",
                                      # replace it with .md
                                      "md")

  # read the .md file name as a string
  md_string_raw <- readr::read_file(md_filename)

  # replace every instance of here::here() with .. to create a relative path 
  md_string_clean <- stringr::str_replace_all(md_string_raw,
                                              here::here(),
                                              "..")

  # add the .yaml file at the top of .md file
  md_string_clean_with_yaml <- paste(append(md_string_clean, 
                                            readr::read_file(yaml_file),
                                            # add .yaml contents to the top
                                            after = 0),
                                     # squish the vector into one element
                                     collapse = "\n")
  print(md_string_clean_with_yaml)

  # rewrite the .md file with the relative paths now referenced
  readr::write_file(md_string_clean_with_yaml,
                    file = md_filename)

  # move the .md file from assets/img into _posts/
  file.rename(from = md_filename,
              to = here::here("_posts", basename(md_filename)))
}

# for each file, create a chapter that the user can view on the website ----
mapply(FUN = create_chapter,
       FILE_INFO$rmd_files,
       FILE_INFO$yaml_files,
       SIMPLIFY = FALSE)

# end of script #
cenuno commented 3 years ago

Hi Ghida,

This is strange. Other groups are using the code as is without this error.

I cannot provide technical support right now but will check back in on this issue by Monday.

Cheers, Cristian

— Cristian E. Nuno


From: gzbib @.> Sent: Saturday, April 17, 2021 10:01:44 AM To: DS4PS/cpp-528-spr-2021 @.> Cc: Cristian Ernesto Nuno @.>; Mention @.> Subject: Re: [DS4PS/cpp-528-spr-2021] Adding Chapters to the website (#48)

Hello Sir @cenunohttps://github.com/cenuno

I am actually getting the same error, even though I have the same code as the original file without the extra bracket. I am not sure why the input_file is not being found.

[image]https://user-images.githubusercontent.com/60263729/115120729-a3bf8000-9fb7-11eb-8777-01f85d3c678c.png

`#

Author: Cristian Nuno Date: March 14, 2021 Purpose: Create .md files from .rmd files and store them elsewhere load necessary packages ----

library(here) library(readr) library(rmarkdown) library(stringr)

load necessary constants ----

FILE_INFO <- list(

note: all .rmd files must live in the analysis/ directory

"rmd_files" = c(here::here("analysis/lab05.rmd") ), "yaml_files" = c( here::here("assets/yaml/2021-03-14-ch01-example_page.yaml") ) )

create render function ----

create_chapter <- function(input_file, yaml_file) {

export the .rmd files as .md files into the assets/img/ directory

rmarkdown::render(input = input_file, output_format = "md_document", output_dir = here::here("assets/img/") )

obtain the .rmd file name

rmd_filename <- stringr::str_replace(input_file,

find the analysis sub string

"analysis",

replace it with where the .md file lives

"assets/img")

replace the .rmd with .md within the file name

md_filename <- stringr::str_replace(rmd_filename,

find the .rmd sub string

"rmd",

replace it with .md

"md")

read the .md file name as a string

md_string_raw <- readr::read_file(md_filename)

replace every instance of here::here() with .. to create a relative path

md_string_clean <- stringr::str_replace_all(md_string_raw, here::here(), "..")

add the .yaml file at the top of .md file

md_string_clean_with_yaml <- paste(append(md_string_clean, readr::read_file(yaml_file),

add .yaml contents to the top

after = 0),

squish the vector into one element

collapse = "\n") print(md_string_clean_with_yaml)

rewrite the .md file with the relative paths now referenced

readr::write_file(md_string_clean_with_yaml, file = md_filename)

move the .md file from assets/img into _posts/

file.rename(from = md_filename, to = here::here("_posts", basename(md_filename))) }

for each file, create a chapter that the user can view on the website ----

mapply(FUN = create_chapter, FILE_INFO$rmd_files, FILE_INFO$yaml_files, SIMPLIFY = FALSE)

end of script

`

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/DS4PS/cpp-528-spr-2021/issues/48#issuecomment-821854131, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AFZB2S736KC3HJ6PIRVZALTTJG5HRANCNFSM43DEVEDQ.

cenuno commented 3 years ago

Hi Ghida -- again, if you open a pull request with all the code in a feature branch within your group repo, I can see every file related to this issue. Without that, all my advice is based on assumptions which is always less optimal than being presented all the information.

One thing I noticed is that my example .rmd file contains the following in the YAML header:

---
title: "This is an example page"
subtitle: "The title above can be anything you want. However, the file name must be in this specific format: `YYYY-MM-DD-chXX-short_name.md`."
author: "Cristian E. Nuno"
date: "`r format(Sys.time(), '%B %d, %Y')`"
output:
  md_document:
    variant: gfm
---

I think this may be the issue. Try adding that specific md document output code within your YAML header in your rmd file.

malmufre commented 3 years ago

Hi Professor,

I will create a feature branch that has all the code for adding the chapters to the website, but please see the error I am getting here. image I believe everything is going well in the assets folder, but nothing is reflecting in the posts folder.

malmufre commented 3 years ago

Hello,

I think after applying your advice by adding the YAML header in the .rmd I saw that the chapter is in the posts folder right now. I am only getting this and I believe this is only a warning. I think it is related to updating the tidyverse package but I am not sure

image

cenuno commented 3 years ago

Hi Marah,

That’s wonderful. Yes you are correct, that is only a warning.

Thank you for your patience!

Cheers, Cristian

— Cristian E. Nuno


From: Marah @.> Sent: Saturday, April 17, 2021 11:15:47 AM To: DS4PS/cpp-528-spr-2021 @.> Cc: Cristian Ernesto Nuno @.>; Mention @.> Subject: Re: [DS4PS/cpp-528-spr-2021] Adding Chapters to the website (#48)

Hello,

I think after applying your advice by adding the YAML header in the .rmd I saw that the chapter is in the posts folder right now. I am only getting this and I believe this is only a warning. I think it is related to updating the tidyverse package but I am not sure

[image]https://user-images.githubusercontent.com/59895354/115122682-e8501900-9fc1-11eb-8742-afc004ba79c8.png

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/DS4PS/cpp-528-spr-2021/issues/48#issuecomment-821864906, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AFZB2SZZSCZQVURKW4P6IF3TJHF5HANCNFSM43DEVEDQ.