Package description

Row

KPI - 1

44786

KPI - 2

1444.7

KPI - 3

59

Row

KPI - 4

36

KPI - 5

0

KPI - 6

7

Row

Summary

CRAN Download Stats

Column

Graph

Column

Table

Function Summary

Column

Table

Package dependencies

Column

Package dependencies

Pull Requests

Column

About

The table shows the pull requests that have been created, closed, or merged during the reporting period.

Column

Active pull requests

Issues

Column

About

The table shows the issues that have been created or closed during the reporting period.

Column

Active issues

---
date: "`r Sys.Date()`"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
params:
  pkgname: pkgname
  set_title: report_title
  from: from
  to: to
  gh: gh
title: "`r params$set_title`"      
---

```{r setup, include=FALSE}
library(flexdashboard)
```

```{r}
## Get user data
pkgname <- params$pkgname
from_date <- params$from
to_date <- params$to
gh <- params$gh

library(cranlogs)
library(dplyr)
library(ggplot2)
library(purrr)
library(ggiraph)
do.call(library, args = list("package" = pkgname))

date_diff <- as.Date(to_date, "%Y-%m-%d") - as.Date(from_date, "%Y-%m-%d")

cran_df <-
  cranlogs::cran_downloads(
  packages = pkgname,
  from = from_date,
  to = to_date
  )

cran_summary <-
  cran_df %>%
  summarise(
    min_count = min(count, na.rm = TRUE),
    max_count = max(count, na.rm = TRUE),
    mean_count = mean(count, na.rm = TRUE),
    max_date = max(date, na.rm = TRUE)
  )

# custom environment for package
env_pkg <- loadNamespace(pkgname)

# get full list of function names
fnc_str <-
  Filter(
  f = function(x, p = env_pkg){
    (is.function(get(x, p))
     # Exclude Reference Class object generators for now
     & !methods::is(get(x, p), "refObjectGenerator")
    )
  }
  , x = names(env_pkg)
)

# package dependencies
pkg_deps <- recursive_dependencies(package = pkgname, db = NULL)

# github owner/repository
gh_owner <- gsub(pattern = "/.+", replacement = "", x = gh)
gh_repos <- gsub(pattern = ".+/", replacement = "", x = gh)

# Open pull requests

if(is.null(gh)){
  
  gh_opr <- "N/A"
  
} else {
  
  gh_pr_df <- get_gh_pr(
    owner = gh_owner,
    repo = gh_repos
    )
  
  gh_opr <- gh_pr_df %>%
    filter(state == "open") %>%
    nrow()
}


# Open issues

if(is.null(gh)){
  
  gh_ois <- "N/A"
  
} else {
  
  gh_iss_df <- get_gh_issues(
    owner = gh_owner,
    repo = gh_repos
    ) 
  
  gh_ois <- gh_iss_df %>%
    filter(state == "open") %>%
    nrow()
}
```

Package description {data-orientation=rows}
=======================================================================

Row
-----------------------------------------------------------------------

### KPI - 1

```{r}
cran_df %>%
  summarise(count = sum(count, na.rm = TRUE)) %>%
  pull(count) %>%
  flexdashboard::valueBox(
    caption = "Monthly CRAN downloads",
    color = "primary"
  )
```

### KPI - 2

```{r}
cran_df %>%
  summarise(count = mean(count, na.rm = TRUE)) %>%
  pull(count) %>%
  round(digits = 1) %>%
  flexdashboard::valueBox(
    caption = "Daily CRAN downloads",
    color = "success"
  )
```

### KPI - 3

```{r}
fnc_str %>%
  length() %>%
  flexdashboard::valueBox(
    caption = "Total number of functions",
    color = "info"
  )
```

Row
-----------------------------------------------------------------------

### KPI - 4

```{r}
pkg_deps %>%
  length() %>%
  flexdashboard::valueBox(
    caption = "Package dependencies",
    color = "warning"
  )
```

### KPI - 5
```{r}
gh_opr %>%
  flexdashboard::valueBox(
    caption = "Open pull requests",
    color = "danger"
  )
```  

### KPI - 6

```{r}
gh_ois %>%
  flexdashboard::valueBox(
    caption = "Open issues",
    color = "#224e87"
  )
```  

Row
-----------------------------------------------------------------------

### Summary

```{r}
# Package description
desc <- utils::packageDescription(pkg = pkgname)

dplyr::tibble(
  Field = names(desc),
  Values = unlist(desc)
) %>%
  DT::datatable(
    extensions = c('Buttons',
                   'FixedColumns'),
             options = list(dom = 'lfrtip',
                            fixedColumns = list(leftColumns = 2),
                            scrollX = TRUE,
                            lengthMenu = list(c(-1, 10,25,50),
                                              c("All", 10,25,50))))
```

CRAN Download Stats
=======================================================================

Column {data-width=650}
-----------------------------------------------------------------------

### Graph

```{r}
cran_g <-
  cran_df %>%
  ggplot(aes(x = date, y = count)) +
  geom_line_interactive(size = 1, colour = "#224e87") +
  geom_point_interactive(aes(tooltip = count), colour = "#224e87") +
  
  # average line --------------------------------------------------
  geom_hline_interactive(yintercept = mean(cran_df$count),
             colour = "red",
             linetype = 2) + # Average line
  geom_text(
    data = cran_summary,
    aes(y = mean_count,
        x = max_date,
        label = paste("Daily average:", round(mean_count, 1))),
    vjust = 1.2,
    hjust = 0.8,
    size = 3,
    colour = "red") +
  # max line -------------------------------------------------------
  geom_hline_interactive(yintercept = cran_summary$max_count,
             colour = "grey20",
             linetype = 2) + # Average line
  geom_text(
    data = cran_summary,
    aes(y = max_count,
        x = max_date,
        label = paste("Daily max:", max_count)),
    vjust = 1.2,
    hjust = 0.8,
    size = 3,
    colour = "grey20") +
  labs(
    title = paste0(pkgname, ": CRAN Package Downloads over the past ",
                   as.numeric(date_diff), " days"),
    subtitle = paste("From", min(cran_df$date),
                     "to", max(cran_df$date)),
    y = "Downloads",
    x = "Date"
  )

ggiraph::girafe(ggobj = cran_g)
```

Column {data-width=350}
-----------------------------------------------------------------------

### Table

```{r}
DT::datatable(cran_df,
             extensions = c('Buttons',
                            'FixedColumns'),
             options = list(dom = 'Blfrtip',
                            fixedColumns = list(leftColumns = 2),
                            scrollX = TRUE,
                            buttons = c('copy', 'csv', 'excel', 'pdf', 'print'),
                            lengthMenu = list(c(-1, 10,25,50),
                                              c("All", 10,25,50))))
```

Function Summary
=======================================================================

Column {data-width=350}
-----------------------------------------------------------------------

### Table

```{r}
fs_df <- generate_summary(package_name = pkgname)

DT::datatable(fs_df,
             extensions = c('Buttons',
                            'FixedColumns'),
             options = list(dom = 'Blfrtip',
                            fixedColumns = list(leftColumns = 2),
                            scrollX = TRUE,
                            buttons = c('copy', 'csv', 'excel', 'pdf', 'print'),
                            lengthMenu = list(c(-1, 10,25,50),
                                              c("All", 10,25,50))))
```

Package dependencies
=======================================================================

Column
-----------------------------------------------------------------------

```{r}
dependencyList <- tools::package_dependencies(
  pkg_deps,
  reverse = FALSE,
  recursive = FALSE,
  db = NULL,
  which = c("Depends", "Imports")
)

dependencyList <- Filter(function(x){!is.null(x)}, dependencyList)

# If pkg A depends on pkg B, then A -> B
# A is the SOURCE and B is the TARGET
# This is UML dependency convention
edges <- names(dependencyList) %>%
  purrr::map(function(x){
    dplyr::tibble(
      SOURCE = rep(x, length(dependencyList[[x]])),
      TARGET = dependencyList[[x]]
    )}) %>%
  bind_rows()

# Get and save nodes
nodes <- data.table::data.table(
  node = unique(
    c(
      edges$SOURCE,
      edges$TARGET
    )
  )
)
```

### Package dependencies

```{r}
nodes_df <-
  nodes %>%
  mutate(id = node) %>%
  rename(label = "node")

edges_df <-
  edges %>%
  rename(from = "SOURCE",
         to = "TARGET")

visNetwork::visNetwork(nodes_df, edges_df, width = "100%") %>%
  visNetwork::visEdges(arrows = "from")
```

Pull Requests
=======================================================================

Column {data-width=200}
-----------------------------------------------------------------------

### About

The table shows the pull requests that have been created, closed, or merged
during the reporting period.


Column {data-width=800}
-----------------------------------------------------------------------

### Active pull requests

```{r}
if(is.null(gh)){
  
  gh_out <- md2html("Data unavailable as no GitHub repository is provided/available.")
  
} else {
  
  gh_out <- gh_pr_df %>%
      select(
        number,
        title,
        state,
        user,
        created_at,
        closed_at,
        merged_at
        ) %>%
    filter(between(
      created_at,
      as.Date(from_date, "%Y-%m-%d"),
      as.Date(to_date, "%Y-%m-%d")
    ) |
      between(
        closed_at,
        as.Date(from_date, "%Y-%m-%d"),
        as.Date(to_date, "%Y-%m-%d")
      ) |
      between(
        merged_at,
        as.Date(from_date, "%Y-%m-%d"),
        as.Date(to_date, "%Y-%m-%d")
      )
      )
  
  gh_out <-
    DT::datatable(gh_out,
             extensions = c('Buttons',
                            'FixedColumns'),
             options = list(dom = 'Blfrtip',
                            fixedColumns = list(leftColumns = 2),
                            scrollX = TRUE,
                            buttons = c('copy', 'csv', 'excel', 'pdf', 'print'),
                            lengthMenu = list(c(-1, 10,25,50),
                                              c("All", 10,25,50))))
    
  
}

gh_out

```

Issues
=======================================================================

Column {data-width=200}
-----------------------------------------------------------------------

### About

The table shows the issues that have been created or closed during the reporting
period.


Column {data-width=800}
-----------------------------------------------------------------------

### Active issues

```{r}
if(is.null(gh)){
  
  gh_out <- md2html("Data unavailable as no GitHub repository is provided/available.")
  
} else {
  
  gh_out <- gh_iss_df %>%
      select(
        number,
        title,
        state,
        assignee = "user.login",
        created_at,
        closed_at
        ) %>%
    filter(between(
      created_at,
      as.Date(from_date, "%Y-%m-%d"),
      as.Date(to_date, "%Y-%m-%d")
    ) |
      between(
        closed_at,
        as.Date(from_date, "%Y-%m-%d"),
        as.Date(to_date, "%Y-%m-%d")
      ))
  
  gh_out <-
    DT::datatable(gh_out,
             extensions = c('Buttons',
                            'FixedColumns'),
             options = list(dom = 'Blfrtip',
                            fixedColumns = list(leftColumns = 2),
                            scrollX = TRUE,
                            buttons = c('copy', 'csv', 'excel', 'pdf', 'print'),
                            lengthMenu = list(c(-1, 10,25,50),
                                              c("All", 10,25,50))))
    
  
}

gh_out

```