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

[v2] Add the toc function with the test failure. #155

Merged
merged 30 commits into from Apr 11, 2019

Conversation

JiaxiangBU
Copy link
Contributor

This follows the original pull request.
I add my original function in the dev branch and pull it into your dev branch, just test the git process you show me.
And I will follow your tips to edit the function in the two parts,

  1. relative path
  2. change the base logic of the function
s <- wflow_status()
rmd <- rownames(s$status)[s$status$published]
html <- to_html(rmd, outdir = s$docs)
html <- fs::path_file(html)

Just let you know, thank you for your help, I learn a lot from you.

@jdblischak
Copy link
Member

I add my original function in the dev branch and pull it into your dev branch, just test the git process you show me.

@JiaxiangBU Great!

change the base logic of the function

This will be the biggest change. By using workflowr-specific functions, you should be able to write the function without adding any new dependencies. The only new dependency you should need to add is clipr.

Just let you know, thank you for your help, I learn a lot from you.

Of course! Thanks for helping improve workflowr!

Please let me know when you are ready for me to review the pull request.

@JiaxiangBU
Copy link
Contributor Author

@jdblischak I follows your tips to use

  1. to_html to get HTML paths
  2. rmarkdown::yaml_front_matter to get titles
  3. relative paths

and pull it, just let you know.

Copy link
Member

@jdblischak jdblischak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is coming along great! Please see my comments and let me know what questions you have.

R/wflow_toc.R Outdated
#' @import tibble
#' @export

wflow_toc <- function(analysis_dir = "analysis", docs_dir = "docs") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only argument the function needs is project = ".". wflow_status() returns the paths to these subdirectories.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, I forget it, I will change it in the next commit.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I edit it, here, 8787eed

R/wflow_toc.R Outdated
#' @export

wflow_toc <- function(analysis_dir = "analysis", docs_dir = "docs") {
s <- wflow_status()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After adding project = "." as the default argument to wflow_toc(), you can change wflow_status() to wflow_status(project = project).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, I will do it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I edit it, here, 8787eed

R/wflow_toc.R Outdated
wflow_toc <- function(analysis_dir = "analysis", docs_dir = "docs") {
s <- wflow_status()
s$status %>%
tibble::rownames_to_column(var = 'rmd_path') %>%
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know how to perform these steps without relying on the tidyverse functions? My package doesn't currently use these packages, and I don't want to add a lot of new dependencies for just this one function. If needed, I can translate these to base R commands.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I agree with you. I will try it first.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I edit it, but I don't find a good substitute for dplyr::*join function, do you have any recommendations?
here is my handle, cdbb9c3

R/wflow_toc.R Outdated
link = glue::glue("1. [{name}]({html_path})")
) %>%
.$link %>%
clipr::write_clip()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to make clipr a suggested package. If the user doesn't have clipr installed, then the function can print the results to the R console. Either way it would be good to return the character vector invisibly using return(invisible(x)), where x would be replaced with the character vector containing the toc.

You can check if a package is installed using the following strategy:

if requireNamespace("clipr", quietly = TRUE) {
  # Write to clipboard
} else {
  # Print to R console
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use your recommendation, but I do it in my way because I still get the point of the function invisible.
In my understanding, if the users have already installed the package clipr, they will give the toc in their clipboard. If not, the function wflow_toc will print the toc in the console?

Do I miss something?

@JiaxiangBU
Copy link
Contributor Author

Hi @jdblischak , I think I finish most of them, and I am still working on some minimal parts. Now I am trying to update the DESC manually, do you know to have to update it automatically?
In our conversation, we will delete most of the dependencies from the package tidyverse.

@JiaxiangBU
Copy link
Contributor Author

Hi @jdblischak , I think the rmarkdown chunk option results='asis' is better idea.

when set to 'asis', text output is written “as-is”, e.g., you can write out raw Markdown text from R code (like cat('**Markdown** is cool.\n'))

But I don't find a way to set this argument inside the function. Do you know something about it?
Here is some discussion on SO

@jdblischak
Copy link
Member

Now I am trying to update the DESC manually, do you know to have to update it automatically?

@JiaxiangBU To update DESCRIPTION automatically, first delete the @import statements from the roxygen2 documentation and then run devtools::document().

@JiaxiangBU
Copy link
Contributor Author

JiaxiangBU commented Apr 4, 2019

Now I am trying to update the DESC manually, do you know to have to update it automatically?

@JiaxiangBU To update DESCRIPTION automatically, first delete the @import statements from the roxygen2 documentation and then run devtools::document().

Hi, @jdblischak I do it and drop most of the tidyverse related packages.

@jdblischak
Copy link
Member

@JiaxiangBU My mistake. document() only automatically updates NAMESPACE. You'll need to manually edit DESCRIPTION to remove the packages. When you do that, please add back the version specifications for fs and rmarkdown.

Copy link
Member

@jdblischak jdblischak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is coming along great!

R/wflow_toc.R Outdated
s <- wflow_status(project = ".")
df<- s$status
df$rmd_path <- rownames(df)
df <- df[df$published]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To filter the rows, this needs to be df <- df[df$published, ]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I agree with you.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I agree with you.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I make a new commit for this editing.

R/wflow_toc.R Outdated
df<- s$status
df$rmd_path <- rownames(df)
df <- df[df$published]
df$html_path <- to_html(df$rmd_path,outdir = s$docs) %>% basename()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't use the pipe %>%. Instead, either break this into 2 lines:

df$html_path <- to_html(df$rmd_path,outdir = s$docs)
df$html_path <- basename(df$html_path)

or nest the function calls:

df$html_path <- basename(to_html(df$rmd_path,outdir = s$docs))

R/wflow_toc.R Outdated
df$rmd_path <- rownames(df)
df <- df[df$published]
df$html_path <- to_html(df$rmd_path,outdir = s$docs) %>% basename()
file_name <- sapply(df$rmd_path, function(x) rmarkdown::yaml_front_matter(x)$title) %>%
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part is tricky since an Rmd file may not have a title defined. I think it may be best handled with a custom function. Can I send push a commit to your pull request to add this? Then you would get the change locally by running git pull.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, that is great, I am looking forward to it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I try a pull request from your dev branch to mine and now I know how it works.

R/wflow_toc.R Outdated
df$link <- paste0("1. [",df$name,"](",df$html_path,")")

# output
# if (requireNamespace("clipr", quietly = TRUE)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be able to view the output even though it is copied to the clipboard, you can return the value of df$link at the end of the function with return(invisible(df$link)).

> x <- wflow_toc()
The table of content of your project is on the clipboard.
> x
[1] "1. [About](about.html)"     "1. [Home](index.html)"      "1. [License](license.html)"

This will also make it easier to write automated tests for wflow_toc().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get your point, so the users expect to get an output in a vector.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I make a new commit to edit it.

@JiaxiangBU
Copy link
Contributor Author

@JiaxiangBU My mistake. document() only automatically updates NAMESPACE. You'll need to manually edit DESCRIPTION to remove the packages. When you do that, please add back the version specifications for fs and rmarkdown.

@jdblischak
Yeah, I delete the tidyverse packages in the DESC document and put the clipr in the suggested field.

@JiaxiangBU
Copy link
Contributor Author

Yes, currently I use it in the index.html pages for several workflowr projects.

@JiaxiangBU My question was about including index.html in the list of analyses.

For example, on your main page for learn_rmd, your list of analyses includes Home, About, and License. Since links to these pages are already available in the navigation bar, it is somewhat redundant to include them in the list of rmd files. We could by default have wflow_toc() ignore any rmd files that are included in the navigation bar (by checking analysis/_site.yml), and then provide a function argument to optionally include them. Does that make sense?

I get your point and agree with you. I am thinking about it. When I update the function, I will let you know,.

@JiaxiangBU
Copy link
Contributor Author

Next topic, I find based on this function, the toc is updated manually by users every time. Maybe an automatic solution is better.

@JiaxiangBU
Copy link
Contributor Author

Hi John. I make a new commit for this.

  1. title and HTML are extracted from this object, rmd, so I edit it first.
  2. And I find the imported packages in the function document is deleted, so I don't add here package.

@jdblischak
Copy link
Member

I make a new commit for this.

@JiaxiangBU Awesome! Good job parsing the YAML file!

title and HTML are extracted from this object, rmd, so I edit it first.

I agree that you want to filter the results early.

And I find the imported packages in the function document is deleted, so I don't add here package.

You don't need to use the here package. wflow_status() returns the path to the analysis directory (note that it doesn't need to called analysis/; a user can change it and wflow_status() will still identify the correct subdirectory).

I'll update the tests.

@jdblischak
Copy link
Member

@JiaxiangBU I just pushed two new commits to your branch. Please pull them locally with git pull origin dev.

I changed the filtering to use html. This is because these are always just the filenames like in the navigation bar. The rmd vector may have other parts of the file path included, which is why it won't always work (we have to preserve the path to be able to read the title later on).

I also added some tests to catch some edge cases, e.g. when there is no navbar.

Next topic, I find based on this function, the toc is updated manually by users every time. Maybe an automatic solution is better.

Could you please elaborate on this idea? What would happen automatically?

@JiaxiangBU
Copy link
Contributor Author

You don't need to use the here package. wflow_status() returns the path to the analysis directory (note that it doesn't need to called analysis/; a user can change it and wflow_status() will still identify the correct subdirectory).

