OliverKeyes has submitted this change and it was merged. Change subject: Add features from Search Metrics dash for use in other dashes ......................................................................
Add features from Search Metrics dash for use in other dashes Bug: T118214 Change-Id: I89006df4eff59fc4b2d881dbbc69e15d5fd80729 --- M DESCRIPTION M NAMESPACE M NEWS.md M R/dygraphs.R M R/manipulate.R A R/shiny.R A man/cbind_fill.Rd M man/compress.Rd M man/cond_color.Rd M man/cond_icon.Rd A man/custom_axis_formatter.Rd M man/half.Rd M man/mad.Rd M man/make_dygraph.Rd M man/percent_change.Rd M man/read_dataset.Rd M man/safe_tail.Rd A man/smooth_select.Rd M man/smooth_switch.Rd M man/smoother.Rd M man/subset_by_date_range.Rd A man/time_frame_range.Rd A man/timeframe_daterange.Rd A man/timeframe_select.Rd 24 files changed, 340 insertions(+), 52 deletions(-) Approvals: OliverKeyes: Verified; Looks good to me, approved diff --git a/DESCRIPTION b/DESCRIPTION index 5292f5a..8ce8df1 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,21 +1,28 @@ Package: polloi Type: Package Title: Common Functionality for Wikimedia Dashboards -Version: 0.0.5 -Date: 2015-11-04 -Author: Oliver Keyes [aut, cre], Mikhail Popov [aut] -Maintainer: Oliver Keyes <oke...@wikimedia.org> -Description: This package (which I can say because BDR is nowhere in sight) contains common functionality - for all of the Wikimedia Foundation's shiny dashboards. +Version: 0.0.6 +Date: 2015-11-10 +Authors@R: c( + person("Oliver", "Keyes", , "oke...@wikimedia.org", role = c("aut", "cre")), + person("Mikhail", "Popov", , "mpo...@wikimedia.org", role = "aut") + ) +Description: This package contains common functionality for all of the + Wikimedia Foundation's Shiny Dashboards. License: MIT + file LICENSE +URL: https://git.wikimedia.org/summary/wikimedia%2Fdiscovery%2Fpolloi +BugReports: https://phabricator.wikimedia.org/maniphest/task/create/?projects=Search-Team +Depends: + R (>= 3.0.2) Imports: - magrittr, - dygraphs, - RColorBrewer, - xts, - readr, - lubridate, - plyr, - shiny, - zoo -LazyData: TRUE \ No newline at end of file + magrittr, + dygraphs, + RColorBrewer, + xts, + readr, + lubridate, + plyr, + shiny, + zoo +LazyData: TRUE +RoxygenNote: 5.0.0 diff --git a/NAMESPACE b/NAMESPACE index 9fe990b..c0b6681 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,17 +1,23 @@ -# Generated by roxygen2 (4.1.1): do not edit by hand +# Generated by roxygen2: do not edit by hand +export(cbind_fill) export(compress) export(cond_color) export(cond_icon) +export(custom_axis_formatter) export(half) export(mad) export(make_dygraph) export(percent_change) export(read_dataset) export(safe_tail) +export(smooth_select) export(smooth_switch) export(smoother) export(subset_by_date_range) +export(time_frame_range) +export(timeframe_daterange) +export(timeframe_select) importFrom(RColorBrewer,brewer.pal) importFrom(dygraphs,dyCSS) importFrom(dygraphs,dyLegend) diff --git a/NEWS.md b/NEWS.md index da1a980..353c01b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,21 +1,26 @@ +polloi 0.0.6 +============ +- Added features from Search Metrics dashboard + to be used across all dashboards. + polloi 0.0.5 ============= -- Added subsetting by date range +- Added subsetting by date range. polloi 0.0.4 ============= -- Added a change log -- Added a contributor code of conduct -- Added a readme +- Added a change log. +- Added a contributor code of conduct. +- Added a readme. polloi 0.0.3 ============= -- Updated reading to support optional arguments +- Updated reading to support optional arguments. polloi 0.0.2 ============= -- Made make_dygraph support reactivity +- Made make_dygraph support reactivity. polloi 0.0.1 ============= -- Initial release +- Initial release. diff --git a/R/dygraphs.R b/R/dygraphs.R index 8db9f64..aa5ec97 100644 --- a/R/dygraphs.R +++ b/R/dygraphs.R @@ -1,4 +1,4 @@ -#'@title Construct a Standard Wikimedia Dygraph +#'@title Construct a Standard Wikimedia Discovery Dygraph #'@description Construct a dygraph using the custom formatting Wikimedia dashboards use #'as standard. This is nothing special - a standard dygraph with a bit of custom CSS #'shipped with the package - but it's surrounded by code that allows the function to @@ -17,8 +17,13 @@ #' #'@param use_si whether to use si labelling (1000 becomes 1K). TRUE by default. #' -#'@param expr an optional expression to evaluate prior to building the dygraph. We use this in -#'(for example) reactive graphing. +#'@param expr an optional expression to evaluate prior to building the dygraph. +#' We use this in (for example) reactive graphing. +#' +#'@param group Group to associate this plot with. The x-axis zoom level of +#' plots within a group is automatically synchronized. +#' +#'@param ... Additional parameters to pass on to \code{dyOptions}. #' #'@importFrom dygraphs renderDygraph dyCSS dyOptions dyLegend dygraph #' @@ -29,7 +34,9 @@ #'@importFrom magrittr "%>%" #' #'@export -make_dygraph <- function(data, xlab, ylab, title, legend_name = NULL, use_si = TRUE, expr = NULL) { +make_dygraph <- function(data, xlab, ylab, title, + legend_name = NULL, use_si = TRUE, expr = NULL, + group = NULL, ...) { #Evaluate the expression expr @@ -50,12 +57,12 @@ } # Construct and return the dygraph - return(dygraph(data, main = title, xlab = xlab, ylab = ylab) %>% + return(dygraph(data, main = title, xlab = xlab, ylab = ylab, group = group) %>% dyLegend(width = 400, show = "always") %>% dyOptions(strokeWidth = 3, colors = brewer.pal(max(3, ncol(data)), "Set2"), drawPoints = FALSE, pointSize = 3, labelsKMB = use_si, - includeZero = TRUE) %>% + includeZero = TRUE, ...) %>% dyCSS(css = system.file("custom.css", package = "polloi"))) } @@ -95,4 +102,26 @@ } return(shiny::icon(ifelse(condition, "arrow-down", "arrow-up"))) -} \ No newline at end of file +} + +#'@title Custom Axis Formatter +#'@description This is a JS date formatter to be used in \code{dyAxis} when +#' it's desired to have the dates on the axis look like "Monday (8/13)", in +#' other words: "Day of the week (MM/DD)" +#'@export +#'@examples +#'\dontrun{ +#'polloi::make_dygraph(data) %>% +#' dyAxis("x", axisLabelFormatter = polloi::custom_axis_formatter) +#'} +custom_axis_formatter <- 'function (d, gran) { +var weekday = new Array(7); +weekday[0]= "Sunday"; +weekday[1] = "Monday"; +weekday[2] = "Tuesday"; +weekday[3] = "Wednesday"; +weekday[4] = "Thursday"; +weekday[5] = "Friday"; +weekday[6] = "Saturday"; +return weekday[d.getDay()] + " (" + (d.getMonth()+1) + "/" + d.getDate() + ")"; +}' \ No newline at end of file diff --git a/R/manipulate.R b/R/manipulate.R index e855b2a..186f30a 100644 --- a/R/manipulate.R +++ b/R/manipulate.R @@ -80,4 +80,17 @@ to <- as.Date(to) } return(x[x[[date_col]] >= from & x[[date_col]] <= to, ]) -} \ No newline at end of file +} + +#'@title Safely Combine R Objects of Variying Lengths by Columns +#'@description Take a sequence of vector, matrix or data-frame arguments and +#' combine by columns or rows, respectively. +#'@param ... Vectors or matrices. +#'@return A matrix with NAs wherever needed. +#'@references \url{http://r.789695.n4.nabble.com/How-to-join-matrices-of-different-row-length-from-a-list-td3177212.html} +#'@export +cbind_fill <- function(...) { + nm <- lapply(list(...), as.matrix) + n <- max(sapply(nm, nrow)) + do.call(cbind, lapply(nm, function (x) rbind(x, matrix(, n-nrow(x), ncol(x))))) +} diff --git a/R/shiny.R b/R/shiny.R new file mode 100644 index 0000000..71185ea --- /dev/null +++ b/R/shiny.R @@ -0,0 +1,76 @@ +#'@title Standardised Input Selector for Smoothing +#'@param input_id ID +#'@param label Label +#'@family inputs +#'@export +smooth_select <- function(input_id, label = "Smoothing") { + return(selectInput(inputId = input_id, label = label, selectize = TRUE, + selected = "global", choices = c("Use Global Setting" = "global", + "No Smoothing" = "day", "Weekly Median" = "week", "Monthly Median" = "month"))) +} +#'@title Standardized Drop-down Selector for Time Frame +#'@param input_id ID +#'@param label Label +#'@return A \code{selectInput} +#'@family inputs +#'@seealso timeframe_daterange +#'@export +timeframe_select <- function(input_id, label = "Time Frame") { + return(selectInput(inputId = input_id, label = label, selectize = TRUE, selected = "global", + choices = c("Use Global Setting" = "global", "All available data" = "all", + "Last 7 days" = "week", "Last 30 days" = "month", + "Last 90 days" = "quarter", "Custom" = "custom"))) +} +#'@title Standardized Date Range Selector for Time Frame +#'@param select_input_id The ID you used for the corresponding +#' \code{timeframe_select} +#'@param label Label +#'@return A \code{conditionalPanel} containing a \code{dateRangeInput} +#'@family inputs +#'@seealso timeframe_select +#'@export +timeframe_daterange <- function(select_input_id, label = "Custom Date Range") { + return(conditionalPanel(paste0("input.", select_input_id," == 'custom'"), + dateRangeInput(paste(select_input_id, "daterange", sep = "_"), label = label, + start = Sys.Date()-11, end = Sys.Date()-1, min = "2015-04-14"))) +} + +#'@title Get The Time Range +#'@description This figures out the date range based on the four different +#'inputs: local & global timeframe selections and local & global date range +#'selections. It is used for \code{subset_by_date_range}. +#'@param input_local_timeframe The value of the input corresponding to the +#' local timeframe selector. +#'@param input_local_daterange The value of the input corresponding to the +#' local date range selector. +#'@param input_global_timeframe The value of the input corresponding to the +#' global timeframe selector. +#'@param input_global_daterange The value of the input corresponding to the +#' global date range selector. +#'@return A Date vector of length 2 +#'@examples +#'\dontrun{ +#'time_frame_range(input$timeframe, input$daterange, +#' input$timeframe_global, input$daterange_global) +#'} +#'@seealso subset_by_date_range +#'@export +time_frame_range <- function(input_local_timeframe, + input_local_daterange, + input_global_timeframe, + input_global_daterange) { + tf_setting <- input_local_timeframe + if ( tf_setting == 'global' ) { + if ( input_global_timeframe == 'custom' ) { + return(input_global_daterange) + } else { + tf_setting <- input_global_timeframe + } + } + return(switch(tf_setting, + all = c(as.Date("2015-04-14"), Sys.Date()-1), + week = c(Sys.Date()-8, Sys.Date()-1), + month = c(Sys.Date()-31, Sys.Date()-1), + quarter = c(Sys.Date()-91, Sys.Date()-1), + custom = input_local_daterange)) +} diff --git a/man/cbind_fill.Rd b/man/cbind_fill.Rd new file mode 100644 index 0000000..f5a96c7 --- /dev/null +++ b/man/cbind_fill.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/manipulate.R +\name{cbind_fill} +\alias{cbind_fill} +\title{Safely Combine R Objects of Variying Lengths by Columns} +\usage{ +cbind_fill(...) +} +\arguments{ +\item{...}{Vectors or matrices.} +} +\value{ +A matrix with NAs wherever needed. +} +\description{ +Take a sequence of vector, matrix or data-frame arguments and + combine by columns or rows, respectively. +} +\references{ +\url{http://r.789695.n4.nabble.com/How-to-join-matrices-of-different-row-length-from-a-list-td3177212.html} +} + diff --git a/man/compress.Rd b/man/compress.Rd index 5aa2a33..68ec8a1 100644 --- a/man/compress.Rd +++ b/man/compress.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.1.1): do not edit by hand +% Generated by roxygen2: do not edit by hand % Please edit documentation in R/maths.R \name{compress} \alias{compress} diff --git a/man/cond_color.Rd b/man/cond_color.Rd index 3faa7ae..7129613 100644 --- a/man/cond_color.Rd +++ b/man/cond_color.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.1.1): do not edit by hand +% Generated by roxygen2: do not edit by hand % Please edit documentation in R/dygraphs.R \name{cond_color} \alias{cond_color} diff --git a/man/cond_icon.Rd b/man/cond_icon.Rd index 9b79ddc..b3963ff 100644 --- a/man/cond_icon.Rd +++ b/man/cond_icon.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.1.1): do not edit by hand +% Generated by roxygen2: do not edit by hand % Please edit documentation in R/dygraphs.R \name{cond_icon} \alias{cond_icon} diff --git a/man/custom_axis_formatter.Rd b/man/custom_axis_formatter.Rd new file mode 100644 index 0000000..817449a --- /dev/null +++ b/man/custom_axis_formatter.Rd @@ -0,0 +1,23 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/dygraphs.R +\docType{data} +\name{custom_axis_formatter} +\alias{custom_axis_formatter} +\title{Custom Axis Formatter} +\format{An object of class \code{character} of length 1.} +\usage{ +custom_axis_formatter +} +\description{ +This is a JS date formatter to be used in \code{dyAxis} when + it's desired to have the dates on the axis look like "Monday (8/13)", in + other words: "Day of the week (MM/DD)" +} +\examples{ +\dontrun{ +polloi::make_dygraph(data) \%>\% + dyAxis("x", axisLabelFormatter = polloi::custom_axis_formatter) +} +} +\keyword{datasets} + diff --git a/man/half.Rd b/man/half.Rd index 4085ac9..8a1d052 100644 --- a/man/half.Rd +++ b/man/half.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.1.1): do not edit by hand +% Generated by roxygen2: do not edit by hand % Please edit documentation in R/manipulate.R \name{half} \alias{half} diff --git a/man/mad.Rd b/man/mad.Rd index 00e8047..994421f 100644 --- a/man/mad.Rd +++ b/man/mad.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.1.1): do not edit by hand +% Generated by roxygen2: do not edit by hand % Please edit documentation in R/maths.R \name{mad} \alias{mad} diff --git a/man/make_dygraph.Rd b/man/make_dygraph.Rd index 0b79692..3deb686 100644 --- a/man/make_dygraph.Rd +++ b/man/make_dygraph.Rd @@ -1,11 +1,11 @@ -% Generated by roxygen2 (4.1.1): do not edit by hand +% Generated by roxygen2: do not edit by hand % Please edit documentation in R/dygraphs.R \name{make_dygraph} \alias{make_dygraph} -\title{Construct a Standard Wikimedia Dygraph} +\title{Construct a Standard Wikimedia Discovery Dygraph} \usage{ make_dygraph(data, xlab, ylab, title, legend_name = NULL, use_si = TRUE, - expr = NULL) + expr = NULL, group = NULL, ...) } \arguments{ \item{data}{a data.frame reformatted to be XTS-able.} @@ -21,8 +21,13 @@ \item{use_si}{whether to use si labelling (1000 becomes 1K). TRUE by default.} -\item{expr}{an optional expression to evaluate prior to building the dygraph. We use this in -(for example) reactive graphing.} +\item{expr}{an optional expression to evaluate prior to building the dygraph. +We use this in (for example) reactive graphing.} + +\item{group}{Group to associate this plot with. The x-axis zoom level of +plots within a group is automatically synchronized.} + +\item{...}{Additional parameters to pass on to \code{dyOptions}.} } \description{ Construct a dygraph using the custom formatting Wikimedia dashboards use diff --git a/man/percent_change.Rd b/man/percent_change.Rd index 867808b..d7c7e91 100644 --- a/man/percent_change.Rd +++ b/man/percent_change.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.1.1): do not edit by hand +% Generated by roxygen2: do not edit by hand % Please edit documentation in R/maths.R \name{percent_change} \alias{percent_change} diff --git a/man/read_dataset.Rd b/man/read_dataset.Rd index a38d951..70e95ca 100644 --- a/man/read_dataset.Rd +++ b/man/read_dataset.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.1.1): do not edit by hand +% Generated by roxygen2: do not edit by hand % Please edit documentation in R/reading.R \name{read_dataset} \alias{read_dataset} diff --git a/man/safe_tail.Rd b/man/safe_tail.Rd index 40572f9..4d76bb0 100644 --- a/man/safe_tail.Rd +++ b/man/safe_tail.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.1.1): do not edit by hand +% Generated by roxygen2: do not edit by hand % Please edit documentation in R/manipulate.R \name{safe_tail} \alias{safe_tail} diff --git a/man/smooth_select.Rd b/man/smooth_select.Rd new file mode 100644 index 0000000..03de68d --- /dev/null +++ b/man/smooth_select.Rd @@ -0,0 +1,18 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/shiny.R +\name{smooth_select} +\alias{smooth_select} +\title{Standardised Input Selector for Smoothing} +\usage{ +smooth_select(input_id, label = "Smoothing") +} +\arguments{ +\item{input_id}{ID} + +\item{label}{Label} +} +\seealso{ +Other inputs: \code{\link{timeframe_daterange}}, + \code{\link{timeframe_select}} +} + diff --git a/man/smooth_switch.Rd b/man/smooth_switch.Rd index 91aef26..b93ecc9 100644 --- a/man/smooth_switch.Rd +++ b/man/smooth_switch.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.1.1): do not edit by hand +% Generated by roxygen2: do not edit by hand % Please edit documentation in R/smoothing.R \name{smooth_switch} \alias{smooth_switch} diff --git a/man/smoother.Rd b/man/smoother.Rd index a1f5a3b..707ee1e 100644 --- a/man/smoother.Rd +++ b/man/smoother.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.1.1): do not edit by hand +% Generated by roxygen2: do not edit by hand % Please edit documentation in R/smoothing.R \name{smoother} \alias{smoother} diff --git a/man/subset_by_date_range.Rd b/man/subset_by_date_range.Rd index e5cede6..ceddf34 100644 --- a/man/subset_by_date_range.Rd +++ b/man/subset_by_date_range.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.1.1): do not edit by hand +% Generated by roxygen2: do not edit by hand % Please edit documentation in R/manipulate.R \name{subset_by_date_range} \alias{subset_by_date_range} @@ -12,13 +12,10 @@ \item{range}{A vector of length 2} -\item{from,to}{A character or Date object to use as upper/lower bound for +\item{from, to}{A character or Date object to use as upper/lower bound for the subsetting.} \item{date_col}{Just in case the date column is named something else other than 'date'} -} -\description{ -Subset a data frame by a date range } diff --git a/man/time_frame_range.Rd b/man/time_frame_range.Rd new file mode 100644 index 0000000..9eb60c3 --- /dev/null +++ b/man/time_frame_range.Rd @@ -0,0 +1,40 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/shiny.R +\name{time_frame_range} +\alias{time_frame_range} +\title{Get The Time Range} +\usage{ +time_frame_range(input_local_timeframe, input_local_daterange, + input_global_timeframe, input_global_daterange) +} +\arguments{ +\item{input_local_timeframe}{The value of the input corresponding to the +local timeframe selector.} + +\item{input_local_daterange}{The value of the input corresponding to the +local date range selector.} + +\item{input_global_timeframe}{The value of the input corresponding to the +global timeframe selector.} + +\item{input_global_daterange}{The value of the input corresponding to the +global date range selector.} +} +\value{ +A Date vector of length 2 +} +\description{ +This figures out the date range based on the four different +inputs: local & global timeframe selections and local & global date range +selections. It is used for \code{subset_by_date_range}. +} +\examples{ +\dontrun{ +time_frame_range(input$timeframe, input$daterange, + input$timeframe_global, input$daterange_global) +} +} +\seealso{ +subset_by_date_range +} + diff --git a/man/timeframe_daterange.Rd b/man/timeframe_daterange.Rd new file mode 100644 index 0000000..31314af --- /dev/null +++ b/man/timeframe_daterange.Rd @@ -0,0 +1,24 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/shiny.R +\name{timeframe_daterange} +\alias{timeframe_daterange} +\title{Standardized Date Range Selector for Time Frame} +\usage{ +timeframe_daterange(select_input_id, label = "Custom Date Range") +} +\arguments{ +\item{select_input_id}{The ID you used for the corresponding +\code{timeframe_select}} + +\item{label}{Label} +} +\value{ +A \code{conditionalPanel} containing a \code{dateRangeInput} +} +\seealso{ +timeframe_select + +Other inputs: \code{\link{smooth_select}}, + \code{\link{timeframe_select}} +} + diff --git a/man/timeframe_select.Rd b/man/timeframe_select.Rd new file mode 100644 index 0000000..17c92e9 --- /dev/null +++ b/man/timeframe_select.Rd @@ -0,0 +1,23 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/shiny.R +\name{timeframe_select} +\alias{timeframe_select} +\title{Standardized Drop-down Selector for Time Frame} +\usage{ +timeframe_select(input_id, label = "Time Frame") +} +\arguments{ +\item{input_id}{ID} + +\item{label}{Label} +} +\value{ +A \code{selectInput} +} +\seealso{ +timeframe_daterange + +Other inputs: \code{\link{smooth_select}}, + \code{\link{timeframe_daterange}} +} + -- To view, visit https://gerrit.wikimedia.org/r/252269 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I89006df4eff59fc4b2d881dbbc69e15d5fd80729 Gerrit-PatchSet: 2 Gerrit-Project: wikimedia/discovery/polloi Gerrit-Branch: master Gerrit-Owner: Bearloga <mpo...@wikimedia.org> Gerrit-Reviewer: OliverKeyes <oke...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits