Skip to content
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

Merge options if plugin was already added #158

Merged
merged 1 commit into from
Mar 6, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions R/dependency.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ dyPlugin <- function(dygraph, name, path, options = list(), version = "1.0") {
if (length(options) == 0) {
options <- JS("{}")
}
# merge options if plugin was already added
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might be missing something, but doesn't this only pick up new options? (i.e. it wouldn't pick up overwriting of an existing option).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it merges new options with the old ones:

old.options <- list(color = "red",
                    fruit = "apple",
                    days = list("Monday", "Tuesday", "Wednesday"),
                    number = 42,
                    figure = "square")
options <- list(color = "blue",
                days = list("Saturday", "Sunday"),
                animal = "cat")

i <- match(names(old.options), names(options))
i <- is.na(i)
if (any(i)) {
  options[names(old.options)[which(i)]] = old.options[which(i)]
}

options
$color
[1] "blue"

$days
$days[[1]]
[1] "Saturday"

$days[[2]]
[1] "Sunday"


$animal
[1] "cat"

$fruit
[1] "apple"

$number
[1] 42

$figure
[1] "square"

Passing such options to a Dygraph plugin instance results in the following JavaScript object:

{
   "animal" : "cat",
   "figure" : "square",
   "days" : [
      "Saturday",
      "Sunday"
   ],
   "fruit" : "apple",
   "number" : 42,
   "color" : "blue"
}

if (name %in% names(dygraph$x$plugins)) {
current.options <- dygraph$x$plugins[[name]]
i <- match(names(current.options), names(options))
i <- is.na(i)
if (any(i)) {
options[names(current.options)[which(i)]] = current.options[which(i)]
}
}
dygraph$x$plugins[[name]] <- options

# return dygraph
Expand Down