Thanks, I have a deep understanding of this functon wflow_status().

@JiaxiangBU
Copy link
Contributor Author

I changed the filtering to use html. This is because these are always just the filenames like in the navigation bar. The rmd vector may have other parts of the file path included, which is why it won't always work (we have to preserve the path to be able to read the title later on).

I think this change makes this function better, thanks.

@JiaxiangBU
Copy link
Contributor Author

Next topic, I find based on this function, the toc is updated manually by users every time. Maybe an automatic solution is better.

Could you please elaborate on this idea? What would happen automatically?

For exmaple, in one of my workflowr project, I add a analysis document by wflow_open. And then I feel good, so use wflow_build, commit this adding, wflow_publish. I learn these steps from the tutorial you write.

However, in this processs, the toc I think is not updated. the new analysis document is not listed in the toc. Does it make sense to automatically add it in the toc?

@JiaxiangBU
Copy link
Contributor Author

@JiaxiangBU I just pushed two new commits to your branch. Please pull them locally with git pull origin dev.

John, I update this package locally by git pull origin dev and on the GitHub by a new pull request. Thanks for your tips.

@jdblischak
Copy link
Member

However, in this processs, the toc I think is not updated. the new analysis document is not listed in the toc. Does it make sense to automatically add it in the toc?

@JiaxiangBU Thanks for the explanation. I see how this would be convenient. Unfortunately this would be currently different to implement (i.e. to automatically re-run index.Rmd and insert an updated table of contents).

As a compromise, one option would be to add the following code chunk to analysis/index.Rmd:

```{r toc, results="asis", echo=FALSE}
suppressMessages(toc <- workflowr::wflow_toc())
cat(toc, sep = "\n")
```

This way, every time you built/published index.Rmd, it would insert an updated version of the table of contents.

Note: In general running workflowr functions from within an Rmd file is a bad idea, but this works for wflow_toc().

@JiaxiangBU
Copy link
Contributor Author

JiaxiangBU commented Apr 10, 2019

suppressMessages(toc <- workflowr::wflow_toc())
cat(toc, sep = "\n")

Thus, I think just print a message with the content you write for the users. Users could just paste it in their index.Rmd. I think it is an option for users.

