Skip to content

Commit

Permalink
kittyR gains only_kitten argument; closes #3
Browse files Browse the repository at this point in the history
  • Loading branch information
IndrajeetPatil committed Mar 21, 2019
1 parent e58f8ad commit 43ca319
Show file tree
Hide file tree
Showing 33 changed files with 329 additions and 74 deletions.
7 changes: 4 additions & 3 deletions DESCRIPTION
Expand Up @@ -18,15 +18,16 @@ Depends:
R (>= 3.5.0)
Imports:
beepr (>= 1.3),
dplyr (>= 0.7.8),
dplyr (>= 0.8.0),
ellipsis,
glue,
graphics,
imager (>= 0.41.2),
magrittr,
purrr (>= 0.3.0),
purrr (>= 0.3.2),
rvest (>= 0.3.2),
stringr (>= 1.3.1),
tibble (>= 2.0.1)
tibble (>= 2.1.1)
Suggests:
spelling,
testthat
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Expand Up @@ -8,6 +8,7 @@ importFrom(beepr,beep)
importFrom(dplyr,"%>%")
importFrom(dplyr,filter)
importFrom(dplyr,mutate)
importFrom(ellipsis,check_dots_used)
importFrom(graphics,plot)
importFrom(imager,load.image)
importFrom(magrittr,"%>%")
Expand Down
48 changes: 38 additions & 10 deletions R/kittyR.R
Expand Up @@ -8,6 +8,8 @@
#' \enumerate{
#' \item \url{https://pixabay.com/en/photos/cat/}
#' \item \url{https://www.pexels.com/search/cat/}
#' \item \url{https://pixabay.com/en/photos/kitten/}
#' \item \url{https://www.pexels.com/search/kitten/}
#' }
#' The sound files come from the following CC BY 3.0 source:
#' \url{http://soundbible.com/suggest.php?q=cat+meow&x=0&y=0}
Expand All @@ -17,7 +19,10 @@
#' default collection will be shown.
#' @param meow Logical that decides whether to play a meow sound along with the
#' picture of a cat.
#' @param only_kitten Logical that decides whether only kitten pics should be
#' selected (default: `FALSE`).
#' @inheritParams meowR
#' @param ... Additional arguments (currently ignored).
#'
#' @importFrom rvest html_session html_nodes html_attr
#' @importFrom imager load.image
Expand All @@ -26,29 +31,53 @@
#' @importFrom dplyr %>% mutate filter
#' @importFrom stringr str_split str_remove_all
#' @importFrom graphics plot
#' @importFrom ellipsis check_dots_used
#'
#' @examples
#' # relying on default collection
#' set.seed(3444)
#' set.seed(122)
#' kittyR::kittyR(meow = FALSE)
#'
#' # proving a custom URL
#' # in case you want only pics of kittens
#' set.seed(111)
#' kittyR::kittyR(meow = FALSE, only_kitten = TRUE)
#'
#' # provide a custom URL
#' kittyR::kittyR(
#' url = "https://www.pexels.com/search/cat/",
#' url = "https://visualhunt.com/photos/cat/",
#' meow = FALSE
#' )
#' @export

# function body
kittyR <- function(url = NULL, meow = TRUE, sound = 3) {
kittyR <- function(url = NULL,
meow = TRUE,
sound = 3,
only_kitten = FALSE,
...) {
ellipsis::check_dots_used()

# if no url is provided
if (is.null(url)) {
# if only kitten pics are needed
if (isTRUE(only_kitten)) {
url_list <- list(url = tibble::lst(
"https://pixabay.com/en/photos/kitten/",
"https://www.pexels.com/search/kitten/"
))
} else {
url_list <- list(url = tibble::lst(
"https://pixabay.com/en/photos/cat/",
"https://www.pexels.com/search/cat/",
"https://pixabay.com/en/photos/kitten/",
"https://www.pexels.com/search/kitten/"
))
}

# getting the dataframe with image URLs
df_combined <-
purrr::pmap_dfr(
.l = list(url = tibble::lst(
"https://pixabay.com/en/photos/cat/",
"https://www.pexels.com/search/cat/"
)),
.l = url_list,
.f = kittyR::kitty_pics_df,
.id = "source"
)
Expand All @@ -67,7 +96,7 @@ kittyR <- function(url = NULL, meow = TRUE, sound = 3) {
)

# bring the kitties to R
kitty <- imager::load.image(temporary_file_location)
kitty <- imager::load.image(file = temporary_file_location)

# display the cat
graphics::plot(kitty, yaxt = "n", axes = FALSE)
Expand All @@ -76,5 +105,4 @@ kittyR <- function(url = NULL, meow = TRUE, sound = 3) {
if (isTRUE(meow)) {
kittyR::meowR(sound = sound)
}

}
5 changes: 4 additions & 1 deletion R/kittypics_df_maker.R
Expand Up @@ -15,11 +15,14 @@ kitty_pics_df <- function(url) {
rvest::html_session(url = url) %>%
rvest::html_nodes(x = ., css = "img")

# exclusion pattern
exclude_str_pattern <- "logo|static|users|avatar|assets|hazelnut"

# getting static images
df_static <- kitties %>%
rvest::html_attr(x = ., name = "src") %>%
tibble::enframe(x = ., name = "id", value = "url") %>%
dplyr::filter(.data = ., !stringr::str_detect(url, "static|users|avatar|assets"))
dplyr::filter(.data = ., !stringr::str_detect(url, exclude_str_pattern))

# getting images with two source files (but retaining only the 480 pixel ones)
df_srcset <-
Expand Down
15 changes: 14 additions & 1 deletion README.Rmd
Expand Up @@ -45,19 +45,32 @@ There are only two available functions in this package-

In case you want a picture of a kitty:

```{r kittyR}
```{r kittyR1}
# to always have the same kitty on README
set.seed(100)
# show me the kitty NOW!
kittyR::kittyR()
```

By default, the collection will include any kind of cat (big, small, wild,
etc.). If you want only kittens, you can use an additional argument.

```{r kittyR2}
# to always have the same kitty on README
set.seed(100)
# show me the kitten NOW!
kittyR::kittyR(meow = FALSE, only_kitten = TRUE)
```

The collection of cat pictures comes from the following collections of freely
available images:

- <https://pixabay.com/en/photos/cat/>
- <https://www.pexels.com/search/cat/>
- <https://pixabay.com/en/photos/kitten/>
- <https://www.pexels.com/search/kitten/>

In case you just want to hear a meow either *apropos* nothing or at the end of
execution of some code to let you know that the execution is complete, just
Expand Down
Binary file added Rplots.pdf
Binary file not shown.
27 changes: 21 additions & 6 deletions codemeta.json
Expand Up @@ -17,7 +17,7 @@
"version": "3.6.0",
"url": "https://r-project.org"
},
"runtimePlatform": "R Under development (unstable) (2019-02-26 r76164)",
"runtimePlatform": "R Under development (unstable) (2019-03-19 r76252)",
"provider": {
"@id": "https://cran.r-project.org",
"@type": "Organization",
Expand Down Expand Up @@ -92,7 +92,7 @@
"@type": "SoftwareApplication",
"identifier": "dplyr",
"name": "dplyr",
"version": ">= 0.7.8",
"version": ">= 0.8.0",
"provider": {
"@id": "https://cran.r-project.org",
"@type": "Organization",
Expand All @@ -101,6 +101,18 @@
},
"sameAs": "https://CRAN.R-project.org/package=dplyr"
},
{
"@type": "SoftwareApplication",
"identifier": "ellipsis",
"name": "ellipsis",
"provider": {
"@id": "https://cran.r-project.org",
"@type": "Organization",
"name": "Comprehensive R Archive Network (CRAN)",
"url": "https://cran.r-project.org"
},
"sameAs": "https://CRAN.R-project.org/package=ellipsis"
},
{
"@type": "SoftwareApplication",
"identifier": "glue",
Expand Down Expand Up @@ -147,7 +159,7 @@
"@type": "SoftwareApplication",
"identifier": "purrr",
"name": "purrr",
"version": ">= 0.3.0",
"version": ">= 0.3.2",
"provider": {
"@id": "https://cran.r-project.org",
"@type": "Organization",
Expand Down Expand Up @@ -186,7 +198,7 @@
"@type": "SoftwareApplication",
"identifier": "tibble",
"name": "tibble",
"version": ">= 2.0.1",
"version": ">= 2.1.1",
"provider": {
"@id": "https://cran.r-project.org",
"@type": "Organization",
Expand All @@ -196,9 +208,12 @@
"sameAs": "https://CRAN.R-project.org/package=tibble"
}
],
"fileSize": "3116.092KB",
"fileSize": "3004.372KB",
"contIntegration": [],
"developmentStatus": "https://www.tidyverse.org/lifecycle/#experimental",
"readme": "https://github.com/IndrajeetPatil/kittyR/blob/master/README.md",
"releaseNotes": "https://github.com/IndrajeetPatil/kittyR/blob/master/NEWS.md"
"releaseNotes": "https://github.com/IndrajeetPatil/kittyR/blob/master/NEWS.md",
"contributor": {},
"copyrightHolder": {},
"funder": {}
}
138 changes: 138 additions & 0 deletions docs/404.html

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

0 comments on commit 43ca319

Please sign in to comment.