diff --git a/DESCRIPTION b/DESCRIPTION index 88be796..43bc2b9 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,44 +1,62 @@ -Package: abjutils Type: Package -Date: 2018-01-18 -Title: Useful Tools for Jurimetrical Analysis Used by the Brazilian Jurimetrics Association -Description: The Brazilian Jurimetrics Association (ABJ in Portuguese, - see for more information) is a non-profit - organization which aims to investigate and promote the use of - statistics and probability in the study of Law and its institutions. - This package implements general purpose tools used by ABJ, such as functions - for sampling and basic manipulation of Brazilian lawsuits identification - number. It also implements functions for text cleaning, such as accentuation removal. +Package: abjutils +Title: Useful Tools for Jurimetrical Analysis Used by the + Brazilian Jurimetrics Association Version: 0.2.3 -Authors@R: c( - person("Julio", "Trecenti", role = "aut", email = "julio.trecenti@gmail.com"), - person("Athos", "Damiani", role = "ctb", email = "athos.damiani@gmail.com"), - person("Fernando", "Correa", role = "aut", email = "fcorrea@abj.org.br"), - person("Caio", "Lente", role = c("aut","cre"), email = "caio.lente@usp.br"), - person(family = "Brazilian Jurimetrics Association", role = "cph")) +Date: 2018-01-18 +Authors@R: + c(person(given = "Julio", + family = "Trecenti", + role = "aut", + email = "julio.trecenti@gmail.com"), + person(given = "Athos", + family = "Damiani", + role = "ctb", + email = "athos.damiani@gmail.com"), + person(given = "Fernando", + family = "Correa", + role = "aut", + email = "fcorrea@abj.org.br"), + person(given = "Caio", + family = "Lente", + role = c("aut", "cre"), + email = "caio.lente@usp.br"), + person(family = "Brazilian Jurimetrics Association", + role = "cph")) Maintainer: Caio Lente -URL: https://github.com/abjur/abjutils -LazyData: TRUE -Depends: R (>= 3.4) +Description: The Brazilian Jurimetrics Association (ABJ in + Portuguese, see for more information) is + a non-profit organization which aims to investigate and promote the + use of statistics and probability in the study of Law and its + institutions. This package implements general purpose tools used by + ABJ, such as functions for sampling and basic manipulation of + Brazilian lawsuits identification number. It also implements functions + for text cleaning, such as accentuation removal. License: MIT + file LICENSE +URL: https://github.com/abjur/abjutils +Depends: + R (>= 3.4) Imports: - stringr, - dplyr, - httr, - tibble, - devtools, - magrittr, - purrr, - rstudioapi, - scales, - stringi, - glue, - tidyr, - progress, - furrr, - future, - readr, - rlang -Suggests: testthat + devtools, + dplyr, + furrr, + future, + glue, + httr, + magrittr, + progress, + purrr, + readr, + rlang, + rstudioapi, + scales, + stringi, + stringr, + tibble, + tidyr +Suggests: + testthat +LazyData: TRUE +Roxygen: list(markdown = TRUE, roclets = c("rd", "namespace", + "collate")) RoxygenNote: 6.1.1 -Roxygen: list(markdown = TRUE, roclets = c("rd", "namespace", "collate")) \ No newline at end of file diff --git a/R/carf.R b/R/carf.R index 7ffed82..205dded 100644 --- a/R/carf.R +++ b/R/carf.R @@ -27,8 +27,6 @@ verify_length <- function(val) { #' carf_calc_dig("10120.008427/2003", build = TRUE) #' carf_calc_dig("15374.002430/99", build = FALSE) #' carf_calc_dig(c("101200084272003", "1537400243099")) -#' -#' # will fail #' \donttest{ #' carf_calc_dig("10766.000511/96-12") #' } @@ -95,4 +93,4 @@ carf_build_id <- function(id) { } else { stop ("Length must be 15 or 17.") } -} \ No newline at end of file +} diff --git a/R/cnj.R b/R/cnj.R index 3ecb7ba..0052503 100644 --- a/R/cnj.R +++ b/R/cnj.R @@ -1,3 +1,4 @@ + #' @title Calculate digits for Brazilian lawsuit identification numbers #' #' @description Returns the check digit of a lawsuit numbers in the format @@ -13,8 +14,6 @@ #' @examples { #' calc_dig("001040620018260004", build = TRUE) #' calc_dig("001040620018260004", build = FALSE) -#' -#' #will fail #' \dontrun{ #' calc_dig("00104062001826000", build = TRUE) #' } @@ -253,4 +252,3 @@ pattern_cnj <- function() { clean_cnj <- function(x) { stringr::str_replace_all(x, '[^0-9]','') } - diff --git a/R/esaj.R b/R/esaj.R index 59a4b64..0a57894 100644 --- a/R/esaj.R +++ b/R/esaj.R @@ -1,22 +1,22 @@ #' @title Gather subjects from esaj::cjsg_table("subjects") -#' +#' #' @description Once you run `esaj::cjsg_table("subjects")`, you can #' use this function to gather the subjects automatically. Download #' `esaj` by running `devtools::install_github("courtsbr/esaj")`. -#' +#' #' @param subjects Table returned by `esaj::cjsg_table("subjects")` -#' +#' #' @export gather_subjects <- function(subjects) { unite_index <- function(d, i) { tidyr::unite_(d, paste0("level", i), paste0(c("id", "name"), i)) } col <- readr::cols(.default = readr::col_character()) - purrr::reduce(0:5, unite_index, .init = subjects) %>% - tidyr::gather() %>% - tidyr::separate(value, c("id", "nm"), sep = "_") %>% - dplyr::distinct(id, .keep_all = TRUE) %>% - readr::type_convert(col_types = col) %>% + purrr::reduce(0:5, unite_index, .init = subjects) %>% + tidyr::gather() %>% + tidyr::separate(value, c("id", "nm"), sep = "_") %>% + dplyr::distinct(id, .keep_all = TRUE) %>% + readr::type_convert(col_types = col) %>% dplyr::filter(!is.na(id)) } diff --git a/R/escape_unicode.R b/R/escape_unicode.R index 30507b9..7aea201 100644 --- a/R/escape_unicode.R +++ b/R/escape_unicode.R @@ -1,13 +1,13 @@ #' @title Escape accented characters in a document -#' +#' #' @description This function is used by the "Escape Unicode" add-in #' and removes all accented characters from the current file, replacing #' them by their equivalent Unicode-escaped values. -#' +#' #' @export escape_unicode <- function() { - + # Escape accented characters from one character vector escape <- . %>% stringi::stri_escape_unicode() %>% @@ -15,12 +15,12 @@ escape_unicode <- function() { stringi::stri_replace_all_fixed("\\", "") %>% stringi::stri_replace_all_fixed("#$!*%", "\\") %>% stringr::str_replace_all("(u00[:alnum:]{2})", "\\\\\\1") - + # Escape accented characters from every line of the document escape_all <- . %>% purrr::map_chr(escape) %>% stringr::str_c(collapse = "\n") - + # Get text of document and escape accented characters doc <- rstudioapi::getSourceEditorContext() diff --git a/R/pvec.R b/R/pvec.R index 2dd6ac4..0da9a76 100644 --- a/R/pvec.R +++ b/R/pvec.R @@ -17,26 +17,26 @@ #' and the returned object is flattened (a vector, a list, or a tibble) #' @param .options Options passed on to [furrr::future_map()] #' ([furrr::future_options()] by default) -#' +#' #' @seealso [purrr::map()], [furrr::future_map()], [furrr::future_options()] -#' +#' #' @return A tibble with 3 columns: input, return, and output #' @export pvec <- function(.x, .f, ..., .cores = get_cores(), .progress = TRUE, .flatten = FALSE, .options = future_options()) { - + # Preserve execution plan oplan <- future::plan() on.exit(future::plan(oplan), add = TRUE) - + # Set execution plan to multicore future::plan(future::multicore, workers = .cores) - + # Capture function side-effects .f <- purrr::safely(purrr::as_mapper(.f)) - + # Run future map out <- furrr::future_map(.x, .f, ..., .progress = .progress, .options = .options) - + # Compact with care compact_ <- function(x) { if (is.null(x[[1]]) && is.null(x[[2]])) { @@ -46,35 +46,37 @@ pvec <- function(.x, .f, ..., .cores = get_cores(), .progress = TRUE, .flatten = return(purrr::compact(x)) } } - + # Process output pout <- out %>% purrr::map(compact_) %>% purrr::flatten() %>% tibble::tibble( id = purrr::`%||%`(names(.x), seq_along(.x)), - return = names(.), output = .) - + return = names(.), output = . + ) + # Flatten results if necessary if (.flatten) { - n_error <- length(pout$return[pout$return == "error"]) if (n_error > 0) { warning( "Since '.flatten = TRUE', a total of ", n_error, - " errors are being ignored", call. = FALSE) + " errors are being ignored", + call. = FALSE + ) } - + pout <- pout %>% dplyr::filter(return != "error") %>% - dplyr::select(-return) %>% + dplyr::select(-return) %>% tidyr::unnest() - + if (ncol(pout) == 1) { pout <- dplyr::pull(pout, output) } } - + return(pout) } diff --git a/R/sample_cnj.R b/R/sample_cnj.R index 9e1a5d2..7e94590 100644 --- a/R/sample_cnj.R +++ b/R/sample_cnj.R @@ -1,11 +1,11 @@ #' @title Generate sample Brazilian lawsuit identification numbers -#' -#' @description Returns a data frame containing a random sample of lawsuit +#' +#' @description Returns a data frame containing a random sample of lawsuit #' numbers distributed according to some regional and jurisdictional parameters. -#' The implementation supports both vector and scalar parameters, depending +#' The implementation supports both vector and scalar parameters, depending #' whether or not the function should uniformly sample from a scope of lawsuit #' numbers or one should define the parameters for each sample unit. -#' +#' #' @param n A non negative integer giving the number of codes to generate #' @param foros One or more strings with 4 characters indicating the juridical #' forum for the sampled codes @@ -20,79 +20,88 @@ #' @param sample_pars Whether or not the parameters define the characteristics #' of the codes #' @param return_df Whether or not the function should return a data frame -#' +#' #' @return A data frame or a vector containing a random sample of lawsuits IDs -#' -#' @examples { -#' #sampling the parameters -#' sample_cnj(3, foros = "0000", -#' anos = "2015", orgao = 8, tr = 26, -#' first_dig = "0",sample_pars = TRUE, return_df = FALSE) +#' +#' @examples +#' { +#' # sampling the parameters +#' sample_cnj(3, +#' foros = "0000", +#' anos = "2015", orgao = 8, tr = 26, +#' first_dig = "0", sample_pars = TRUE, return_df = FALSE +#' ) #' -#' sample_cnj(10, foros = c("0000","0001"), -#' anos = c("2014","2015"), orgao = 8, tr = 26, -#' first_dig = "0",sample_pars = TRUE, return_df = FALSE) +#' sample_cnj(10, +#' foros = c("0000", "0001"), +#' anos = c("2014", "2015"), orgao = 8, tr = 26, +#' first_dig = "0", sample_pars = TRUE, return_df = FALSE +#' ) #' -#' #not sampling the parameters +#' # not sampling the parameters #' -#' sample_cnj(3, foros = c("0000","0001","0002"), -#' anos = c("2014","2015","2016"), orgao = rep(8,3), tr = rep(26,3), -#' first_dig = "0",sample_pars = FALSE, return_df = FALSE) +#' sample_cnj(3, +#' foros = c("0000", "0001", "0002"), +#' anos = c("2014", "2015", "2016"), orgao = rep(8, 3), tr = rep(26, 3), +#' first_dig = "0", sample_pars = FALSE, return_df = FALSE +#' ) #' } -#' #' @export -sample_cnj <- function(n, foros, anos, orgao, tr, first_dig = '0', - sample_pars = TRUE, return_df = TRUE){ +sample_cnj <- function(n, foros, anos, orgao, tr, first_dig = "0", + sample_pars = TRUE, return_df = TRUE) { # checks - if(sample_pars){ + if (sample_pars) { foros <- build_params_list(foros, n) anos <- build_params_list(anos, n) orgao <- build_params_list(orgao, n) tr <- build_params_list(tr, n) } else { - lengths <- sapply(list(foros, anos, orgao, tr), length) - + max_length <- max(lengths) - + logical_test <- max_length != min(lengths) - - if(logical_test){ + + if (logical_test) { stop("When sample_pars is FALSE, sample_cnj expects foros, anos, orgaos and tr with same length.") } else { - if(max_length != 1 & (max_length != n)){ + if (max_length != 1 & (max_length != n)) { stop("When sample_pars is FALSE, sample_cnj expects parameters lengths to be equal to n.") } } } - #end of checks - + # end of checks + serial_size <- ifelse(first_dig == "", 9, 8) - - #main code + + # main code ret <- runif(n) %>% - as.character %>% - stringr::str_sub(3,serial_size) %>% + as.character() %>% + stringr::str_sub(3, serial_size) %>% dplyr::data_frame() %>% - stats::setNames("serial") %>% - dplyr::mutate(no_cd_code = sprintf("%s%s%s%s%s%s", first_dig, - serial, anos, orgao, tr, foros), - n_processo = calc_dig(no_cd_code, build = T)) %>% + stats::setNames("serial") %>% + dplyr::mutate( + no_cd_code = sprintf( + "%s%s%s%s%s%s", first_dig, + serial, anos, orgao, tr, foros + ), + n_processo = calc_dig(no_cd_code, build = T) + ) %>% dplyr::select(n_processo) - - #df or vec - if(return_df){ + + # df or vec + if (return_df) { return(ret) } else { return(with(ret, n_processo)) } } -build_params_list <- function(x, n){ - if (length(x) > 1){ +build_params_list <- function(x, n) { + if (length(x) > 1) { sample(as.character(x), n, replace = T) } else { rep(x, n) } -} \ No newline at end of file +} diff --git a/R/tabela.R b/R/tabela.R index c654dcf..f2132ed 100755 --- a/R/tabela.R +++ b/R/tabela.R @@ -1,22 +1,22 @@ #' @title Produce frequency and relative frequency tables -#' +#' #' @description Produces a contingency table of the elements of a vector #' calculating relative frequencies as well. -#' +#' #' @param x A vector -#' @param label Quoted name of the column to create in output -#' +#' @param label Quoted name of the column to create in output +#' #' @return A data frame containing frequency and relative frequencies for #' the levels of x -#' +#' #' @export -tabela <- function(x, label='variavel') { +tabela <- function(x, label = "variavel") { tab1 <- data.frame(table(x)) - tab2 <- data.frame(prop.table(table(x))*100) - tab3 <- merge(tab1,tab2, by='x') - tab3 <- tab3[order(tab3$Freq.y, decreasing=T),] - tab3$Freq.y <- round(tab3$Freq.y,2) + tab2 <- data.frame(prop.table(table(x)) * 100) + tab3 <- merge(tab1, tab2, by = "x") + tab3 <- tab3[order(tab3$Freq.y, decreasing = T), ] + tab3$Freq.y <- round(tab3$Freq.y, 2) tab3 <- as.data.frame(rbind(as.matrix(tab3), c("Total", sum(tab1$Freq), "100.0"))) - names(tab3) <- c(label, 'Freq\032ncia', ' %') + names(tab3) <- c(label, "Freq\032ncia", " %") tab3 } diff --git a/R/table_utils.R b/R/table_utils.R index a1dc09f..b0aed9a 100644 --- a/R/table_utils.R +++ b/R/table_utils.R @@ -1,24 +1,27 @@ -prettify_number <- function(d, number = T, percent = T, ...){ - - if(percent){ - query <- '{fmt_p(x/sum(x))}' - } - - if(number) { - query <- sprintf('{fmt(x)} (%s)', glue::glue(query)) +prettify_number <- function(d, number = T, percent = T, ...) { + if (percent) { + query <- "{fmt_p(x/sum(x))}" } - - d %>% - dplyr::select(...) %>% - dplyr::mutate_if(is.numeric, .funs = function(x){glue::glue(query)}) %>% + + if (number) { + query <- sprintf("{fmt(x)} (%s)", glue::glue(query)) + } + + d %>% + dplyr::select(...) %>% + dplyr::mutate_if(is.numeric, .funs = function(x) { + glue::glue(query) + }) %>% dplyr::as_data_frame() } # adapted from plyr package fmt <- function(x) { - format(x, big.mark = '.', small.mark = ',', - decimal.mark = ',', - scientific = FALSE, trim = TRUE) + format(x, + big.mark = ".", small.mark = ",", + decimal.mark = ",", + scientific = FALSE, trim = TRUE + ) } # adapted from plyr package diff --git a/R/test_fun.R b/R/test_fun.R index 81c0d19..afe9ee6 100644 --- a/R/test_fun.R +++ b/R/test_fun.R @@ -1,17 +1,19 @@ #' @title Tests a function by checking if its arguments are declared -#' +#' #' @description This function verifies whether all of the arguments of another #' function already have assigned values. If an argument has a default value #' but there isn't a corresponding variable, it creates that variable. -#' +#' #' @param f A function #' @param force_default Whether or not to assign the default value to arguments #' that already have assigned values -#' +#' #' @examples #' \dontrun{ -#' f <- function(a, b = 3) { a*b } +#' f <- function(a, b = 3) { +#' a * b +#' } #' #' test_fun(f) #' a @@ -34,12 +36,11 @@ #' #' @export test_fun <- function(f, force_default = FALSE) { - args <- names(formals(f)) vals <- formals(f) - + does_exist <- purrr::map_lgl(args, exists, envir = rlang::env_parent()) - + for (i in seq_along(args)) { if (does_exist[[i]]) { if (force_default) { @@ -54,6 +55,6 @@ test_fun <- function(f, force_default = FALSE) { message(paste0("Argument named '", args[[i]], "' needs a value!")) } } - + invisible(does_exist) } diff --git a/R/utils.R b/R/utils.R index b4cfe17..c03f198 100644 --- a/R/utils.R +++ b/R/utils.R @@ -1,6 +1,6 @@ #' @title Convert Chrome's Query String Parameters to a list -#' +#' #' @description To use this function, simply copy the Query String #' Parameters returned by Chrome when analysing the network flow of #' a web page. Paste these QSPs into an R string with double quotes @@ -9,16 +9,16 @@ #' formatted command that creates a list with the QSPs. This list #' works perfectly with [httr::GET()] and [httr::POST()] so that #' you can easily reproduce a website's behavior. -#' +#' #' @param x A string with Chrome's Query String Parameters -#' +#' #' @seealso [httr::GET()], [httr::POST()] -#' +#' #' @export chrome_to_body <- function(x) { x <- unlist(strsplit(x, "\n")) x_split <- stringr::str_split_fixed(x, "\\:(?=[^\\:]*$)", 2) - x_split[,2] <- stringr::str_trim(x_split[,2]) + x_split[, 2] <- stringr::str_trim(x_split[, 2]) x_unite <- sprintf('"%s" = "%s"', x_split[, 1], x_split[, 2]) x_unite <- paste(x_unite, collapse = ",\n") x_unite <- paste0("list(\n", x_unite, ")") @@ -27,39 +27,38 @@ chrome_to_body <- function(x) { } #' Shortcut to write file to "data/" directory from a pipe -#' +#' #' @param x Object to write #' @param name Name of the object (important when loading) #' @param dir Directory where to save file -#' +#' #' @export write_data <- function(x, name, dir = "data/") { - assign(name, x) save(list = name, file = stringr::str_c(dir, "/", name, ".rda")) rm(name) - + return(x) } #' Extract file name without extension #' #' @param x Character vector of file paths -#' +#' #' @export file_sans_ext <- function(x) { basename(tools::file_path_sans_ext(x)) } #' @title Remove accentuation -#' +#' #' @description Remove accented characters from strings converting them to #' ASCII. -#' +#' #' @param x A string vector -#' +#' #' @return A version of `x` without non-ASCII characters -#' +#' #' @export rm_accent <- function(x) { stringi::stri_trans_general(x, "Latin-ASCII") @@ -77,14 +76,14 @@ rm_accent <- function(x) { #' @param decreasing Should the sorting be decreasing? #' @param head Should [utils::head()] function be used for printing? #' @param n How many lines [utils::head()] function should show? -#' +#' #' @references http://stackoverflow.com/questions/1358003/tricks-to-manage-the-available-memory-in-an-r-session -#' +#' #' @export -lsos <- function (pos = 1, pattern, order.by = "Size", - decreasing=TRUE, head=TRUE, n=10) { +lsos <- function(pos = 1, pattern, order.by = "Size", + decreasing = TRUE, head = TRUE, n = 10) { napply <- function(names, fn) sapply(names, function(x) - fn(get(x, pos = pos))) + fn(get(x, pos = pos))) names <- ls(pos = pos, pattern = pattern) obj.class <- napply(names, function(x) as.character(class(x))[1]) obj.mode <- napply(names, mode) @@ -96,59 +95,68 @@ lsos <- function (pos = 1, pattern, order.by = "Size", obj.dim[vec, 1] <- napply(names, length)[vec] out <- data.frame(obj.type, obj.size, obj.dim) names(out) <- c("Type", "Size", "Rows", "Columns") - if (!missing(order.by)) - out <- out[order(out[[order.by]], decreasing=decreasing), ] - if (head) + if (!missing(order.by)) { + out <- out[order(out[[order.by]], decreasing = decreasing), ] + } + if (head) { out <- utils::head(out, n) + } out } #' @title Add pipe template -#' +#' #' @description Adds pipe template to package documentation. -#' +#' #' @param pkg Package description (can be path or package name) -#' +#' #' @export -use_pipe <- function(pkg = '.') { +use_pipe <- function(pkg = ".") { pkg <- devtools::as.package(pkg) - devtools::use_package('magrittr', pkg = pkg) - txt_pipe <- readLines(system.file('pipe-op.R', - package = 'abjutils')) - cat(txt_pipe, file = paste0(pkg$path, '/R/utils.R'), - append = TRUE, sep = '\n') + devtools::use_package("magrittr", pkg = pkg) + txt_pipe <- readLines(system.file("pipe-op.R", + package = "abjutils" + )) + cat(txt_pipe, + file = paste0(pkg$path, "/R/utils.R"), + append = TRUE, sep = "\n" + ) devtools::document() } #' Mirror of scales:::precision() -#' +#' #' @param x See scales:::precision() -#' +#' #' @export precision <- function(x) { rng <- range(x, na.rm = TRUE) - span <- if (scales::zero_range(rng)) + span <- if (scales::zero_range(rng)) { abs(rng[1]) - else diff(rng) - if (span == 0) + } else { + diff(rng) + } + if (span == 0) { return(1) + } 10^floor(log10(span)) } #' Convert brazilian currency values (text) to numeric -#' +#' #' @param x A currency vector. Ex: c("R$ 10.000,00", "R$ 123,00") -#' +#' #' @export reais <- function(x) { - x %>% - stringr::str_remove("R\\$") %>% - stringr::str_remove(".") %>% - stringr::str_replace_all(",", "\\.") %>% + x %>% + stringr::str_remove("R\\$") %>% + stringr::str_remove(".") %>% + stringr::str_replace_all(",", "\\.") %>% as.numeric() } # Get rid of NOTEs globalVariables(c( - ".","item","object.size","%>%", "n_processo", "runif", "serial", "no_cd_code", "warn", - "output", "input", "result", "value", "id")) + ".", "item", "object.size", "%>%", "n_processo", "runif", "serial", "no_cd_code", "warn", + "output", "input", "result", "value", "id" +)) diff --git a/man/calc_dig.Rd b/man/calc_dig.Rd index bfb81e4..5a075a6 100644 --- a/man/calc_dig.Rd +++ b/man/calc_dig.Rd @@ -24,8 +24,6 @@ unified by the brazillian National Council of Justice. { calc_dig("001040620018260004", build = TRUE) calc_dig("001040620018260004", build = FALSE) - -#will fail \dontrun{ calc_dig("00104062001826000", build = TRUE) } diff --git a/man/carf_calc_dig.Rd b/man/carf_calc_dig.Rd index fc3afbb..e610b8d 100644 --- a/man/carf_calc_dig.Rd +++ b/man/carf_calc_dig.Rd @@ -27,8 +27,6 @@ the check digit. carf_calc_dig("10120.008427/2003", build = TRUE) carf_calc_dig("15374.002430/99", build = FALSE) carf_calc_dig(c("101200084272003", "1537400243099")) - -# will fail \donttest{ carf_calc_dig("10766.000511/96-12") } diff --git a/man/sample_cnj.Rd b/man/sample_cnj.Rd index bf7bdd4..df713e3 100644 --- a/man/sample_cnj.Rd +++ b/man/sample_cnj.Rd @@ -42,20 +42,25 @@ numbers or one should define the parameters for each sample unit. } \examples{ { -#sampling the parameters -sample_cnj(3, foros = "0000", -anos = "2015", orgao = 8, tr = 26, -first_dig = "0",sample_pars = TRUE, return_df = FALSE) + # sampling the parameters + sample_cnj(3, + foros = "0000", + anos = "2015", orgao = 8, tr = 26, + first_dig = "0", sample_pars = TRUE, return_df = FALSE + ) -sample_cnj(10, foros = c("0000","0001"), -anos = c("2014","2015"), orgao = 8, tr = 26, -first_dig = "0",sample_pars = TRUE, return_df = FALSE) + sample_cnj(10, + foros = c("0000", "0001"), + anos = c("2014", "2015"), orgao = 8, tr = 26, + first_dig = "0", sample_pars = TRUE, return_df = FALSE + ) -#not sampling the parameters + # not sampling the parameters -sample_cnj(3, foros = c("0000","0001","0002"), -anos = c("2014","2015","2016"), orgao = rep(8,3), tr = rep(26,3), -first_dig = "0",sample_pars = FALSE, return_df = FALSE) + sample_cnj(3, + foros = c("0000", "0001", "0002"), + anos = c("2014", "2015", "2016"), orgao = rep(8, 3), tr = rep(26, 3), + first_dig = "0", sample_pars = FALSE, return_df = FALSE + ) } - } diff --git a/man/test_fun.Rd b/man/test_fun.Rd index d9d026e..b801d18 100644 --- a/man/test_fun.Rd +++ b/man/test_fun.Rd @@ -19,7 +19,9 @@ but there isn't a corresponding variable, it creates that variable. } \examples{ \dontrun{ -f <- function(a, b = 3) { a*b } +f <- function(a, b = 3) { + a * b +} test_fun(f) a diff --git a/tests/testthat/test-check_dig.R b/tests/testthat/test-check_dig.R index dd76523..dd3524c 100644 --- a/tests/testthat/test-check_dig.R +++ b/tests/testthat/test-check_dig.R @@ -11,4 +11,4 @@ test_that("check_dig verifies if the check digit is well formed.", { expect_equal(check_dig("0005268-75.2013.8.26.0100"), TRUE) expect_equal(check_dig("0010406-40.2001.8.26.0004"), TRUE) expect_equal(check_dig("0010406-50.2001.8.26.0004"), FALSE) -}) \ No newline at end of file +}) diff --git a/tests/testthat/test-rm_accent.R b/tests/testthat/test-rm_accent.R index 3d02819..ab771c6 100644 --- a/tests/testthat/test-rm_accent.R +++ b/tests/testthat/test-rm_accent.R @@ -1,36 +1,35 @@ context("Remove non-ASCII characters.") test_that("rm_accent is the converted version of a string with all non-ASCII characters removed.", { - - if(.Platform$OS.type == "unix"){ - #symbols - acute = "áéíóúÁÉÍÓÚýÝ" - grave = "àèìòùÀÈÌÒÙ" - circunflex = "âêîôûÂÊÎÔÛ" - tilde = "ãõÃÕñÑ" - umlaut = "äëïöüÄËÏÖÜÿ" - cedil = "çÇ" + if (.Platform$OS.type == "unix") { + # symbols + acute <- "áéíóúÁÉÍÓÚýÝ" + grave <- "àèìòùÀÈÌÒÙ" + circunflex <- "âêîôûÂÊÎÔÛ" + tilde <- "ãõÃÕñÑ" + umlaut <- "äëïöüÄËÏÖÜÿ" + cedil <- "çÇ" } else { - acute = iconv("\u00e1\u00e9\u00ed\u00f3\u00fa\u00c1\u00c9\u00cd\u00d3\u00da\u00fd\u00dd", from = "utf-8", "latin1") - grave = iconv("\u00e0\u00e8\u00ec\u00f2\u00f9\u00c0\u00c8\u00cc\u00d2\u00d9", from = "utf-8", "latin1") - circunflex = iconv("\u00e2\u00ea\u00ee\u00f4\u00fb\u00c2\u00ca\u00ce\u00d4\u00db", from = "utf-8", "latin1") - tilde = iconv("\u00e3\u00f5\u00c3\u00d5\u00f1\u00d1", from = "utf-8", "latin1") - umlaut = iconv("\u00e4\u00eb\u00ef\u00f6\u00fc\u00c4\u00cb\u00cf\u00d6\u00dc\u00ff", from = "utf-8", "latin1") - cedil = iconv("\u00e7\u00c7", from = "utf-8", "latin1") + acute <- iconv("\u00e1\u00e9\u00ed\u00f3\u00fa\u00c1\u00c9\u00cd\u00d3\u00da\u00fd\u00dd", from = "utf-8", "latin1") + grave <- iconv("\u00e0\u00e8\u00ec\u00f2\u00f9\u00c0\u00c8\u00cc\u00d2\u00d9", from = "utf-8", "latin1") + circunflex <- iconv("\u00e2\u00ea\u00ee\u00f4\u00fb\u00c2\u00ca\u00ce\u00d4\u00db", from = "utf-8", "latin1") + tilde <- iconv("\u00e3\u00f5\u00c3\u00d5\u00f1\u00d1", from = "utf-8", "latin1") + umlaut <- iconv("\u00e4\u00eb\u00ef\u00f6\u00fc\u00c4\u00cb\u00cf\u00d6\u00dc\u00ff", from = "utf-8", "latin1") + cedil <- iconv("\u00e7\u00c7", from = "utf-8", "latin1") } - - #nudeSymbols - nudeAcute = "aeiouAEIOUyY" - nudeGrave = "aeiouAEIOU" - nudeCircunflex = "aeiouAEIOU" - nudeTilde = "aoAOnN" - nudeUmlaut = "aeiouAEIOUy" - nudeCedil = "cC" - + + # nudeSymbols + nudeAcute <- "aeiouAEIOUyY" + nudeGrave <- "aeiouAEIOU" + nudeCircunflex <- "aeiouAEIOU" + nudeTilde <- "aoAOnN" + nudeUmlaut <- "aeiouAEIOUy" + nudeCedil <- "cC" + expect_equal(rm_accent(acute), nudeAcute) expect_equal(rm_accent(grave), nudeGrave) expect_equal(rm_accent(circunflex), nudeCircunflex) expect_equal(rm_accent(tilde), nudeTilde) expect_equal(rm_accent(umlaut), nudeUmlaut) expect_equal(rm_accent(cedil), nudeCedil) -}) \ No newline at end of file +}) diff --git a/tests/testthat/test-sample_cnj.R b/tests/testthat/test-sample_cnj.R index 36d757a..00a76d7 100644 --- a/tests/testthat/test-sample_cnj.R +++ b/tests/testthat/test-sample_cnj.R @@ -1,16 +1,21 @@ context("Sample CNJ process codes.") test_that("sample_cnj is a set of random process codes.", { + expect_equal(nrow(sample_cnj(3, + foros = "0000", + anos = "2015", orgao = 8, tr = 26, + first_dig = "0", sample_pars = T, return_df = T + )), 3) - expect_equal(nrow(sample_cnj(3, foros = "0000", - anos = "2015", orgao = 8, tr = 26, - first_dig = "0",sample_pars = T, return_df = T)), 3) - - expect_error(sample_cnj(3, foros = c("0001","0000"), - anos = "2015", orgao = 8, tr = 26, - first_dig = "0",sample_pars = F, return_df = T)) - - expect_error(sample_cnj(3, foros = c("0001","0000"), anos = c("2015","2014"), - orgao = c(8,7), tr = c(25,26), first_dig = "0", - sample_pars = F, return_df = T)) -}) \ No newline at end of file + expect_error(sample_cnj(3, + foros = c("0001", "0000"), + anos = "2015", orgao = 8, tr = 26, + first_dig = "0", sample_pars = F, return_df = T + )) + + expect_error(sample_cnj(3, + foros = c("0001", "0000"), anos = c("2015", "2014"), + orgao = c(8, 7), tr = c(25, 26), first_dig = "0", + sample_pars = F, return_df = T + )) +})