3

Issue

Some versions of RStudio throw an error in the Viewer pane of my javascript widget, and some don't.


Code

I've created a htmlwidget for my googleway package that plots a Google Map.

To reproduce this issue (if indeed it is an issue on your system) you can simply run this code

devtools::install_github("SymbolixAU/googleway")
library(googleway)
google_map(key = '')  ## you don't need a key to see the error

But if you want to view a map, you'll need a Google Maps API key


Description

The issue I'm having is that on some versions of Rstudio the map shows in the Viewer pane, and in others it doesn't.

When I "inspect" the Viewer (right-click > inspect > console), I get the error

SyntaxError: Unexpected identifier 'i'. Expected either 'in' or 'of' in enumeration syntax.

enter image description here

Which links to a for loop inside the javascript (see screenshot and the source code)

enter image description here


This morning I upgraded Rstudio on the system that caused the error, but it's still giving the error.

The following two screenshots show two different Macs (both running OS Sierra) with Rstudio, with examples of

  • Rstudio v1.0.143 - not working
  • Rstudio v1.0.136 - working

enter image description here

enter image description here

Why does some versions of RStudio throw the error, and some don't?

SymbolixAU
  • 25,502
  • 4
  • 67
  • 139
  • I'm voting to close this question as off-topic because its probably better asked on the RStudio support channels. – Spacedman Apr 20 '17 at 07:04
  • not sure but `let` is ES2015 so older browsers will not know how to use it. you can try the old `var` instead. – timelyportfolio Apr 20 '17 at 12:45
  • @timelyportfolio - that could have something to do with it - changing to `var` "fixes" that issue, but then stops at another: `data.find(x => x.id == _id)`, which I suspect is also causing a similar issue : http://stackoverflow.com/a/35398031/5977215 – SymbolixAU Apr 20 '17 at 21:39
  • @timelyportfolio - I think you've hit the nail on the head; changing `let` to `var`, and `.find(x => ...)` to a more primitive loop has solved the issue! – SymbolixAU Apr 20 '17 at 22:05
  • However, I'm still not sure *why* the `let` and `.find()` worked in an older version of Rstudio... – SymbolixAU Apr 20 '17 at 22:07

1 Answers1

4

@timelyportfolio's suggestion was instrumental (again!) in finding the solution.

I'm not convinced the issue was purely due to Rstudio, or there are other factors involved, especially as the widget works on an older version, but for now I'll leave this as the solution.

The let in the line

for (let i = 0; i < data.calls.length; i++) {

is not supported in all browsers, so changing it to var worked for that line (and all the lines that use a let).

I was also using this method for finding values in an array

data_.find(x => x.id === _id)

Which again is not supported in all browsers, so reverting to

function findById(source, id) {
  for (var i = 0; i < source.length; i++) {
    if (source[i].id === id) {
      return source[i];
    }
  }
  return;
}

seems to resolve that issue too.

et voila!

enter image description here

Community
  • 1
  • 1
SymbolixAU
  • 25,502
  • 4
  • 67
  • 139