JohnCoene / javascript-for-r-comments

1 stars 0 forks source link

7.4.1 - JS code not working? #23

Closed MayaGans closed 4 years ago

MayaGans commented 4 years ago

I am trying to use this code to render the arc data in our geo widget but I only see a blank page - what am I missing?

R code from 7.3

gio <- function(message, width = NULL, height = NULL, elementId = NULL) {

  # forward options using x
  x = list(
    data = data
  )

  # create widget
  htmlwidgets::createWidget(
    name = 'gio',
    x,
    width = width,
    height = height,
    package = 'gio',
    elementId = elementId
  )
}

Updated JS code 7.4.1

renderValue: function(x) {

  // long to wide
  x.data = HTMLWidgets.dataframeToD3(x.data);

  var controller = new GIO.Controller(el);
  controller.addData(x.data); 
  controller.init();

}

Now shouldn't this work?

arcs <- jsonlite::fromJSON(
  '[
    {
      "e": "CN",
      "i": "US",
      "v": 3300000
    },
    {
      "e": "CN",
      "i": "RU",
      "v": 10000
    }
  ]'
)

jsonlite::toJSON(arcs, dataframe = "columns")

devtools::load_all()
gio(arcs)

What am I missing?

JohnCoene commented 4 years ago

Maybe not explained properly in the code. The gio function you have takes an argument message when it should be named data.

gio <- function(data, width = NULL, height = NULL, elementId = NULL) {

  # forward options using x
  x = list(
    data = data
  )

  # create widget
  htmlwidgets::createWidget(
    name = 'gio',
    x,
    width = width,
    height = height,
    package = 'gio',
    elementId = elementId
  )
}
JohnCoene commented 4 years ago

I just tried again and everything works fine. Are you sure you have the dependencies (7.1)?

MayaGans commented 4 years ago

I just invited you as a collab to a private repo where I believe I have all the correct code up to 7.4.1? [I looked at the code you pasted and my code above but don't see any differences, am I missing something? In the code above I think I did change it from message to data?] It totally could be an error on my end but thought I should bring it up just in case!

MayaGans commented 4 years ago

And of course there's no errors in the console - thanks a lot JS 😆

JohnCoene commented 4 years ago

I'm so sorry you had to go through this, I hope it's not a typo in my book.

In your DESCRIPTION you have

Package: geo

And htmlwidgets relies on the package name in the htmlwidgets::createWidget function (in the gio) function to load up the dependencies.

Use this and it will work (I just tried)

  # create widget
  htmlwidgets::createWidget(
    name = 'gio',
    x,
    width = width,
    height = height,
    package = 'geo', # needs package name
    elementId = elementId
  )
JohnCoene commented 4 years ago

(Or alternatively you could change the package name of course)

MayaGans commented 4 years ago

Yay! Although slightly embarrassed.... Glad the error was on my end and not yours - thanks for looking into it!

JohnCoene commented 4 years ago

Not at all! My pleasure :) I'm the one thanking you for so thoroughly going through the book: thank you very much!