Skip to content

dySliderInput plugin and minor bug fix #135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jeffreyhanson
Copy link
Contributor

dySliderInput

Overview

This pull request includes a dySliderInput plugin that turns dygraphs into a slider input widget. Check out this online demo at shinyapps.io. Dygraphs offers great potential for many plugins and I think it would be great if these were all available from this package so that users can easily access them. To that end, I have added functions for this plugin and the other plugins (dyUnzoom and dyCrosshair) so that users can use these plugins. It also fixes a bug in returning date-times from dygraphs.

How does dySliderInput work?

I have written a dygraphs plugin called dySliderInput in javascript. This plugin can be compiled using the dyPlugin function and added to an existing dygraph object. Once added to a dygraph object, users can click on the dygraph and a bar will be added to mark closest data point (which can be accessed programatically using input$dygraph_click$date). Additionally, users can animate the slider bar by clicking on the play button. The animation can be customised similar to that in shiny::sliderInput using the shiny::animationOptions function. This plugin is best used when in conjunction with other widgets. For instance, it could be used with the leaflet R package to show spatio-temporal data (as shown in the demo). This plugin is compatible with the dyRangeSelector. I have included the above demo as an example in the package.

Proposed usage of dySliderInput

#' dySliderInput
#' 
#' @inheritParams dyPlugin
#' @param color Color to draw slider. Defaults to 'red'.
#' @param strokePattern Line type for slider. Defaults to 'dashed'. Valid
#' arguments are 'dashed', 'solid', 'dotted', and 'dotdash'.
#' @param animate 'TRUE' to show simple animation controls with default 
#' settings; 'FALSE' not to; or a custom settings list, such as those
#' created using 'animationOptions'.
#'
#' @return A dygraph with the specified plugin enabled.
#'
#' @details The dySliderInput plugin turns the dyDygraph into a slider input
#' widget. The user can click on a point along the graph and the graph
#' will place a vertical line on the graph. The user can also use the animation
#' options to scroll through points along the graph.
#' 
#' @examples 
#' library(dygraphs)
#' dygraph(mdeaths) %>%
#'   dySliderInput()
#' 

List of changes

Here I will list all the changes I have made to this package, and my rationale behind them.

dySliderInput

  • NAMESPACE: Now includes the dySliderInput function and functions it uses in other packages (grDevices::col2rgb, shiny::icon, and shiny::animationOptions).
  • DESCRIPTION: The dySliderInput function uses the animationOptions and icon functions from shiny. Thus I have added 'shiny' to the imports section of the package. Additionally, because devtools::check() does not allow packages to listed under imports and enchances, I have removed shiny from enhances.
  • R/plugins.R: I have added the dySliderInput function to allow users to add this plugin.
  • tests/testthat/test-slider-input.R: This provides a test for the dySliderInput function.
  • inst/examples/shiny_2: I have added the code for the dySliderInput demo here.
  • inst/examples/plugins/sliderinput.js: This include the code for the dySliderInput plugin.

Bug fix

  • inst/htmlwidgets/dygraphs.js:
    • Currently, when users click on a dygraph widget, this updates the input$dygraph_click and input$dygraph_date_window variables in a shiny session. The variables are based on a call to new Date(x) in javascript. However, just returning this can cause the returned dates to be incorrect.
    • Instead, to return the correct dates, a date-time string needs to be parsed manually after generating the Date object. To do this, I have included a shinyValueFormatter function that uses the function that generates x-axis labels (xValueFormatterFixedTZ or xValueFormatter) to correctly parse the string.
    • Additionally, since xValueFormatter originally made a call to Date().toLocaleString which uses different date-time formats depending on the environment, I have changed this to manually parse the same date-time format returned from xValueFormatterFixedTZ (but without the time-zone information).

DyUnzoom and dyCrosshair functions

  • NAMESPACE: Now includes the dygraphs::dyCrosshair, dygraphs::dyUnzoom functions.
  • R/plugins.R: I have added the dyCrosshair, dyUnzoom functions to allow users to add these plugins.
  • tests/testthat/test-crosshair.R: This provides a test for the dyCrosshair function.
  • tests/testthat/test-unzoom.R: This provides a test for the dyUnzoom function.

Miscellaneous

  • I have added '.directory' and 'rsconnect' to the .gitignore to prevent these junk files from polluting the repository.

I really would like to see the dySliderInput widget incorporated into the dygraphs R package, so please let me know if you disagree with any of these changes, and I will update my PR accordingly.

@jjallaire
Copy link
Member

This is a great PR and I very much look forward to merging it! I'm tied up this week but will have a look early the following week.

@przmv
Copy link
Collaborator

przmv commented Feb 16, 2018

@jeffreyhanson Could you please divide your PR into several smaller ones to easier review and merge?

@jeffreyhanson
Copy link
Contributor Author

I'm sorry I don't have any time to work on this for the foreseeable future. Please feel free to close this PR if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants