Skip to content

Commit

Permalink
v0.0.5-fix-comments-given-by-goodpractices
Browse files Browse the repository at this point in the history
  • Loading branch information
kauedesousa committed Jan 9, 2020
1 parent ff43bc5 commit 95d016f
Show file tree
Hide file tree
Showing 30 changed files with 274 additions and 233 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: chirps
Type: Package
Title: API Client for CHIRPS
Version: 0.0.4
Version: 0.0.5
Authors@R: c(person("Kaue", "de Sousa",
email = "kaue.desousa@inn.no", role = c("aut", "cre"),
comment = c(ORCID = "0000-0002-7571-7845")),
Expand Down
18 changes: 14 additions & 4 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,19 @@ S3method(get_esi,sf)
export(get_chirps)
export(get_esi)
export(precip_indices)
import(crul)
import(jsonlite)
import(methods)
import(sf)
importFrom(crul,HttpClient)
importFrom(jsonlite,fromJSON)
importFrom(jsonlite,toJSON)
importFrom(methods,addNextMethod)
importFrom(methods,asMethodDefinition)
importFrom(methods,assignClassDef)
importFrom(sf,read_sf)
importFrom(sf,st_as_sf)
importFrom(sf,st_buffer)
importFrom(sf,st_centroid)
importFrom(sf,st_geometry_type)
importFrom(sf,st_point)
importFrom(sf,st_sfc)
importFrom(sf,st_write)
importFrom(stats,quantile)
importFrom(tibble,as_tibble)
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
chirps 0.0.5 (2020-01-09)
=========================

### ENHANCEMENTS

* Fix comments given by good practice `goodpractice::gp()`. Avoid long code lines, it is bad for readability. Avoid 1:length(...), 1:nrow(...), 1:ncol(...), 1:NROW(...) and 1:NCOL(...) expressions. Not import packages as a whole, as this can cause name clashes between the imported packages.


chirps 0.0.4 (2020-01-03)
=========================

Expand Down
32 changes: 20 additions & 12 deletions R/GET.R
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
#' General function to get data from ClimateSERV API
#'
#' @param object a list with geojson strings
#' @param dates a character of start and end dates in that order in the format
#' MM/DD/YYYY
#' @param dates a character of start and end dates in that order in
#' the format MM/DD/YYYY
#' @param operation an integer that represents which type of statistical
#' operation to perform on the dataset
#' @param datatype an integer, the unique datatype number for the dataset which
#' this request operates on
#' @param datatype an integer, the unique datatype number for the dataset
#' which this request operates on
#' @return A data.frame with values
#' @details
#' operation: supported operations are max = 0, min = 1, median = 2, sum = 4,
#' average = 5
#' operation: supported operations are max = 0, min = 1, median = 2,
#' sum = 4, average = 5
#'
#' datatype: supported datatypes are Global CHIRPS = 0, Global ESI 4 Week = 29
#' datatype: supported datatypes are Global CHIRPS = 0,
#' Global ESI 4 Week = 29
#' datatype codes are described at
#' <https://climateserv.readthedocs.io/en/latest/api.html>
#'
Expand Down Expand Up @@ -56,7 +57,7 @@

# check request progress and wait
# until the request is done by the server
request_progress <- rep(FALSE, length(ids))
request_progress <- seq_along(ids) == FALSE

while (!all(request_progress)) {

Expand Down Expand Up @@ -84,13 +85,20 @@
# define ids
ids <- NULL
for(i in seq_along(result)) {
ids <- c(ids, rep(i, nrow(result[[i]])))

nr <- dim(result[[i]])[[1]]

ids <- c(ids, rep(i, nr))

}

result <- do.call("rbind", result)

if (nrow(result) == 0) {
stop("Fail to get valid values, try to increase the buffer area with 'dist' \n")
nr <- dim(result)[[1]]

if (nr == 0) {
stop("Fail to get valid values,
try to increase the buffer area with 'dist' \n")
}

# add ids
Expand All @@ -104,7 +112,7 @@

names(result) <- c("date", "value", "id")

rownames(result) <- 1:nrow(result)
rownames(result) <- seq_len(nr)

result <- result[, c("id", "date", "value")]

Expand Down
11 changes: 7 additions & 4 deletions R/chirps.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
#' @seealso
#'\strong{Useful links:}
#' \itemize{
#' \item{Development repository: \url{https://github.com/agrobioinfoservices/chirps}}
#' \item{Static documentation: \url{https://agrobioinfoservices.github.io/chirps/}}
#' \item{Report bugs: \url{https://github.com/agrobioinfoservices/chirps/issues}}
#' \item{Development repository:
#' \url{https://github.com/agrobioinfoservices/chirps}}
#' \item{Static documentation:
#' \url{https://agrobioinfoservices.github.io/chirps/}}
#' \item{Report bugs:
#' \url{https://github.com/agrobioinfoservices/chirps/issues}}
#' }
"_PACKAGE"
"_PACKAGE"
52 changes: 21 additions & 31 deletions R/get_chirps.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#' ClimateSERV works with geojson of type 'Polygon'. The input \code{object}
#' is then transformed into polygons with a small buffer area around the point.
#'
#' @param object input, an object of class \code{\link[base]{data.frame}},\code{geojson} or
#' \code{\link[sf]{sf}}
#' @param object input, an object of class \code{\link[base]{data.frame}},
#' \code{geojson} or \code{\link[sf]{sf}}
#' @param dates a character of start and end dates in that order in the format
#' "YYYY-MM-DD"
#' @param operation optional, an integer that represents which type of
Expand Down Expand Up @@ -125,10 +125,9 @@
#' # as.geojson = TRUE returns an object of class 'geojson'
#' dat <- get_chirps(object, dates, as.geojson = TRUE)
#'
#' }
#' @import sf
#' @import methods
#' @import jsonlite
#' }
#' @importFrom methods addNextMethod asMethodDefinition assignClassDef
#' @importFrom sf st_centroid read_sf st_geometry_type
#' @importFrom tibble as_tibble
#' @export
get_chirps <- function(object, dates, operation = 5, ...) {
Expand Down Expand Up @@ -194,48 +193,39 @@ get_chirps.sf <- function(object, dates, operation = 5,

type <- type[which(supp_type)]

n <- nrow(object)
nr <- dim(object)[[1]]

# find the sf_column
index <- attr(object, "sf_column")

# get the sf column
lonlat <- object[[index]]

if (type == "POINT") {

# unlist the sf_column
lonlat <- unlist(object[[index]])

lonlat <- matrix(lonlat,
nrow = n,
ncol = 2,
byrow = TRUE,
dimnames = list(1:n, c("lon","lat")))

lonlat <- as.data.frame(lonlat)

}

if (type == "POLYGON") {

lonlat <- object[[index]]

nl <- length(lonlat)

# set centroid to validade lonlat
lonlat <- sf::st_centroid(lonlat)

# unlist the sf_column
lonlat <- unlist(lonlat)

lonlat <- matrix(lonlat,
nrow = nl,
ncol = 2,
byrow = TRUE,
dimnames = list(1:nl, c("lon","lat")))

lonlat <- as.data.frame(lonlat)

}

lonlat <- matrix(lonlat,
nrow = nr,
ncol = 2,
byrow = TRUE,
dimnames = list(seq_len(nr), c("lon","lat")))

lonlat <- as.data.frame(lonlat)

# validate lonlat to check if they are within the CHIRPS range
.validate_lonlat(lonlat, xlim = c(-180, 180), ylim = c(-50, 50))

Expand All @@ -253,7 +243,7 @@ get_chirps.sf <- function(object, dates, operation = 5,
if (as.sf) {

result$date <- as.integer(result$date)
result$date <- paste0("d_",result$date)
result$date <- paste0("day_",result$date)

result <- split(result, result$date)

Expand Down Expand Up @@ -357,7 +347,7 @@ get_chirps.geojson <- function(object, dates, operation = 5,

lonlat <- as.data.frame(lonlat)

gjson <- split(object, 1:length(object))
gjson <- split(object, seq_along(object))

}

Expand All @@ -377,7 +367,7 @@ get_chirps.geojson <- function(object, dates, operation = 5,

result <- split(result, result$id)

object <- split(object, 1:length(object))
object <- split(object, seq_along(object))

# add geojson properties
result <- mapply(function(X, Y) {
Expand Down Expand Up @@ -411,4 +401,4 @@ get_chirps.geojson <- function(object, dates, operation = 5,

return(result)

}
}
46 changes: 19 additions & 27 deletions R/get_esi.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
#' then transformed into polygons with a small buffer area around the point.
#'
#' @inheritParams get_chirps
#' @param period an integer value for the period of ESI data, four weeks period = 1, twelve weeks = 2
#' @param period an integer value for the period of ESI data,
#' four weeks period = 1, twelve weeks = 2
#' @details
#' \bold{operation}: supported operations are:
#' \tabular{rll}{
Expand Down Expand Up @@ -193,49 +194,40 @@ get_esi.sf <- function(object, dates, operation = 5, period = 1,

type <- type[which(supp_type)]

n <- nrow(object)
nr <- dim(object)[[1]]

# find the sf_column
index <- attr(object, "sf_column")

# get the sf column
lonlat <- object[[index]]

if (type == "POINT") {

# unlist the sf_column
lonlat <- unlist(object[[index]])

lonlat <- matrix(lonlat,
nrow = n,
ncol = 2,
byrow = TRUE,
dimnames = list(1:n, c("lon","lat")))

lonlat <- as.data.frame(lonlat)

}

if (type == "POLYGON") {

lonlat <- object[[index]]

nl <- length(lonlat)

# set centroid to validade lonlat
lonlat <- sf::st_centroid(lonlat)

# unlist the sf_column
lonlat <- unlist(lonlat)

lonlat <- matrix(lonlat,
nrow = nl,
ncol = 2,
byrow = TRUE,
dimnames = list(1:nl, c("lon","lat")))

lonlat <- as.data.frame(lonlat)

}

# validate lonlat to check if they are within the CHIRPS range
lonlat <- matrix(lonlat,
nrow = nr,
ncol = 2,
byrow = TRUE,
dimnames = list(seq_len(nr), c("lon","lat")))

lonlat <- as.data.frame(lonlat)

# validate lonlat to check if they are within the ESI range
.validate_lonlat(lonlat, xlim = c(-180, 180), ylim = c(-50, 50))

# validate and reformat dates
Expand All @@ -260,7 +252,7 @@ get_esi.sf <- function(object, dates, operation = 5, period = 1,
if (as.sf) {

result$date <- as.integer(result$date)
result$date <- paste0("d_",result$date)
result$date <- paste0("day_",result$date)

result <- split(result, result$date)

Expand Down Expand Up @@ -361,7 +353,7 @@ get_esi.geojson <- function(object, dates, operation = 5, period = 1,

lonlat <- as.data.frame(lonlat)

gjson <- split(object, 1:length(object))
gjson <- split(object, seq_along(object))

}

Expand All @@ -388,7 +380,7 @@ get_esi.geojson <- function(object, dates, operation = 5, period = 1,

result <- split(result, result$id)

object <- split(object, 1:length(object))
object <- split(object, seq_along(object))

# add geojson properties
result <- mapply(function(X, Y) {
Expand Down Expand Up @@ -420,4 +412,4 @@ get_esi.geojson <- function(object, dates, operation = 5, period = 1,

return(result)

}
}
Loading

0 comments on commit 95d016f

Please sign in to comment.