Skip to content
Snippets Groups Projects
Commit 0f2447b5 authored by David Dorchies's avatar David Dorchies :zany_face:
Browse files

feat: create R project with minimum functions

parent fbdfa3ce
No related branches found
No related tags found
No related merge requests found
^.*\.Rproj$
^\.Rproj\.user$
################################################################################
# R template from:
# https://raw.githubusercontent.com/github/gitignore/main/R.gitignore
# Downloaded the 2023-08-31 11:51:25
################################################################################
# History files
.Rhistory
.Rapp.history
# Session Data files
.RData
.RDataTmp
# User-specific files
.Ruserdata
# Example code in package build process
*-Ex.R
# Output files from R CMD build
/*.tar.gz
# Output files from R CMD check
/*.Rcheck/
# RStudio files
.Rproj.user/
# produced vignettes
vignettes/*.html
vignettes/*.pdf
# OAuth2 token, see https://github.com/hadley/httr/releases/tag/v0.3
.httr-oauth
# knitr and R markdown default cache directories
*_cache/
/cache/
# Temporary files created by R markdown
*.utf8.md
*.knit.md
# R Environment Variables
.Renviron
# pkgdown site
docs/
# translation temp files
po/*~
# RStudio Connect folder
rsconnect/
Package: fairify
Title: Produce FAIR reports with Rmarkdown and data in the cloud
Version: 0.0.0.9000
Authors@R:
person("David", "Dorchies", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-6595-7984"), email = "david.dorchies@inrae.fr")
Description: R-package proposing a framework to apply FAIR principles based on reports created using the bookdown R-package and published as a website (e.g. gitlab pages) and data located on a owncloud/nextcloud server.
License: AGPL (>= 3)
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
This diff is collapsed.
# Generated by roxygen2: do not edit by hand
export("%>%")
export(add_gitignore)
export(getDataPath)
export(iconv_filename)
export(loadConfig)
importFrom(magrittr,"%>%")
#' Add .gitignore file suitable for R project
#'
#' @inheritParams utils:download.file
#'
#' @return `NULL`, this function is used for side effect.
#' @export
#'
#' @examples
#' tmpfile <- tempfile()
#' add_gitignore(destfile = tmpfile)
#' readLines(tmpfile)
add_gitignore <- function(url = "https://raw.githubusercontent.com/github/gitignore/main/R.gitignore",
destfile = ".gitignore") {
stopifnot(download.file(url, destfile) == 0)
header <- add_comment_block("R template from:",
url,
paste("Downloaded the", Sys.time()))
s <- readLines(destfile)
s <- c(header, s)
writeLines(s, destfile)
}
add_comment <- function(x, comment_tag = "#") {
paste(comment_tag, x)
}
add_comment_block <- function(..., comment_tag = "#", line_len) {
x <- list(...)
c("",
strrep(comment_tag, 80),
sapply(x, add_comment, comment_tag = comment_tag),
strrep(comment_tag, 80),
"")
}
#' Give the complete path of the data and cache data on local disk
#'
#' @description
#' It supports Nextcloud public links in case of using cloud. In this case,
#' this function can download the file in a temporary
#' folder and returns the path of the downloaded file.
#' This functionality is useful for `read_excel` from the package *readxl*
#' (See: https://stackoverflow.com/a/41368947/5300212).
#'
#' @param path [character] representing the path of the data in the database
#' @param ... [character] path elements to add after `path`
#' @param use_cache [logical] download the data and use cache location
#' @param cfg a config object. Configuration to use. See [loadConfig] for details
#' @param root [character] URL or path of the database root
#' @param cache [character] path where downloaded files from cloud are cached (See details)
#'
#' @details
#' The cache directory is by default located in the system temporary folder in a
#' subdirectory named as the package name or the value of the environment variable
#' "PKG_DATA_CACHE" if it is defined.
#'
#' @return [character] with the path of the file to read either on:
#' - the local storage if `cfg$data$cloud == FALSE`
#' - the cache directory if `cfg$data$cloud == FALSE` and `use_cache == TRUE`
#' - the cloud server if `cfg$data$cloud == FALSE` and `use_cache == FALSE`
#' @export
#'
getDataPath <- function(path,
...,
use_cache = TRUE,
cfg = loadConfig(),
root = cfg$data$path,
cache = Sys.getenv("PKG_DATA_CACHE",
file.path(dirname(tempdir()),
utils::packageName()))) {
path <- file.path(path, ...)
stopifnot(is.character(path),
length(path) == 1)
if (cfg$data$cloud) {
file <- basename(path)
ext <- tools::file_ext(file)
folder <- dirname(path)
# model of link on Nextcloud https://nextcloud.inrae.fr/s/QbwR7yCo2HSDCb9/download?path=hydrometrie&files=Hydrometrie_site.csv
query <- file.path(root, "download")
query <- urltools::param_set(query,
key = "path",
value = urltools::url_encode(folder))
query <- urltools::param_set(query,
key = "files",
value = urltools::url_encode(file))
if (use_cache) {
exdir <- cache
if (folder != ".") {
exdir <- file.path(exdir, folder)
}
dir.create(exdir, showWarnings = FALSE, recursive = TRUE)
path <- file.path(exdir, file)
if (!file.exists(path)) {
if (cfg$data$cloud)
message("Download from URL: ", query)
resp <- httr::GET(query,
httr::write_disk(path))
if (!identical(httr::status_code(resp), 200L)) {
stop("Error ", httr::status_code(resp), " on query ", query)
}
if (ext == "") {
# Folder in ZIP format
zipfile <- paste(path, "zip", sep = ".")
file.rename(path, zipfile)
utils::unzip(zipfile, exdir = exdir)
}
}
return(path)
} else {
return(query)
}
} else {
return(file.path(root, path))
}
}
#' Convert a string to a compatible file name
#'
#' It removes accents and converts spaces and other special characters to an underscore.
#'
#' @param name [character] string to convert
#'
#' @return The converted [character] string.
#' @export
#'
#' @examples
#' iconv_filename("We don't like (wéïrd) àcçênts. And other things like $?€#%")
iconv_filename <- function(name) {
s <-purge_string(name)
s <- gsub(" +", "_", s)
s <- gsub("?", "", s, fixed = TRUE)
gsub('(_)\\1+', '\\1', s)
}
#' Read default configuration of the package and complete it with eventual user config
#'
#' @param userFile location of the user config YML file
#' @param pathDefaultCfg The location of the default configuration (located in "inst/config.yml" of the package by default)
#'
#' @return A configuration as it is returned by [config::get]
#' @export
#'
#' @examples
#' cfg <- loadConfig()
#' str(cfg)
loadConfig <- function(userFile = "config.yml", pathDefaultCfg = system.file("config.yml", package = "TalanoaHydro")) {
cfg <- config::get(file = pathDefaultCfg)
if (file.exists(userFile)) {cfg = config::merge(cfg,config::get(file = userFile))}
# By default results are written in the DB if the DB is local
if (is.null(cfg$data$write_results)) cfg$data$write_results <- !cfg$data$cloud
cfg
}
#' Pipe operator
#'
#' See \code{magrittr::\link[magrittr:pipe]{\%>\%}} for details.
#'
#' @name %>%
#' @rdname pipe
#' @keywords internal
#' @export
#' @importFrom magrittr %>%
#' @usage lhs \%>\% rhs
#' @param lhs A value or the magrittr placeholder.
#' @param rhs A function call using the magrittr semantics.
#' @return The result of calling `rhs(lhs)`.
NULL
Version: 1.0
RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default
EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8
RnwWeave: Sweave
LaTeX: pdfLaTeX
AutoAppendNewline: Yes
StripTrailingWhitespace: Yes
BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageRoxygenize: rd,collate,namespace
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/add_gitignore.R
\name{add_gitignore}
\alias{add_gitignore}
\title{Add .gitignore file suitable for R project}
\usage{
add_gitignore(
url = "https://raw.githubusercontent.com/github/gitignore/main/R.gitignore",
destfile = ".gitignore"
)
}
\value{
\code{NULL}, this function is used for side effect.
}
\description{
Add .gitignore file suitable for R project
}
\examples{
tmpfile <- tempfile()
add_gitignore(destfile = tmpfile)
readLines(tmpfile)
}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/getDataPath.R
\name{getDataPath}
\alias{getDataPath}
\title{Give the complete path of the data and cache data on local disk}
\usage{
getDataPath(
path,
...,
use_cache = TRUE,
cfg = loadConfig(),
root = cfg$data$path,
cache = Sys.getenv("PKG_DATA_CACHE", file.path(dirname(tempdir()),
utils::packageName()))
)
}
\arguments{
\item{path}{\link{character} representing the path of the data in the database}
\item{...}{\link{character} path elements to add after \code{path}}
\item{use_cache}{\link{logical} download the data and use cache location}
\item{cfg}{a config object. Configuration to use. See \link{loadConfig} for details}
\item{root}{\link{character} URL or path of the database root}
\item{cache}{\link{character} path where downloaded files from cloud are cached (See details)}
}
\value{
\link{character} with the path of the file to read either on:
\itemize{
\item the local storage if \code{cfg$data$cloud == FALSE}
\item the cache directory if \code{cfg$data$cloud == FALSE} and \code{use_cache == TRUE}
\item the cloud server if \code{cfg$data$cloud == FALSE} and \code{use_cache == FALSE}
}
}
\description{
It supports Nextcloud public links in case of using cloud. In this case,
this function can download the file in a temporary
folder and returns the path of the downloaded file.
This functionality is useful for \code{read_excel} from the package \emph{readxl}
(See: https://stackoverflow.com/a/41368947/5300212).
}
\details{
The cache directory is by default located in the system temporary folder in a
subdirectory named as the package name or the value of the environment variable
"PKG_DATA_CACHE" if it is defined.
}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/iconv_filename.R
\name{iconv_filename}
\alias{iconv_filename}
\title{Convert a string to a compatible file name}
\usage{
iconv_filename(name)
}
\arguments{
\item{name}{\link{character} string to convert}
}
\value{
The converted \link{character} string.
}
\description{
It removes accents and converts spaces and other special characters to an underscore.
}
\examples{
iconv_filename("We don't like (wéïrd) àcçênts. And other things like $?€#\%")
}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/loadConfig.R
\name{loadConfig}
\alias{loadConfig}
\title{Read default configuration of the package and complete it with eventual user config}
\usage{
loadConfig(
userFile = "config.yml",
pathDefaultCfg = system.file("config.yml", package = "TalanoaHydro")
)
}
\arguments{
\item{userFile}{location of the user config YML file}
\item{pathDefaultCfg}{The location of the default configuration (located in "inst/config.yml" of the package by default)}
}
\value{
A configuration as it is returned by \link[config:get]{config::get}
}
\description{
Read default configuration of the package and complete it with eventual user config
}
\examples{
cfg <- loadConfig()
str(cfg)
}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/utils-pipe.R
\name{\%>\%}
\alias{\%>\%}
\title{Pipe operator}
\usage{
lhs \%>\% rhs
}
\arguments{
\item{lhs}{A value or the magrittr placeholder.}
\item{rhs}{A function call using the magrittr semantics.}
}
\value{
The result of calling \code{rhs(lhs)}.
}
\description{
See \code{magrittr::\link[magrittr:pipe]{\%>\%}} for details.
}
\keyword{internal}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment