Skip to content

Commit

Permalink
fix purrr thing and widocumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
jbkunst committed Jan 11, 2016
1 parent 686735f commit 4d8a681
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 75 deletions.
32 changes: 30 additions & 2 deletions R/api.R
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ hc_legend <- function(hc, ...) {
#' hc_add_serie(name = "Tokyo", data = citytemp$tokyo) %>%
#' hc_add_serie(name = "London", data = citytemp$london) %>%
#' hc_tooltip(crosshairs = TRUE, backgroundColor = "gray",
#' headerFormat = "This is a custom header<br>",
#' shared = TRUE, borderWidth = 5)
#'
#' @param hc A \code{highchart} \code{htmlwidget} object.
Expand All @@ -208,7 +209,34 @@ hc_tooltip <- function(hc, ...) {
#' \code{hc_plotOptions(line = list(...))}. Next, options for one single series are given in the series array.
#'
#' @param hc A \code{highchart} \code{htmlwidget} object.
#' @param ... Arguments are defined in \url{http://api.highcharts.com/highcharts#plotOptions}.
#' @param ... Arguments are defined in \url{http://api.highcharts.com/highcharts#plotOptions}.
#'
#' @examples
#'
#' require("dplyr")
#'
#' data(citytemp)
#'
#' hc <- highchart() %>%
#' hc_plotOptions(line = list(color = "blue",
#' marker = list(
#' fillColor = "white",
#' lineWidth = 2,
#' lineColor = NULL
#' )
#' )) %>%
#' hc_add_serie(name = "Tokyo", data = citytemp$tokyo) %>%
#' hc_add_serie(name = "London", data = citytemp$london,
#' marker = list(fillColor = "black"))
#'
#'
#' hc
#'
#' #' override the `blue` option with the explicit parameter
#' hc %>%
#' hc_add_serie(name = "London",
#' data = citytemp$new_york,
#' color = "red")
#'
#' @export
hc_plotOptions <- function(hc, ...) {
Expand All @@ -219,7 +247,7 @@ hc_plotOptions <- function(hc, ...) {

#' Adding Color Axis options to highchart objects
#'
#'
#' Function to set the axis color to highcharts objects.
#'
#' @param hc A \code{highchart} \code{htmlwidget} object.
#' @param ... Arguments are defined in \url{http://www.highcharts.com/docs/maps/color-axis}.
Expand Down
6 changes: 4 additions & 2 deletions R/shortcuts.R
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,13 @@ hc_add_serie_treemap <- function(hc, tm, ...) {
tbl_df() %>%
select_("-x0", "-y0", "-w", "-h", "-stdErr", "-vColorValue") %>%
rename_("value" = "vSize", "valuecolor" = "vColor") %>%
map_if(is.factor, as.character)
purrr::map_if(is.factor, as.character) %>%
data.frame(stringsAsFactors = FALSE) %>%
tbl_df()

ndepth <- which(names(df) == "value") - 1

ds <- ldply(seq(ndepth), function(lvl){ # lvl <- sample(size = 1, seq(ndepth))
ds <- ldply(seq(ndepth), function(lvl){ # lvl <- sample(seq(ndepth), size = 1)

df2 <- df %>%
filter_(sprintf("level == %s", lvl)) %>%
Expand Down
71 changes: 35 additions & 36 deletions R/theme.R
Original file line number Diff line number Diff line change
@@ -1,35 +1,15 @@
#' Add themes to a highchart object
#'
#' Add highcharts themes to a highchart object.
#'
#' @param hc A highchart object
#' @param hc_thm A highchart theme object (\code{"hc_theme"} class)
#' @export
hc_add_theme <- function(hc, hc_thm){

assert_that(.is_highchart(hc),
.is_hc_theme(hc_thm))

hc$x$fonts <- unique(c(hc$x$fonts, .hc_get_fonts(hc_thm)))

hc$x$theme <- hc_thm

hc

}

#' Highchart theme constructor
#'
#' Function to create highcharts themes.
#'
#' @param ... Usually named list
#' @param ... A named list with the parameters.
#'
#' @examples
#'
#' \dontrun{
#' require("dplyr")
#'
#' hc <- highchart(debug = TRUE) %>%
#' hc_add_serie_scatter(mtcars$wt, mtcars$mpg, mtcars$cyl) %>%
#' hc_add_serie_scatter(mtcars$wt, mtcars$mpg, color = mtcars$cyl) %>%
#' hc_chart(zoomType = "xy") %>%
#' hc_title(text = "Motor Trend Car Road Tests") %>%
#' hc_subtitle(text = "Motor Trend Car Road Tests") %>%
Expand Down Expand Up @@ -70,36 +50,39 @@ hc_add_theme <- function(hc, hc_thm){
#'
#' hc %>% hc_add_theme(thm)
#'
#' }
#'
#' @export
hc_theme <- function(...){

structure(list(...), class = "hc_theme")

}

#' @importFrom stringr str_replace_all str_replace str_trim
.hc_get_fonts <- function(lst){
#' Add themes to a highchart object
#'
#' Add highcharts themes to a highchart object.
#'
#' @param hc A highchart object
#' @param hc_thm A highchart theme object (\code{"hc_theme"} class)
#'
#' @export
hc_add_theme <- function(hc, hc_thm){

unls <- unlist(lst)
unls <- unls[grepl("fontFamily", names(unls))]
assert_that(.is_highchart(hc),
.is_hc_theme(hc_thm))

fonts <- unls %>%
str_replace_all(",\\s+sans-serif|,\\s+serif", "") %>%
str_replace("\\s+", "+") %>%
str_trim() %>%
unlist()
hc$x$fonts <- unique(c(hc$x$fonts, .hc_get_fonts(hc_thm)))

fonts
hc$x$theme <- hc_thm

hc

}

#' Merge themes
#'
#' Function to combine hc_theme objects.
#'
#' @param ... \code{hc_theme} objects
#' @param ... A \code{hc_theme} objects.
#'
#' @export
hc_theme_merge <- function(...){
Expand All @@ -113,3 +96,19 @@ hc_theme_merge <- function(...){
theme

}

#' @importFrom stringr str_replace_all str_replace str_trim
.hc_get_fonts <- function(lst){

unls <- unlist(lst)
unls <- unls[grepl("fontFamily", names(unls))]

fonts <- unls %>%
str_replace_all(",\\s+sans-serif|,\\s+serif", "") %>%
str_replace("\\s+", "+") %>%
str_trim() %>%
unlist()

fonts

}
6 changes: 4 additions & 2 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
drillUpText = "Back to list(series.name)",
invalidDate = NULL,
loading = "Loading...",
months = c( "January" , "February" , "March" , "April" , "May" , "June" , "July" , "August" , "September" , "October" , "November" , "December"),
months = c( "January" , "February" , "March" , "April" , "May" , "June" ,
"July" , "August" , "September" , "October" , "November" , "December"),
noData = "No data to display",
numericSymbols = c( "k" , "M" , "G" , "T" , "P" , "E"),
printChart = "Print chart",
resetZoom = "Reset zoom",
resetZoomTitle = "Reset zoom level 1:1",
shortMonths = c( "Jan" , "Feb" , "Mar" , "Apr" , "May" , "Jun" , "Jul" , "Aug" , "Sep" , "Oct" , "Nov" , "Dec"),
shortMonths = c( "Jan" , "Feb" , "Mar" , "Apr" , "May" , "Jun" ,
"Jul" , "Aug" , "Sep" , "Oct" , "Nov" , "Dec"),
thousandsSep = " ",
weekdays = c("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
),
Expand Down
2 changes: 1 addition & 1 deletion man/hc_colorAxis.Rd

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

28 changes: 28 additions & 0 deletions man/hc_plotOptions.Rd

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

8 changes: 3 additions & 5 deletions man/hc_theme.Rd

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

2 changes: 1 addition & 1 deletion man/hc_theme_merge.Rd

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

1 change: 1 addition & 0 deletions man/hc_tooltip.Rd

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

52 changes: 26 additions & 26 deletions no_build/index.R
Original file line number Diff line number Diff line change
Expand Up @@ -293,31 +293,17 @@ hc

##' # Shorcuts for add Data (data series) ####

##' ## Time Series ####

data(economics, package = "ggplot2")

highchart() %>%
hc_add_serie_ts(economics$psavert, economics$date,
name = "Personal Savings Rate")

#' There's a `hc_add_serie_ts2` which recieve a `ts`object.

highchart() %>%
hc_add_serie_ts2(AirPassengers, color = "#26838E")

highchart() %>%
hc_title(text = "Monthly Deaths from Lung Diseases in the UK") %>%
hc_subtitle(text = "Deaths from bronchitis, emphysema and asthma") %>%
hc_add_serie_ts2(fdeaths, name = "Female") %>%
hc_add_serie_ts2(mdeaths, name = "Male")

##' ## Scatter plot ####
##' ## Scatter ####

highchart() %>%
hc_title(text = "Simple scatter chart") %>%
hc_add_serie_scatter(mtcars$wt, mtcars$mpg)

highchart() %>%
hc_title(text = "Scatter chart with color") %>%
hc_add_serie_scatter(mtcars$wt, mtcars$mpg,
color = mtcars$hp)

highchart() %>%
hc_title(text = "Scatter chart with size") %>%
hc_add_serie_scatter(mtcars$wt, mtcars$mpg,
Expand All @@ -328,11 +314,6 @@ highchart() %>%
hc_add_serie_scatter(mtcars$wt, mtcars$mpg,
mtcars$drat, mtcars$hp)

highchart() %>%
hc_title(text = "Scatter chart with color and no size") %>%
hc_add_serie_scatter(mtcars$wt, mtcars$mpg,
color = mtcars$hp)

highchart(height = 500) %>%
hc_title(text = "A complete example for Scatter") %>%
hc_add_serie_scatter(mtcars$wt, mtcars$mpg,
Expand All @@ -352,7 +333,7 @@ highchart(height = 500) %>%
"<tr><th>HP</th><td>{point.valuecolor} hp</td></tr>"),
footerFormat = "</table>")

# or We can add series one by one.
#' Or we can add series one by one.
hc <- highchart()
for (cyl in unique(mtcars$cyl)) {
hc <- hc %>%
Expand All @@ -364,6 +345,25 @@ for (cyl in unique(mtcars$cyl)) {

hc

##' ## Time Series ####

data(economics, package = "ggplot2")

highchart() %>%
hc_add_serie_ts(economics$psavert, economics$date,
name = "Personal Savings Rate")

#' There's a `hc_add_serie_ts2` which recieve a `ts`object.

highchart() %>%
hc_add_serie_ts2(AirPassengers, color = "#26838E")

highchart() %>%
hc_title(text = "Monthly Deaths from Lung Diseases in the UK") %>%
hc_subtitle(text = "Deaths from bronchitis, emphysema and asthma") %>%
hc_add_serie_ts2(fdeaths, name = "Female") %>%
hc_add_serie_ts2(mdeaths, name = "Male")

##' ## Treemaps ####
#'
#' Here we use the `treemap` package to create a treemap object and then
Expand Down

0 comments on commit 4d8a681

Please sign in to comment.