diff --git a/R/freq-table.R b/R/freq-table.R index 14531aa..67292e3 100644 --- a/R/freq-table.R +++ b/R/freq-table.R @@ -6,16 +6,32 @@ #' @param obj Object used to create the table. Data frame, list or environment #' (or object coercible by as.data.frame to a data frame) #' @param var Variable that you want the table (not written in string format) +#' @param sort_by Variable you want to sort the `tibble` +#' @param desc if you want decreasing order put `FALSE` +#' @note By default the `sort_by` is in alphabetical order of `{{ var }}` #' @examples #' iris %>% #' mypdf1::pdf1_tbl_freq(Species) %>% -#' mypdf1::pdf1_tbl(" You can combine this function too!") +#' mypdf1::pdf1_tbl("You can combine this function too!") #' @export -pdf1_tbl_freq <- function(obj, var) { - obj %>% +pdf1_tbl_freq <- function(obj, + var, + sort_by = {{ var }}, + desc = F) +{ + non_order <- obj %>% dplyr::count({{ var }}) %>% - dplyr::mutate(`relative_frequency` = prop.table(n) %>% round(3)) %>% - dplyr::rename(`absolute_frequency` = n) %>% - janitor::adorn_totals("row") |> - dplyr::tibble() + dplyr::mutate(`relative_frequency` = prop.table(n) %>% round(4)) %>% + dplyr::rename(`absolute_frequency` = n) + if (desc == T) { + non_order |> + dplyr::arrange(dplyr::desc({{ sort_by }})) |> + janitor::adorn_totals("row") |> + dplyr::tibble() + } else { + non_order |> + dplyr::arrange({{ sort_by }}) |> + janitor::adorn_totals("row") |> + dplyr::tibble() + } } diff --git a/R/freq-table2.R b/R/freq-table2.R index 0fb48ba..8b45f2e 100644 --- a/R/freq-table2.R +++ b/R/freq-table2.R @@ -17,13 +17,13 @@ #' mtcars |> #' mypdf1::pdf1_tbl_freq2(cyl, am, "title", marg = TRUE) #' @export -pdf1_tbl_freq2 <- function(obj, var1, var2, title = '', marg = F) { - if(any(is.na(obj |> dplyr::select({{var1}},{{var2}}))) == TRUE){ - warning('Your dataframe has NA, they will be removed from calculations') +pdf1_tbl_freq2 <- function(obj, var1, var2, title = "", marg = F) { + if (any(is.na(obj |> dplyr::select({{ var1 }}, {{ var2 }}))) == TRUE) { + warning("Your dataframe has NA, they will be removed from calculations") } tab <- obj |> - tab({{ var1}}, {{var2}}) - tab[is.na(tab)] = 0 + tab({{ var1 }}, {{ var2 }}) + tab[is.na(tab)] <- 0 if (marg != TRUE) { title2 <- obj |> dplyr::select({{ var2 }}) |> @@ -50,7 +50,3 @@ pdf1_tbl_freq2 <- function(obj, var1, var2, title = '', marg = F) { } tab } - - - - diff --git a/R/imports.R b/R/imports.R index 899b299..859f123 100644 --- a/R/imports.R +++ b/R/imports.R @@ -11,6 +11,3 @@ janitor::as_tabyl utils::globalVariables(c("where", "median", "sd", "n", "setNames")) - - - diff --git a/R/logo.R b/R/logo.R index 4b2d193..4c68139 100644 --- a/R/logo.R +++ b/R/logo.R @@ -1,4 +1,4 @@ -#library(hexSticker) -#imgurl <- system.file("figures/teste.png", package="mypdf1") -#(sticker(imgurl, package="mypdf1", p_size=20, s_x=1, s_y=.75, s_width=.55, - # h_fill="#E7EDF0", h_color="#135389",p_color = "#135389")) +# library(hexSticker) +# imgurl <- system.file("figures/teste.png", package="mypdf1") +# (sticker(imgurl, package="mypdf1", p_size=20, s_x=1, s_y=.75, s_width=.55, +# h_fill="#E7EDF0", h_color="#135389",p_color = "#135389")) diff --git a/R/pdf1_summary.R b/R/pdf1_summary.R index 6a6408a..a6d8378 100644 --- a/R/pdf1_summary.R +++ b/R/pdf1_summary.R @@ -19,18 +19,18 @@ #' iris |> mypdf1::pdf1_summary() #' @export pdf1_summary <- function(obj, na_rm = TRUE) { - - char <- obj |> - dplyr::select(where((is.character))) |> # dumb i know - ncol() - fac <- obj |> - dplyr::select(where((is.factor))) |> # dumb i know - ncol() - if ((char + fac) != 0) { - warning("string and factors variables were removed for calculations")} + char <- obj |> + dplyr::select(where((is.character))) |> # dumb i know + ncol() + fac <- obj |> + dplyr::select(where((is.factor))) |> # dumb i know + ncol() + if ((char + fac) != 0) { + warning("string and factors variables were removed for calculations") + } obj <- obj |> dplyr::select(where(is.numeric)) - if( na_rm== TRUE & any(is.na(obj))){ + if (na_rm == TRUE & any(is.na(obj))) { warning("Your dataframe has NA, they will be removed from calculations \n use na_rm = FALSE if you want to keep them") } diff --git a/R/pdf1_tbl.R b/R/pdf1_tbl.R index 548703b..da83d74 100644 --- a/R/pdf1_tbl.R +++ b/R/pdf1_tbl.R @@ -15,7 +15,7 @@ #' #' The default of `code` is `FALSE` #' -#'The value of `format` will be automatically determined if the function is called within a knitr document +#' The value of `format` will be automatically determined if the function is called within a knitr document #' @examples #' iris %>% #' dplyr::group_by(Species) %>% @@ -27,7 +27,7 @@ #' dplyr::summarise(sd = sd(wt)) |> #' mypdf1::pdf1_tbl() #' @export -pdf1_tbl <- function(obj, title = "", format = NULL, code = F,...) { +pdf1_tbl <- function(obj, title = "", format = NULL, code = F, ...) { if (code == T) { obj %>% kableExtra::kable(caption = title, align = "c", format = format) diff --git a/R/tab.R b/R/tab.R index a9e9f75..1a0fee5 100644 --- a/R/tab.R +++ b/R/tab.R @@ -1,9 +1,6 @@ -tab = function(obj, var1, var2) { +tab <- function(obj, var1, var2) { obj %>% dplyr::group_by({{ var1 }}, {{ var2 }}) %>% dplyr::summarise(n = dplyr::n()) %>% tidyr::spread({{ var2 }}, n) - } - - diff --git a/README.md b/README.md index 2b1b549..e590a6f 100644 --- a/README.md +++ b/README.md @@ -11,13 +11,20 @@ test coverage](https://codecov.io/gh/AlissonRP/mypdf1/branch/main/graph/badge.sv ## Overview -Whenever I created a file in Rmarkdown (mainly for [Sigma JR](https://www.instagram.com/_sigmajr/)), there was a pattern of necessary packages and functions that I almost always used +Whenever I created a file in Rmarkdown, there was a pattern of necessary packages and functions that I almost always used. So this package provides a template for pdf reports in Rmarkdown and quite usual functions, note that it is a package more *aggregator* than creator. ## Instalation ```r devtools::install_github("https://github.com/AlissonRP/mypdf1") ``` +### Recommendation +To generate a pdf in Rmarkdown is recommended to use [`tinytex`](https://cran.r-project.org/web/packages/tinytex/index.html). If you don't have, use: + +```r +tinytex::install_tinytex() +``` + ## Use of default template ```r rmarkdown::draft("namefile.Rmd", template = "pdf1", package = "mypdf1") @@ -25,12 +32,12 @@ rmarkdown::draft("namefile.Rmd", template = "pdf1", package = "mypdf1") ## Use of paper template ```r -rmarkdown::draft("namefile.Rmd", template = "pdf1 - paper", package = "mypdf1", create_dir = FALSE) +rmarkdown::draft("namefile.Rmd", template = "pdf1-paper", package = "mypdf1", create_dir = FALSE) ``` ## Some functions already available * `pdf1_tbl`: This function is used to generate tables * `pdf1_freq_tbl`: This function assists in creating frequency tables (`tibble`) -* `pdf1_freq_tbl2`: 2x2 table, values can be absolute or marginal rows using argument `marg=TRUE` +* `pdf1_freq_tbl2`: 2x2 table, values can be absolute or marginal rows using argument `marg = TRUE` * `pdf1_na`: Inform the amount of `NA` per variable * `pdf1_summary`: Summarize the `data.frame` outputting a `tibble` diff --git a/man/pdf1_tbl_freq.Rd b/man/pdf1_tbl_freq.Rd index 02e289c..f039eb4 100644 --- a/man/pdf1_tbl_freq.Rd +++ b/man/pdf1_tbl_freq.Rd @@ -4,19 +4,30 @@ \alias{pdf1_tbl_freq} \title{pdf1_freq_tbl} \usage{ -pdf1_tbl_freq(obj, var) +pdf1_tbl_freq(obj, var, sort_by = { + { + var + } +}, desc = F) } \arguments{ \item{obj}{Object used to create the table. Data frame, list or environment (or object coercible by as.data.frame to a data frame)} \item{var}{Variable that you want the table (not written in string format)} + +\item{sort_by}{Variable you want to sort the \code{tibble}} + +\item{desc}{if you want decreasing order put \code{FALSE}} } \description{ this is a very simple frequency table generator } +\note{ +By default the \code{sort_by} is in alphabetical order of \code{{{ var }}} +} \examples{ iris \%>\% mypdf1::pdf1_tbl_freq(Species) \%>\% - mypdf1::pdf1_tbl(" You can combine this function too!") + mypdf1::pdf1_tbl("You can combine this function too!") } diff --git a/tests/testthat/test-frequency.R b/tests/testthat/test-frequency.R index ba8812a..82e27b5 100644 --- a/tests/testthat/test-frequency.R +++ b/tests/testthat/test-frequency.R @@ -5,9 +5,16 @@ test_that("sum total", { mypdf1::pdf1_tbl_freq(Species) |> (\(x) x[4, 2])() |> as.numeric(), 150) expect_equal(iris %>% mypdf1::pdf1_tbl_freq(Species) |> (\(x) x[4, 3])() |> round(2) |> as.numeric(), 1) + expect_equal(mtcars %>% + mypdf1::pdf1_tbl_freq(carb, sort_by = absolute_frequency) |> (\(x) x[1, 2])() |> round(2) |> as.numeric(), 1) + expect_equal(mtcars %>% + mypdf1::pdf1_tbl_freq(carb, sort_by = relative_frequency) |> (\(x) x[6, 3])() |> round(3) |> as.numeric(), 0.312) + expect_equal(mtcars %>% + mypdf1::pdf1_tbl_freq(carb, sort_by = absolute_frequency, desc = T) |> (\(x) x[1, 2])() |> round(3) |> as.numeric(), 10) + expect_equal(mtcars %>% + mypdf1::pdf1_tbl_freq(carb, sort_by = relative_frequency, desc = T) |> (\(x) x[1, 3])() |> round(3) |> as.numeric(), 0.312) expect_warning( airquality |> - mypdf1::pdf1_tbl_freq2(Ozone, Month),'Your dataframe has NA, they will be removed from calculations' + mypdf1::pdf1_tbl_freq2(Ozone, Month), "Your dataframe has NA, they will be removed from calculations" ) }) - diff --git a/tests/testthat/test_summary.R b/tests/testthat/test_summary.R index 7f0e5da..5b4c410 100644 --- a/tests/testthat/test_summary.R +++ b/tests/testthat/test_summary.R @@ -13,9 +13,10 @@ test_that("sum mean", { iris |> mypdf1::pdf1_summary(), "string and factors variables were removed for calculations" ) expect_equal( - airquality |> mypdf1::pdf1_summary(na_rm=F) |> (\(x) x[1, 2])() |> as.numeric() |> round(1), - as.numeric(NA)) + airquality |> mypdf1::pdf1_summary(na_rm = F) |> (\(x) x[1, 2])() |> as.numeric() |> round(1), + as.numeric(NA) + ) expect_warning( - airquality |> mypdf1::pdf1_summary() ,"Your dataframe has NA, they will be removed from calculations \n use na_rm = FALSE if you want to keep them") - + airquality |> mypdf1::pdf1_summary(), "Your dataframe has NA, they will be removed from calculations \n use na_rm = FALSE if you want to keep them" + ) })