Skip to content

add new functions render_all_rmd_project() and an addin that uses it #3

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -11,8 +11,8 @@ Authors@R: c(
email = "guyliann.engels@umons.ac.be"))
Maintainer: Philippe Grosjean <phgrosjean@sciviews.org>
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
Expand Down
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -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)
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
46 changes: 46 additions & 0 deletions R/addins.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)

}
55 changes: 55 additions & 0 deletions R/corr_rmd.R
Original file line number Diff line number Diff line change
@@ -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)
}
26 changes: 26 additions & 0 deletions R/render_all_rmd_project.R
Original file line number Diff line number Diff line change
@@ -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,...)
}

5 changes: 5 additions & 0 deletions inst/rstudio/addins.dcf
Original file line number Diff line number Diff line change
Expand Up @@ -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
25 changes: 25 additions & 0 deletions man/corr_rmd.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions man/render_sdd.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.