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

Dygraph attribute in HTMLWidget is null #196

Closed
prise6 opened this issue Jan 10, 2018 · 15 comments · Fixed by #197
Closed

Dygraph attribute in HTMLWidget is null #196

prise6 opened this issue Jan 10, 2018 · 15 comments · Fixed by #197

Comments

@prise6
Copy link

prise6 commented Jan 10, 2018

dygraph: dygraph

Hi, thanks to this documentation, we can see that the javascript objet can be accessed.
In this implementation of Dygraph, i should access thanks to HTMLWidgets.find('.dygraphs').dygraph, but it seems that it is null.
Am i wrong ? Is there a way to access the dygraph object ?

Thanks.

@danklotz
Copy link

not really helpful for you, but it might be related to issue #127

@timelyportfolio
Copy link
Contributor

timelyportfolio commented Jan 12, 2018

@prise6, I just checked and you are correct even though line is designed to accomplish this. Unfortunately, the joys of JavaScript scoping means this does not work. I will submit a pull that should resolve the issue.

As a side note, I will caution that you will also need to wait for the htmlwidget to render before dygraph is truly available.


...from what I remember at one point `dygraph` was available, so I think (but not all that confident) that something changed.

@prise6
Copy link
Author

prise6 commented Jan 12, 2018

Ok thank you, agree with your fix.
@danklotz : i think it will fix your issues too.

The joys of JavaScript scoping

Haha 👍

@danklotz
Copy link

@prise6 Mh.. Not sure about that. HTMLWidgets.getInstance still seems to give Null. As in:

HTMLWidgets.getInstance(document.getElementById('element_g1')).dygraph

@timelyportfolio
Copy link
Contributor

timelyportfolio commented Jan 13, 2018

@danklotz, there are 3 patterns that I have used to make sure the object is available to the outside.

  1. export so that it is available with HTMLWidgets.findWidget
  2. attach so that it is available with HTMLWidgets.getInstance
  3. attach with expando property to the DOM node

Reading the code led me to believe that the intent was the first HTMLWidgets.findWidget, so I used that approach in my pull request. Happy to change given some guidance from @jjallaire.

As another RStudio htmlwidget, we can look at how this is done in leaflet. leaflet pursues a slightly modified version 1 by providing a function getMap() available through HTMLWidgets.findWidget(). See lines.

@danklotz
Copy link

you (@timelyportfolio) are of course right (and your correction does what it is supposed to do).

It just seemed that the problems are very similar (so I was hoping). I just want to be able to have a synchronised mouse over and/or selector over multiple dygraphs. Alas, my JS skills are to rudimentary to solve this : /

@przmv
Copy link
Collaborator

przmv commented Jan 21, 2018

@prise6 What's your high-level goal? Why do you need to access the dygraph instance?

@jjallaire
Copy link
Member

We made a change some time back to enable the second pattern (HTMLWidgets.getInstnace). See 08a4d59

@timelyportfolio
Copy link
Contributor

@jjallaire I see those changes but unfortuately they do not work for me, which resulted in #197.

@danklotz
Copy link

Both still don't work for me. (Tested it on a Mac, but that should not change anything).

@przmv
Copy link
Collaborator

przmv commented Jan 22, 2018

@danklotz Could you please provide a Minimal Working Example (https://www.r-bloggers.com/writing-a-minimal-working-example-mwe-in-r/)?

@danklotz
Copy link

danklotz commented Jan 22, 2018

Yes. But, I think the the example in #127 (by @ijlyttle) is ok. No?
So far, the following code does not work for me:

library("dygraphs")
library("htmltools")

dygraph(mdeaths, elementId = "mdeaths")

tags$script("
  var elem = document.getElementById('mdeaths');
  var dyg = HTMLWidgets.getInstance(elem);
  
  console.log(elem);
  console.log(dyg);
")

(also not with the corrections of #197)

@timelyportfolio
Copy link
Contributor

timelyportfolio commented Jan 23, 2018

@danklotz, I had tried to warn earlier that htmlwidgets render after page load, so the instance will not be available immediately. To access will require a delay or asynchrony. Here is a quick example that might help explain using my pull request version.

library(htmltools)
library(dygraphs)

browsable(
  tagList(
    dygraph(mdeaths),
    tags$script(
"
console.log(HTMLWidgets.find('.dygraphs'))

setTimeout(
  function() {
    console.log(i, HTMLWidgets.find('.dygraphs').dygraph)
  }, 50
)
"
    )
  )
)

One other way to access after render is to use htmlwidgets::onRender().

dy <- dygraph(mdeaths)
htmlwidgets::onRender(
  dy,
"
function(el, x) {
  debugger
  // this will equal instance
  console.log(this.dygraph)
}
"
)

@danklotz
Copy link

danklotz commented Jan 23, 2018

Yessss. Makes me so happy :) 👍

(Sorry for beeing stupid/Not understanding the warning)

@prise6
Copy link
Author

prise6 commented Feb 21, 2018

@pshevtsov i don't think my high level goal is really relevant, but i wanted to synchronized multiple dygraphs. You'll find a tiny repo to see it can works with PR of @timelyportfolio : https://github.com/prise6/R-dygraphs-sync

Anyway, @timelyportfolio provides good examples to run with current package.

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 a pull request may close this issue.

5 participants