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..f6389e6 100755 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,7 +1,11 @@ # Generated by roxygen2: do not edit by hand export(config) +export(corr_rmd) export(obfuscator) +export(render_sdd) export(sdd_info) export(sign_in) export(sign_out) +import(rmarkdown) +import(rprojroot) diff --git a/NEWS.md b/NEWS.md index 3582d06..c6f2b16 100755 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,9 @@ +# BioDataScience 2021.2.0 + +- `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 - `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..414203b 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::corr_rmd(".", correction = FALSE) + + 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/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 new file mode 100644 index 0000000..6d91977 --- /dev/null +++ b/R/render_all_rmd_project.R @@ -0,0 +1,26 @@ +#' Convert to HTML documents an Rmarkdown with specific rules +#' +#' 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 Rmd file. +#' @param ... The arguments of render() +#' +#' @import rmarkdown +#' +#' @return an html document +#' @export +#' +#' @examples +#' #library(pfunctions) +#' #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,...) +} + 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/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_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") + +}