Skip to content

Commit

Permalink
update metadata addin
Browse files Browse the repository at this point in the history
  • Loading branch information
alan-y committed Nov 21, 2023
1 parent f1b34a9 commit 43658fb
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 4 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export(new_script)
export(phs_report_docx)
export(phsproject)
export(phsshinyapp)
export(update_metadata)
importFrom(magrittr,"%>%")
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
37 changes: 37 additions & 0 deletions R/update_metadata.R
Original file line number Diff line number Diff line change
@@ -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.")
}
}
6 changes: 3 additions & 3 deletions inst/rstudio/addins.dcf
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 19 additions & 0 deletions man/update_metadata.Rd

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

0 comments on commit 43658fb

Please sign in to comment.