Skip to content

Commit

Permalink
Merge pull request #13 from AlissonRP/fixes_freq
Browse files Browse the repository at this point in the history
Version 0.5
  • Loading branch information
AlissonRP authored May 2, 2022
2 parents a253237 + b29965b commit fd5ab9a
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 50 deletions.
30 changes: 23 additions & 7 deletions R/freq-table.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
14 changes: 5 additions & 9 deletions R/freq-table2.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}) |>
Expand All @@ -50,7 +50,3 @@ pdf1_tbl_freq2 <- function(obj, var1, var2, title = '', marg = F) {
}
tab
}




3 changes: 0 additions & 3 deletions R/imports.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,3 @@ janitor::as_tabyl


utils::globalVariables(c("where", "median", "sd", "n", "setNames"))



8 changes: 4 additions & 4 deletions R/logo.R
Original file line number Diff line number Diff line change
@@ -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"))
20 changes: 10 additions & 10 deletions R/pdf1_summary.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
4 changes: 2 additions & 2 deletions R/pdf1_tbl.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) %>%
Expand All @@ -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)
Expand Down
5 changes: 1 addition & 4 deletions R/tab.R
Original file line number Diff line number Diff line change
@@ -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)

}


13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,33 @@ 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")
```
## 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`
15 changes: 13 additions & 2 deletions man/pdf1_tbl_freq.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions tests/testthat/test-frequency.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
})

9 changes: 5 additions & 4 deletions tests/testthat/test_summary.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
})

0 comments on commit fd5ab9a

Please sign in to comment.