diff --git a/NEWS b/NEWS new file mode 100644 index 0000000..fc0d1df --- /dev/null +++ b/NEWS @@ -0,0 +1,14 @@ + *************************************************** + * * + * Changes and Developments in the CRAN view app * + * * + *************************************************** + +------------------------------------ +- Changes in CRANview (2017-08-17) - +------------------------------------ + + o Package 'plotly' is now used for the plot to make app more + interactive. + o Date range was added. + o Some information about selected packages is now provided. \ No newline at end of file diff --git a/README.md b/README.md index c3c6588..6d7ede0 100644 --- a/README.md +++ b/README.md @@ -3,4 +3,5 @@ View CRAN Package Downloads with Shiny A Shiny app to visualize downloads from RStudio's CRAN mirror. -View the app [here](https://dgrtwo.shinyapps.io/cranview/), or read more about it [here](http://varianceexplained.org/r/cran-view). +View the app [here](https://dgrtwo.shinyapps.io/cranview/), or read more about it +[here](http://varianceexplained.org/r/cran-view). diff --git a/server.R b/server.R index ed1286c..861ff19 100644 --- a/server.R +++ b/server.R @@ -6,46 +6,106 @@ library(lubridate) library(cranlogs) library(zoo) library(scales) +library(plotly) -get_initial_release_date = function(packages) -{ - min_date = Sys.Date() - 1 - - for (pkg in packages) - { - # api data for package. we want the initial release - the first element of the "timeline" - pkg_data = httr::GET(paste0("http://crandb.r-pkg.org/", pkg, "/all")) - pkg_data = httr::content(pkg_data) - - initial_release = pkg_data$timeline[[1]] - min_date = min(min_date, as.Date(initial_release)) - } - - min_date +get_initial_release_date <- function(packages) { + min_date <- Sys.Date() - 1 + + for (pkg in packages) + { + # api data for package. we want the initial release - the first element of the "timeline" + pkg_data <- httr::GET(paste0("http://crandb.r-pkg.org/", pkg, "/all")) + pkg_data <- httr::content(pkg_data) + + initial_release <- pkg_data$timeline[[1]] + min_date <- min(min_date, as.Date(initial_release)) + } + + min_date } -shinyServer(function(input, output) { +get_some_info <- function(packages) { + info <- list() + for (pkg in packages) + { + # api data for package. we want the initial release - the first element of the "timeline" + pkg_data <- httr::GET(paste0("http://crandb.r-pkg.org/", pkg, "/all")) + pkg_data <- httr::content(pkg_data) + + info[[pkg]]$title <- pkg_data$title + info[[pkg]]$version <- pkg_data$latest + info[[pkg]]$cran <- paste("https://CRAN.R-project.org/package=", pkg, sep = "") + } + + info +} + + +shinyServer(function(input, output, session) { + observe({ + release_date <- min(get_initial_release_date(input$package)) + updateDateRangeInput(session, "dateRange", + label = "Date range: yyyy-mm-dd", + start = release_date, + end = Sys.Date() - 1, + max = Sys.Date() + ) + }) + + downloads <- reactive({ - packages <- input$package - cran_downloads0 <- failwith(NULL, cran_downloads, quiet = TRUE) - cran_downloads0(package = packages, - from = get_initial_release_date(packages), - to = Sys.Date()-1) + packages <- input$package + cran_downloads0 <- purrr::possibly(cran_downloads, otherwise = NULL, quiet = TRUE) + cran_downloads0( + package = packages, + from = input$dateRange[1], + to = input$dateRange[2] + ) }) - - output$downloadsPlot <- renderPlot({ - d <- downloads() - if (input$transformation=="weekly") { - d$count=rollapply(d$count, 7, sum, fill=NA) - } else if (input$transformation=="cumulative") { - d = d %>% - group_by(package) %>% - transmute(count=cumsum(count), date=date) - } - - ggplot(d, aes(date, count, color = package)) + geom_line() + - xlab("Date") + - scale_y_continuous(name="Number of downloads", labels = comma) + + + output$downloadsPlot <- renderPlotly({ + d <- downloads() + packages <- input$package + packages <- packages[order(packages)] + if (input$transformation == "weekly") { + d$count <- rollapply(d$count, 7, sum, fill = NA) + } else if (input$transformation == "cumulative") { + d <- d %>% + group_by(package) %>% + transmute(count = cumsum(count), date = date) + } + g <- ggplot(d, aes(date, count, color = package)) + + geom_line() + + xlab("Date") + + scale_y_continuous(name = "Number of downloads", labels = comma) + g <- ggplotly(g) %>% config(displayModeBar = FALSE) + + for (i in 1:length(packages)) { + g$x$data[[i]]$text <- paste( + "date:", d$date[d$package == packages[i]], + "
count:", d$count[d$package == packages[i]], + "
package:", packages[i] + ) + } + g }) + output$packageinfo <- renderUI({ + packages <- input$package + info <- get_some_info(packages) + text <- "" + for (pkg in packages) + { + text <- paste( + text, + paste("", pkg, "", + "
Title: ", info[[pkg]]$title, + "
Latest version: ", info[[pkg]]$version, "
", + sep = "" + ) + ) + } + HTML(text) + }) }) diff --git a/ui.R b/ui.R index 23b527a..0f7a198 100644 --- a/ui.R +++ b/ui.R @@ -1,37 +1,54 @@ library(shiny) library(httr) library(jsonlite) +library(plotly) # get the list of all packages on CRAN -package_names = names(httr::content(httr::GET("http://crandb.r-pkg.org/-/desc"))) +package_names <- names(httr::content(httr::GET("http://crandb.r-pkg.org/-/desc"))) shinyUI(fluidPage( # Application title - titlePanel("Package Downloads Over Time"), + titlePanel("Package CRAN downloads over time"), # Sidebar with a slider input for number of bins sidebarLayout( sidebarPanel( - HTML("Enter an R package to see the # of downloads over time from the RStudio CRAN Mirror.", - "You can enter multiple packages to compare them"), - selectInput("package", - label = "Packages:", - selected = sample(package_names, 2), # initialize the graph with a random package - choices = package_names, - multiple = TRUE), - radioButtons("transformation", - "Data Transformation:", - c("Daily" = "daily", "Weekly" = "weekly", "Cumulative" = "cumulative")), - HTML("Created using the cranlogs package.", - "This app is not affiliated with RStudio or CRAN.", - "You can find the code for the app here,", - "or read more about it here.") + HTML( + "Enter an R package to see the # of downloads over time from the RStudio CRAN Mirror.", + "You can enter multiple packages to compare them" + ), + br(), + br(), + selectizeInput("package", + label = "Packages:", + selected = c("difNLR", "ShinyItemAnalysis"), # initialize the graph with a random package + choices = package_names, + multiple = TRUE + ), + radioButtons("transformation", + "Data Transformation:", + c("Daily" = "daily", "Weekly" = "weekly", "Cumulative" = "cumulative"), + selected = "cumulative" + ), + + dateRangeInput("dateRange", + label = "Date range: yyyy-mm-dd", + start = Sys.Date() - 2, end = Sys.Date() - 1, max = Sys.Date() + ), + + HTML( + "Created using the cranlogs package.", + "This app is not affiliated with RStudio or CRAN.", + "You can find the code for the app here,", + "or read more about it here." + ) ), # Show a plot of the generated distribution mainPanel( - plotOutput("downloadsPlot") + plotlyOutput("downloadsPlot"), + htmlOutput("packageinfo") ) ) ))