Note: In general running workflowr functions from within an Rmd file is a bad idea, but this works for wflow_toc().

Could you please explain this idea? Is there any limitation on the workflowr functions. I am just curious.

@jdblischak
Copy link
Member

Could you please explain this idea? Is there any limitation on the workflowr functions. I am just curious.

@JiaxiangBU The main workflowr functions like wflow_build()/wflow_publish()/wflow_status() all perform operations based on the current status of the R Markdown files in the workflowr project. Thus if you run a workflowr command from within an R Markdown file contained in a workflowr project, this can become circular. It will probably still execute, but long-term it is not a good idea for maintaining a workflowr project, and it will likely lead to problems. Does that make sense?

@jdblischak
Copy link
Member

@JiaxiangBU I think this PR is ready to merge. Could you please add an entry to NEWS.md? Please reference this PR, your GitHub username, and the original Issue #151.

Here's an example of a past entry:

* Improve support for hosting on Shiny Server. Setting the option
`fig_path_ext: false` in `_workflowr.yml` removes the file extension from the
figure subdirectories, allowing them to be viewed on Shiny Server (implemented
by @Tutuchan, #119, #122)

Also, would you like to be included as an official contributor?

@JiaxiangBU
Copy link
Contributor Author

Could you please explain this idea? Is there any limitation on the workflowr functions. I am just curious.

@JiaxiangBU The main workflowr functions like wflow_build()/wflow_publish()/wflow_status() all perform operations based on the current status of the R Markdown files in the workflowr project. Thus if you run a workflowr command from within an R Markdown file contained in a workflowr project, this can become circular. It will probably still execute, but long-term it is not a good idea for maintaining a workflowr project, and it will likely lead to problems. Does that make sense?

I get your point. Thanks for your explanation.

@codecov-io
Copy link

Codecov Report

Merging #155 into dev will increase coverage by 0.2%.
The diff coverage is 88.23%.

Impacted file tree graph

@@            Coverage Diff            @@
##              dev     #155     +/-   ##
=========================================
+ Coverage   86.93%   87.14%   +0.2%     
=========================================
  Files          25       26      +1     
  Lines        2871     2863      -8     
=========================================
- Hits         2496     2495      -1     
+ Misses        375      368      -7
Impacted Files Coverage Δ
R/wflow_toc.R 88.23% <88.23%> (ø)
R/git.R 75.64% <0%> (-5.38%) ⬇️
R/wflow_git_push.R 49.53% <0%> (+4.61%) ⬆️
R/wflow_git_pull.R 61.46% <0%> (+4.8%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9345f74...7da9253. Read the comment docs.

@JiaxiangBU
Copy link
Contributor Author

@JiaxiangBU I think this PR is ready to merge. Could you please add an entry to NEWS.md? Please reference this PR, your GitHub username, and the original Issue #151.

Here's an example of a past entry:

* Improve support for hosting on Shiny Server. Setting the option
`fig_path_ext: false` in `_workflowr.yml` removes the file extension from the
figure subdirectories, allowing them to be viewed on Shiny Server (implemented
by @Tutuchan, #119, #122)

Hi, John, I make several commits to update news. I find there are several changes from origin dev, when I use git pull origin dev, I commit them separately, 4a88cc7.

@JiaxiangBU
Copy link
Contributor Author

Also, would you like to be included as an official contributor?

Yes, I do like it.

@jdblischak jdblischak merged commit 7da9253 into workflowr:dev Apr 11, 2019
@jdblischak
Copy link
Member

@JiaxiangBU I added you as a contributor in 54b2afb. The function wflow_toc() will be included in the next release. Thanks again for your contribution to workflowr!

jdblischak added a commit that referenced this pull request Apr 12, 2019
Related to Issue #151 and PR #155.

Sometimes the system keyboard is not available when I run the tests with
devtools::test(). Since this is technically an interactive session, the
tests fail.
jdblischak added a commit that referenced this pull request Apr 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants