diff --git a/NAMESPACE b/NAMESPACE index 865bbc7..7abb20b 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -8,4 +8,5 @@ export(new_script) export(phs_report_docx) export(phsproject) export(phsshinyapp) +export(update_metadata) importFrom(magrittr,"%>%") diff --git a/NEWS.md b/NEWS.md index 934e142..568092d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -4,7 +4,7 @@ * The Management Info One Page template will accommodate using more than one page and applies page numbers from page 2 onwards, so could also be used as a template for a simple information request response. * Basic PHS style HTML template. * RStudio addin for `add_stylecss()` so users can add a PHS style.css file directly from RStudio. - +* RStudio addin to update the metadata in existing R scripts for latest update date and R version run on. # phstemplates 1.1.0 diff --git a/R/update_metadata.R b/R/update_metadata.R new file mode 100644 index 0000000..659f593 --- /dev/null +++ b/R/update_metadata.R @@ -0,0 +1,37 @@ +#' update_metadata +#' @description Updates metadata in the PHS R script template for existing script. This function is meant to be used within RStudio by clicking on the Addins dropdown menu. +#' +#' @return Updated metadata in the R script. +#' @export +#' @examples +#' \dontrun{ +#' update_metadata() +#' } +update_metadata <- function() { + pos <- grep("^# Written", rstudioapi::getSourceEditorContext()$contents, ) + if (length(pos) > 0) { + pos <- min(pos) + pos_range <- rstudioapi::document_range(c(pos, 0), c((pos + 1), 0)) + rstudioapi::insertText(pos_range, run_on(linebreak = TRUE), + id = rstudioapi::documentId(allowConsole = FALSE) + ) + } else { + warning("The default PHS R script metadata was not detected, so the R version was not updated.") + } + + latest_date <- paste0("# Latest update date: ", Sys.Date(), "\n") + + pos <- grep( + "^# Latest update date", + rstudioapi::getSourceEditorContext()$contents + ) + if (length(pos) > 0) { + pos <- min(pos) + pos_range <- rstudioapi::document_range(c(pos, 0), c((pos + 1), 0)) + rstudioapi::insertText(pos_range, latest_date, + id = rstudioapi::documentId(allowConsole = FALSE) + ) + } else { + warning("The default PHS R script metadata was not detected, so the 'latest date' was not updated.") + } +} diff --git a/inst/rstudio/addins.dcf b/inst/rstudio/addins.dcf index cb81936..20dd0aa 100644 --- a/inst/rstudio/addins.dcf +++ b/inst/rstudio/addins.dcf @@ -8,7 +8,7 @@ Description: Add a PHS style .gitignore file to a chosen directory Binding: add_gitignore Interactive: true -Name: Add PHS style CSS file -Description: Add a PHS style CSS file to a chosen directory. -Binding: add_stylecss +Name: Update PHS metadata +Description: Updates metadata in R scripts for latest update date and R version run +Binding: update_metadata Interactive: true diff --git a/man/update_metadata.Rd b/man/update_metadata.Rd new file mode 100644 index 0000000..bb55233 --- /dev/null +++ b/man/update_metadata.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/update_metadata.R +\name{update_metadata} +\alias{update_metadata} +\title{update_metadata} +\usage{ +update_metadata() +} +\value{ +Updated metadata in the R script. +} +\description{ +Updates metadata in the PHS R script template for existing script. This function is meant to be used within RStudio by clicking on the Addins dropdown menu. +} +\examples{ +\dontrun{ +update_metadata() +} +}