简体   繁体   中英

RStudio Viewer throws error

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.

在此处输入图片说明

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

在此处输入图片说明


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

在此处输入图片说明

在此处输入图片说明

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

@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!

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM