Commit aa603638 authored by Grelot Frederic's avatar Grelot Frederic 🏊🏿
Browse files

geau Version: 1.0.0.0

parent 894615a6
geau Version: 1.0.0.0
0 errors ✔ | 0 warnings ✔ | 0 notes ✔
Note de version :
* NEW add.inset : ajout d'un encart dans une carte
* NEW kable_units : faire un kable en gérant les unités
DESCRIPTION
* Version: 1.0.0.0
R/add.inset.R
* Première version de la fonction
R/kable_units.R
* Première version de la fonction
data
* RAS
Documentation mise à jour
* add.inset.Rd
* kable_units.Rd
Vignettes
* RAS
dev
* package.development.R
* commit-message
presentation
MISC
* .Rbuildignore
* LICENSE.md: automatique
* NAMESPACE: automatique
package = "geau" ; devtools::document(package) ; devtools::load_all(package)
### Vignettes
# setwd(package):usethis::use_vignette(package);setwd("..")
devtools::build_vignettes(package)
### Examples
# devtools::run_examples(package)
### Checks
# system("mv ~/.Rprofile ~/.Rprofile-temp");devtools::check(package);system("mv ~/.Rprofile-temp ~/.Rprofile")
### Build
devtools::build(package, path = "library", vignettes = TRUE)
### install -> sudo
# devtools::install_local(package)
\ No newline at end of file
...@@ -5,3 +5,4 @@ ...@@ -5,3 +5,4 @@
^presentation$ ^presentation$
^temp$ ^temp$
^.vscode$ ^.vscode$
^LICENSE\.md$
Package: geau
Title: Utilities very useful to share within geau-inondation team
Version: 1.0.0.0
Authors@R:
person(given = "Frédéric",
family = "Grelot",
role = c("aut", "cre"),
email = "frederic.grelot@inrae.fr",
comment = c(ORCID = "YOUR-ORCID-ID"))
Description: This package collects some very useful utilities to work in a
collaborative way within geau-inondation.
License: GPL (>= 3)
Encoding: UTF-8
Depends: R (>= 3.4.0)
LazyData: true
Imports:
kableExtra,
knitr,
rio,
terra,
sf
Suggests:
rmarkdown,
testthat
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.1
VignetteBuilder: knitr
This diff is collapsed.
# Generated by roxygen2: do not edit by hand
export(add.inset)
export(kable_units)
#' @title Add an inset within a map
#'
#' @param x should be something of type polygon readable by terra::vect
#' @param scale numeric, how big should be the inset relative to the whole map
#' @param loc character, vwhere to put the inset
#' @param background character, color to be used for the background
#' @param ... some parameters that will be used by terra::plot
#'
#' @return polyVect transformed to be placed somewhere in the map
#'
#' @export
#'
#' @encoding UTF-8
#' @author This is an adaptation of the function inset of the terra package.
#'
#' @examples
#'
#' \dontrun{
#' # To be added (soon)
#' }
add.inset = function (x, scale = 0.2, loc = "", background = "white", ...)
{
e_usr = terra::ext(graphics::par("usr"))
x = try(terra::vect(x))
e = terra::ext(x)
r = (max(c(diff(e_usr[1:2]) / diff(e[1:2]), diff(e_usr[3:4]) / diff(e[3:4]))) * scale)^2
y = terra::rescale(x, f = r)
e = terra::ext(y)
if (loc != "") {
stopifnot(loc %in% c("bottomright", "bottom", "bottomleft",
"left", "topleft", "top", "topright", "right", "center"))
if (grepl("top", loc)) {
dy = e_usr[4] - e[4]
} else if (grepl("bottom", loc)) {
dy = e_usr[3] - e[3]
} else {
dy = (e_usr[3] + diff(e_usr[3:4])/2) - (e[3] + diff(e[3:4])/2)
}
if (grepl("left", loc)) {
dx = e_usr[1] - e[1]
} else if (grepl("right", loc)) {
dx = e_usr[2] - e[2]
} else {
dx = (e_usr[1] + diff(e_usr[1:2])/2) - (e[1] + diff(e[1:2])/2)
}
} else {
dx = e_usr[1] - e[1]
dy = e_usr[4] - e[4]
}
y = terra::shift(y, dx, dy)
e = terra::ext(y)
y = terra::rescale(y, f = .8)
if (!is.na(background)) {
terra::polys(
terra::rescale(terra::as.polygons(e), .9),
col = background,
lty = 1, lwd = 2, border = "lightgray")
}
terra::plot(y, ..., add = TRUE)
invisible(y)
}
#' @title Replace kable with some useful
#'
#' @param x data.frame or path to a file
#' @param read function how to read the file
#' @param align character,
#' @param linesep integer, position of linesep
#' @param ... some parameters that will be used by other functions
#'
#' @return A character vector of the table source code.
#'
#' @export
#'
#' @encoding UTF-8
#' @author This is an adaptation of the function inset of the terra package.
#'
#' @examples
#'
#' temp = head(iris)[c(5, 1:4)]
#' temp[["Species"]] = as.character(temp[["Species"]])
#' kable_units(head(temp))
#' temp = rbind(temp, c(Species = "mean", as.list(colMeans(temp[-1]))))
#' kable_units(temp, -1)
#' temp = rbind(temp, c("units", "mm", "mm", "mm", "mm"))
#' kable_units(temp, -1)
kable_units = function(x, linesep, align = NULL, read = NULL, ...) {
if (!is.data.frame(x) && !is.matrix(x)) {
if (!is.null(read)) {
value = read(x, ...)
} else {
value = rio::import(x, ...)
}
} else {
value = as.data.frame(x)
}
unit = grep("^[uU]nit", value[, 1])
if (missing(linesep)) {
linesep = ""
} else {
position = if (is.logical(linesep)) which(linesep) else as.integer(linesep)
linesep = rep("", nrow(value))
linesep[position[position > 0]] = "\\midrule"
linesep[nrow(value) - (length(unit) == 1) + position[position < 0]] = "\\midrule"
}
if(length(unit) == 1) {
if (is.null(align)) align = c("l", rep("r", ncol(value) - 1))
result = knitr::kable(rbind(value[-unit, ], value[unit, ]), align = align,
booktabs = TRUE, format = "latex", row.names = FALSE, linesep = linesep, ...)
result = kableExtra::row_spec(result, 0, bold = TRUE, hline_after = FALSE)
result = kableExtra::row_spec(result, nrow(value) - 1, extra_latex_after = "\\midrule")
result = kableExtra::row_spec(result, nrow(value), italic = TRUE)
} else {
result = knitr::kable(value, align = align,
booktabs = TRUE, format = "latex", row.names = FALSE, linesep = linesep, ...)
result = kableExtra::row_spec(result, 0, bold = TRUE, hline_after = FALSE)
}
return(result)
}
\ No newline at end of file
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/add.inset.R
\encoding{UTF-8}
\name{add.inset}
\alias{add.inset}
\title{Add an inset within a map}
\usage{
add.inset(x, scale = 0.2, loc = "", background = "white", ...)
}
\arguments{
\item{x}{should be something of type polygon readable by terra::vect}
\item{scale}{numeric, how big should be the inset relative to the whole map}
\item{loc}{character, vwhere to put the inset}
\item{background}{character, color to be used for the background}
\item{...}{some parameters that will be used by terra::plot}
}
\value{
polyVect transformed to be placed somewhere in the map
}
\description{
Add an inset within a map
}
\examples{
\dontrun{
# To be added (soon)
}
}
\author{
This is an adaptation of the function inset of the terra package.
}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/kable_units.R
\encoding{UTF-8}
\name{kable_units}
\alias{kable_units}
\title{Replace kable with some useful}
\usage{
kable_units(x, linesep, align = NULL, read = NULL, ...)
}
\arguments{
\item{x}{data.frame or path to a file}
\item{linesep}{integer, position of linesep}
\item{align}{character,}
\item{read}{function how to read the file}
\item{...}{some parameters that will be used by other functions}
}
\value{
A character vector of the table source code.
}
\description{
Replace kable with some useful
}
\examples{
temp = head(iris)[c(5, 1:4)]
temp[["Species"]] = as.character(temp[["Species"]])
kable_units(head(temp))
temp = rbind(temp, c(Species = "mean", as.list(colMeans(temp[-1]))))
kable_units(temp, -1)
temp = rbind(temp, c("units", "mm", "mm", "mm", "mm"))
kable_units(temp, -1)
}
\author{
This is an adaptation of the function inset of the terra package.
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment