---
title: "Bioconductor Package Dashboard"
format:
  dashboard:
    orientation: columns
logo: https://bioconductor.org/images/logo_bioconductor.gif
theme: minty
server: shiny
---

```{r}
#| context: setup
#| include: false

library(BiocPkgDash)
```

```{r}
#| context: data
#| include: false

version <- BiocManager:::.version_bioc(type = "release")
data <- BiocPkgTools::biocMaintained(
    main = "maintainer@bioconductor.org",
    version = version,
    pkgType = "software"
)
pkgs <-  BiocPkgDash:::filterMaintained(
    data = data
)
```


# {.sidebar}

```{r}
radioButtons(
    inputId = "biocver",
    label = "Bioconductor version:",
    choices = c("release", "devel"),
    selected = "release"
)
checkboxGroupInput(
    inputId = "bioctype",
    label = "Bioconductor package type:",
    choices = c(
        "software",
        "data-experiment",
        "data-annotation",
        "workflows"
    ),
    selected = "software"
)
textInput(
    inputId = "email",
    label = "Enter maintainer e-mail",
    placeholder = "maintainer@bioconductor.org"
)
actionButton(
    inputId = "submit",
    label = "Submit",
    class = "btn-primary"
)
```

-------------------------------------------------------------------------------

Download the badge wall as an HTML fragment for your website:

```{r}
#| output: asis

## downloadButton currently not working
## https://github.com/quarto-dev/quarto-cli/issues/999
## using workaround
## https://github.com/rstudio/shiny/issues/2392#issuecomment-484097398
(downloadButton(
    outputId = "btnSend",
    label = "Download HTML"
))
```

# Badges

## Column {width="80%"}

```{r}
#| title: Wall of Badges
DT::dataTableOutput("badge_out")
```

## Column {width="20%"}


```{r}
bslib::value_box(
    title = "Total Packages",
    value = textOutput("pkg_count"),
    showcase = bsicons::bs_icon("hash"),
    theme = "primary"
)
```

```{r}
bslib::value_box(
    title = "Year-To-Date Downloads",
    value = textOutput("dl_count"),
    showcase = bsicons::bs_icon("cloud-download"),
    theme = "secondary"
)
```

```{r}
bslib::value_box(
    title = "Total No. of Dependencies",
    value = textOutput("dep_count"),
    showcase = bsicons::bs_icon("collection"),
    theme = "warning"
)
```

# Status

## Column {width="80%"}

```{r}
#| title: Package Status Plot
plotly::plotlyOutput("status_out")
```

# Data

## Column {height="80%"}

```{r}
#| title: Packages Maintained
DT::dataTableOutput("data_out")
```

# Status Table

## Column {height="80%"}

```{r}
#| title: Package Status Table
DT::dataTableOutput("status_table")
```


```{r}
#| context: server
emailValue <- reactiveVal("maintainer@bioconductor.org")

main_data <- reactive({
    if (
        emailValue() %in%
            c("maintainer@bioconductor\\.org", "maintainer@bioconductor.org")
    )
        pkgs
    else
        BiocPkgDash:::renderMaintained(
            email = emailValue(),
            version = input$biocver,
            pkgType = input$bioctype
        )
})

output$pkg_count <- renderText({
    nrow(main_data())
})

observeEvent(input$submit, {
    emailValue(input[["email"]])
})

output$badge_out <- DT::renderDataTable({
    DT::datatable(
        BiocPkgDash:::badgesDF(
            data = main_data()
        ),
        escape = FALSE,
        rownames = FALSE,
        options = list(
            dom = "ftp",
            pageLength = 20,
            lengthChange = FALSE,
            paging = TRUE
        )
    )
})

output$btnSend <- downloadHandler(
    filename = function() {
        em <- gsub("@", "_at_", emailValue())
        em <- gsub("\\.", "_dot_", em)
        paste0(em, ".html")
    },
    content = function(file) {
        BiocPkgDash:::renderHTMLfrag(
            file = file, data = main_data()
        )
    }
)

output$data_out <- DT::renderDataTable({
    DT::datatable(
        main_data()[, c(
            "Package", "Version", "License", "NeedsCompilation", "Title",
            "hasREADME", "hasNEWS", "hasINSTALL", "hasLICENSE",
            "dependencyCount"
        )],
        rownames = FALSE,
        options = list(
            dom = "ftp",
            pageLength = 20,
            paging = TRUE
        )
    )
})

output$status_out <- plotly::renderPlotly(
    BiocPkgDash::pkgStatusPlot(
        data = main_data()
    )
)

output$status_table <- DT::renderDataTable({
    DT::datatable(
        BiocPkgDash::pkgStatusTable(
            data = main_data()
        ),
        escape = FALSE,
        rownames = FALSE,
        options = list(
            dom = "ftp",
            pageLength = 20,
            lengthChange = FALSE,
            paging = TRUE
        )
    )
})

output$dl_count <- renderText({
    all_dls <- vapply(
        main_data()$Package,
        function(pkg) {
            dl_pkg <- suppressWarnings({
                BiocPkgTools::pkgDownloadStats(pkg)
            })
            dls <- sum(dl_pkg[["Nb_of_distinct_IPs"]])
            if (!length(dls))
                0
            else
                dls
        },
        numeric(1L)
    )
    prettyNum(
        sum(all_dls),
        big.mark = ",",
        scientific = FALSE
    )
})

output$dep_count <- renderText({
    total_deps <- vapply(
        main_data()$Package,
        function(pkg) {
            deps <- BiocPkgTools::pkgBiocDeps(
                pkg,
                which = "all"
            )
            length(unlist(deps))
        },
        numeric(1L)
    )
    prettyNum(
        sum(total_deps),
        big.mark = ",",
        scientific = FALSE
    )
})

```
