From 5a9835c4bbe7d19e74c0375c36935f9187dc4756 Mon Sep 17 00:00:00 2001 From: GuyliannEngels Date: Wed, 16 Mar 2022 15:34:18 +0100 Subject: [PATCH 1/5] add new functions render_all_rmd_project() and an addin that uses it --- DESCRIPTION | 6 ++-- NAMESPACE | 3 ++ NEWS.md | 5 +++ R/addins.R | 46 +++++++++++++++++++++++++++ R/render_all_rmd_project.R | 58 +++++++++++++++++++++++++++++++++++ inst/rstudio/addins.dcf | 5 +++ man/render_all_rmd_project.Rd | 26 ++++++++++++++++ 7 files changed, 146 insertions(+), 3 deletions(-) create mode 100644 R/render_all_rmd_project.R create mode 100644 man/render_all_rmd_project.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 0e46e94..247fdcb 100755 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: BioDataScience Type: Package -Version: 2021.1.0 +Version: 2021.2.0 Title: Configuration for Biological Data Science Course Description: User sign in and sign out for the Biological Data Science courses at the University of Mons, Belgium. @@ -11,8 +11,8 @@ Authors@R: c( email = "guyliann.engels@umons.ac.be")) Maintainer: Philippe Grosjean Depends: R (>= 3.5.0) -Imports: learnitdown -Suggests: covr, knitr, rmarkdown, testthat +Imports: learnitdown, rmarkdown, rprojroot, shiny, miniUI, rstudioapi +Suggests: covr, knitr, testthat Remotes: SciViews/learnitdown License: MIT + file LICENSE LazyData: true diff --git a/NAMESPACE b/NAMESPACE index 2f01f30..0bc9a20 100755 --- a/NAMESPACE +++ b/NAMESPACE @@ -2,6 +2,9 @@ export(config) export(obfuscator) +export(render_all_rmd_project) export(sdd_info) export(sign_in) export(sign_out) +import(rmarkdown) +import(rprojroot) diff --git a/NEWS.md b/NEWS.md index 3582d06..c64ec53 100755 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,8 @@ +# BioDataScience 2021.2.0 + +- `render_all_rmd_project()` function is used to convert to html documents all Rmarkdown files in a project. +- `Check my project` addin is an Rstudio addin used to validate several criteria on a project like the compilation of Rmarkdown. + # BioDataScience 2021.1.0 - `sdd_info()` function and corresponfing RStudio addin are introduced for easily sharing small pieces of text in a central repository. diff --git a/R/addins.R b/R/addins.R index 3cc9f9e..5e2586f 100644 --- a/R/addins.R +++ b/R/addins.R @@ -5,3 +5,49 @@ sdd_info_addin <- function() sign_out_addin <- function() sign_out() + +check_project_addin <- function() { + + ui <- miniUI::miniPage( + miniUI::gadgetTitleBar("Check my project"), + miniUI::miniContentPanel( + shiny::h4("Project path"), + shiny::textOutput("proj_dir"), + shiny::h4("Convert to HTML documents all Rmarkdown documents in the project"), + shiny::tableOutput("render"), + shiny::hr() + ) + ) + + server <- function(input, output, session) { + + res_proj <- rprojroot::find_root(rprojroot::is_rstudio_project, path = ".") + res <- BioDataScience::render_all_rmd_project(".") + + output$proj_dir <- shiny::renderText({ + print(res_proj) + }) + + output$render <- shiny::renderTable({ + res + }) + + shiny::observeEvent(input$done, { + res1 <- res[res$check == "ERROR", ] + + if(nrow(res1)>0) + rstudioapi::navigateToFile(res1$file) ## open files that do not knit + + shiny::stopApp() + }) + + shiny::observeEvent(input$cancel, { + shiny::stopApp() + }) + + } + + viewer <- shiny::paneViewer(300) + shiny::runGadget(ui, server, viewer = viewer) + +} diff --git a/R/render_all_rmd_project.R b/R/render_all_rmd_project.R new file mode 100644 index 0000000..f9f40aa --- /dev/null +++ b/R/render_all_rmd_project.R @@ -0,0 +1,58 @@ +#' Convert to HTML documents all Rmarkdown in a project +#' +#'Find all R markdown files in a Rstudio project and try to convert then an HTML document. +#'This functions use \code{html_document()} and a serie of predefined arguments +#'in order to meet the requirements of the biological data science course. +#' +#' @param path the path of the project +#' @param correction If the correction is TRUE, a copy of rmd file do not convert is created. +#' +#' @import rprojroot rmarkdown +#' +#' @return a data.frame with the results of the compilations in html +#' @export +#' +#' @examples +#' #library(pfunctions) +#' #test <- render_all_rmd_project() +#' +render_all_rmd_project <- function(path = ".", correction = FALSE) { + + # Check if us a rstuio project + rprojroot::find_root(rprojroot::is_rstudio_project, path = path) + + # Find all rmd files + paths <- list.files(path = path, pattern = "*.Rmd", recursive = TRUE) + paths_lg <- length(paths) + + res <- data.frame( + "file" = vector("character", paths_lg), + "check" = vector("character", paths_lg), + "result" = vector("character", paths_lg) + ) + + for(i in seq_along(paths)) { + rmd_res <- try(rmarkdown::render(paths[i], + output_format = rmarkdown::html_document(toc = TRUE, number_sections = TRUE, code_folding = "hide", + anchor_sections = TRUE, self_contained = FALSE), quiet = TRUE), + silent = TRUE) + + rmd_res1 <- list( + "file" = paths[i] , + "check" = ifelse(class(rmd_res) == "try-error", yes = "ERROR", no = "OK"), + "result" = as.character(rmd_res)) + + res[i, ] <- rmd_res1 + } + + if(isTRUE(correction)) { + res1 <- res[res$check == "ERROR",] + + if(nrow(res1)>0) { + res1$file_corr <-gsub(".Rmd$", "_corr.Rmd", x = res1$file) + } + file.copy(from = res1$file, to = res1$file_corr) + } + + return(res) +} diff --git a/inst/rstudio/addins.dcf b/inst/rstudio/addins.dcf index d2c4dbf..854f301 100644 --- a/inst/rstudio/addins.dcf +++ b/inst/rstudio/addins.dcf @@ -7,3 +7,8 @@ Name: Sign out Description: Sign out current user Binding: sign_out_addin Interactive: true + +Name: Check my project +Description: Check my data science project +Binding: check_project_addin +Interactive: true \ No newline at end of file diff --git a/man/render_all_rmd_project.Rd b/man/render_all_rmd_project.Rd new file mode 100644 index 0000000..0a8d9fe --- /dev/null +++ b/man/render_all_rmd_project.Rd @@ -0,0 +1,26 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/render_all_rmd_project.R +\name{render_all_rmd_project} +\alias{render_all_rmd_project} +\title{Convert to HTML documents all Rmarkdown in a project} +\usage{ +render_all_rmd_project(path = ".", correction = FALSE) +} +\arguments{ +\item{path}{the path of the project} + +\item{correction}{If the correction is TRUE, a copy of rmd file do not convert is created.} +} +\value{ +a data.frame with the results of the compilations in html +} +\description{ +Find all R markdown files in a Rstudio project and try to convert then an HTML document. +This functions use \code{html_document()} and a serie of predefined arguments +in order to meet the requirements of the biological data science course. +} +\examples{ +#library(pfunctions) +#test <- render_all_rmd_project() + +} From 86c1449754cf5549e8a29019f1c9b105a7d96fe4 Mon Sep 17 00:00:00 2001 From: GuyliannEngels Date: Mon, 4 Apr 2022 12:08:29 +0200 Subject: [PATCH 2/5] deletion of render_all_rmd_project() and replacement by corr_rmd() and render_sdd(). --- NAMESPACE | 3 +- R/corr_rmd.R | 55 +++++++++++++++++++++++++++++++++++ R/render_all_rmd_project.R | 26 +++++++++++------ man/corr_rmd.Rd | 25 ++++++++++++++++ man/render_all_rmd_project.Rd | 26 ----------------- man/render_sdd.Rd | 26 +++++++++++++++++ 6 files changed, 125 insertions(+), 36 deletions(-) create mode 100644 R/corr_rmd.R create mode 100644 man/corr_rmd.Rd delete mode 100644 man/render_all_rmd_project.Rd create mode 100644 man/render_sdd.Rd diff --git a/NAMESPACE b/NAMESPACE index 0bc9a20..f6389e6 100755 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,8 +1,9 @@ # Generated by roxygen2: do not edit by hand export(config) +export(corr_rmd) export(obfuscator) -export(render_all_rmd_project) +export(render_sdd) export(sdd_info) export(sign_in) export(sign_out) diff --git a/R/corr_rmd.R b/R/corr_rmd.R new file mode 100644 index 0000000..80d1ccd --- /dev/null +++ b/R/corr_rmd.R @@ -0,0 +1,55 @@ +#' Convert to HTML documents all Rmarkdown in a project +#' +#'Find all R markdown files in a Rstudio project +#'and try to convert then an HTML document with \code{render_sdd()}. +#' +#' @param path the path of the project +#' @param correction If correction is TRUE, a copy of rmd file do not convert is created. +#' +#' @import rprojroot +#' +#' @return a data.frame with the results of the compilations in html +#' @export +#' +#' @examples +#' #library(pfunctions) +#' #test <- corr_project() +#' +corr_rmd <- function(path = ".", correction = TRUE) { + + # Check if us a rstuio project + rprojroot::find_root(rprojroot::is_rstudio_project, path = path) + + # Find all rmd files + paths <- list.files(path = path, pattern = "*.Rmd", recursive = TRUE) + paths_lg <- length(paths) + + res <- data.frame( + "file" = vector("character", paths_lg), + "check" = vector("character", paths_lg), + "result" = vector("character", paths_lg) + ) + + for(i in seq_along(paths)) { + rmd_res <- try(render_sdd(paths[i]), + silent = TRUE) + + rmd_res1 <- list( + "file" = paths[i] , + "check" = ifelse(class(rmd_res) == "try-error", yes = "ERROR", no = "OK"), + "result" = as.character(rmd_res)) + + res[i, ] <- rmd_res1 + } + + if(isTRUE(correction)) { + res1 <- res[res$check == "ERROR",] + + if(nrow(res1)>0) { + res1$file_corr <-gsub(".Rmd$", "_corr.Rmd", x = res1$file) + } + file.copy(from = res1$file, to = res1$file_corr) + } + + return(res) +} diff --git a/R/render_all_rmd_project.R b/R/render_all_rmd_project.R index f9f40aa..451e076 100644 --- a/R/render_all_rmd_project.R +++ b/R/render_all_rmd_project.R @@ -1,21 +1,29 @@ -#' Convert to HTML documents all Rmarkdown in a project +#' Convert to HTML documents an Rmarkdown with specific rules #' -#'Find all R markdown files in a Rstudio project and try to convert then an HTML document. -#'This functions use \code{html_document()} and a serie of predefined arguments -#'in order to meet the requirements of the biological data science course. +#' Convert Rmarkdown file in an HTML document a serie of predefined arguments +#' in order to meet the requirements of the biological data science course. +#' This functions use \code{render()} and \code{html_document()} #' -#' @param path the path of the project -#' @param correction If the correction is TRUE, a copy of rmd file do not convert is created. #' -#' @import rprojroot rmarkdown +#' @param path the path of the Rmd file. +#' @param ... The arguments of render() #' -#' @return a data.frame with the results of the compilations in html +#' @import rmarkdown +#' +#' @return an html document #' @export #' #' @examples #' #library(pfunctions) -#' #test <- render_all_rmd_project() +#' #test <- render_sdd(path = "file.Rmd") #' +render_sdd <- function(path, ...){ + rmarkdown::render(path, + output_format = rmarkdown::html_document( + toc = TRUE, number_sections = TRUE, code_folding = "hide", + anchor_sections = TRUE, self_contained = FALSE), quiet = TRUE) +} + render_all_rmd_project <- function(path = ".", correction = FALSE) { # Check if us a rstuio project diff --git a/man/corr_rmd.Rd b/man/corr_rmd.Rd new file mode 100644 index 0000000..fb5a7ea --- /dev/null +++ b/man/corr_rmd.Rd @@ -0,0 +1,25 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/corr_rmd.R +\name{corr_rmd} +\alias{corr_rmd} +\title{Convert to HTML documents all Rmarkdown in a project} +\usage{ +corr_rmd(path = ".", correction = TRUE) +} +\arguments{ +\item{path}{the path of the project} + +\item{correction}{If correction is TRUE, a copy of rmd file do not convert is created.} +} +\value{ +a data.frame with the results of the compilations in html +} +\description{ +Find all R markdown files in a Rstudio project +and try to convert then an HTML document with \code{render_sdd()}. +} +\examples{ +#library(pfunctions) +#test <- corr_project() + +} diff --git a/man/render_all_rmd_project.Rd b/man/render_all_rmd_project.Rd deleted file mode 100644 index 0a8d9fe..0000000 --- a/man/render_all_rmd_project.Rd +++ /dev/null @@ -1,26 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/render_all_rmd_project.R -\name{render_all_rmd_project} -\alias{render_all_rmd_project} -\title{Convert to HTML documents all Rmarkdown in a project} -\usage{ -render_all_rmd_project(path = ".", correction = FALSE) -} -\arguments{ -\item{path}{the path of the project} - -\item{correction}{If the correction is TRUE, a copy of rmd file do not convert is created.} -} -\value{ -a data.frame with the results of the compilations in html -} -\description{ -Find all R markdown files in a Rstudio project and try to convert then an HTML document. -This functions use \code{html_document()} and a serie of predefined arguments -in order to meet the requirements of the biological data science course. -} -\examples{ -#library(pfunctions) -#test <- render_all_rmd_project() - -} diff --git a/man/render_sdd.Rd b/man/render_sdd.Rd new file mode 100644 index 0000000..b6b0f33 --- /dev/null +++ b/man/render_sdd.Rd @@ -0,0 +1,26 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/render_all_rmd_project.R +\name{render_sdd} +\alias{render_sdd} +\title{Convert to HTML documents an Rmarkdown with specific rules} +\usage{ +render_sdd(path, ...) +} +\arguments{ +\item{path}{the path of the Rmd file.} + +\item{...}{The arguments of render()} +} +\value{ +an html document +} +\description{ +Convert Rmarkdown file in an HTML document a serie of predefined arguments +in order to meet the requirements of the biological data science course. +This functions use \code{render()} and \code{html_document()} +} +\examples{ +#library(pfunctions) +#test <- render_sdd(path = "file.Rmd") + +} From f7d29b7ec7c78ac17ba35241b2fe3d98fd9d4369 Mon Sep 17 00:00:00 2001 From: GuyliannEngels Date: Mon, 4 Apr 2022 12:09:31 +0200 Subject: [PATCH 3/5] update of the `check my project` addin --- R/addins.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/addins.R b/R/addins.R index 5e2586f..414203b 100644 --- a/R/addins.R +++ b/R/addins.R @@ -22,7 +22,7 @@ check_project_addin <- function() { server <- function(input, output, session) { res_proj <- rprojroot::find_root(rprojroot::is_rstudio_project, path = ".") - res <- BioDataScience::render_all_rmd_project(".") + res <- BioDataScience::corr_rmd(".", correction = FALSE) output$proj_dir <- shiny::renderText({ print(res_proj) From 4b20bfb5adc7ff634ba3186c8888866fcb17210c Mon Sep 17 00:00:00 2001 From: GuyliannEngels Date: Mon, 4 Apr 2022 12:14:39 +0200 Subject: [PATCH 4/5] update of NEWS.md --- NEWS.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index c64ec53..c6f2b16 100755 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,7 @@ # BioDataScience 2021.2.0 -- `render_all_rmd_project()` function is used to convert to html documents all Rmarkdown files in a project. +- `render_sdd()` function convert a Rmarkdown files to html document with specific arguments. +- `corr_rmd()` function is used to try to convert all Rmarkdown files in a project to html documents. If a document does not compile, a copy is created. - `Check my project` addin is an Rstudio addin used to validate several criteria on a project like the compilation of Rmarkdown. # BioDataScience 2021.1.0 From 4a190572a8ac612da4c88c98cbfd309eb715a120 Mon Sep 17 00:00:00 2001 From: GuyliannEngels Date: Mon, 4 Apr 2022 15:55:29 +0200 Subject: [PATCH 5/5] delete the old old function (render_all_rmd_project()) --- R/render_all_rmd_project.R | 42 +------------------------------------- 1 file changed, 1 insertion(+), 41 deletions(-) diff --git a/R/render_all_rmd_project.R b/R/render_all_rmd_project.R index 451e076..6d91977 100644 --- a/R/render_all_rmd_project.R +++ b/R/render_all_rmd_project.R @@ -21,46 +21,6 @@ render_sdd <- function(path, ...){ rmarkdown::render(path, output_format = rmarkdown::html_document( toc = TRUE, number_sections = TRUE, code_folding = "hide", - anchor_sections = TRUE, self_contained = FALSE), quiet = TRUE) + anchor_sections = TRUE, self_contained = FALSE), quiet = TRUE,...) } -render_all_rmd_project <- function(path = ".", correction = FALSE) { - - # Check if us a rstuio project - rprojroot::find_root(rprojroot::is_rstudio_project, path = path) - - # Find all rmd files - paths <- list.files(path = path, pattern = "*.Rmd", recursive = TRUE) - paths_lg <- length(paths) - - res <- data.frame( - "file" = vector("character", paths_lg), - "check" = vector("character", paths_lg), - "result" = vector("character", paths_lg) - ) - - for(i in seq_along(paths)) { - rmd_res <- try(rmarkdown::render(paths[i], - output_format = rmarkdown::html_document(toc = TRUE, number_sections = TRUE, code_folding = "hide", - anchor_sections = TRUE, self_contained = FALSE), quiet = TRUE), - silent = TRUE) - - rmd_res1 <- list( - "file" = paths[i] , - "check" = ifelse(class(rmd_res) == "try-error", yes = "ERROR", no = "OK"), - "result" = as.character(rmd_res)) - - res[i, ] <- rmd_res1 - } - - if(isTRUE(correction)) { - res1 <- res[res$check == "ERROR",] - - if(nrow(res1)>0) { - res1$file_corr <-gsub(".Rmd$", "_corr.Rmd", x = res1$file) - } - file.copy(from = res1$file, to = res1$file_corr) - } - - return(res) -}