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

shiny 0.10.2 breaks this rCharts code (works in 0.10.1) #642

Closed
smartinsightsfromdata opened this issue Oct 30, 2014 · 1 comment
Closed

Comments

@smartinsightsfromdata
Copy link

smartinsightsfromdata commented Oct 30, 2014

I've cross posted this in the rCharts project page also
see ramnathv/rCharts#537

The new version of shiny 0.10.2 breaks the code by @Vladicon and @paulyeno in "rCharts Leaflet Heatmap renders in RStudio but not Shiny #498".

After hours trying to understand why I couldn't replicate a simple example, I tried to install shiny 0.10.1 and the code worked perfectly.
The error code appear mysterious to me (but I'm no js expert):
[Error] Failed to load resource: the server responded with a status of 404 (Not Found) (favicon.ico, line 0)

Not sure if it is a shiny or rCharts issue.

This is a sample code

library(shiny)
library(rCharts)
library(rjson)
library(plyr)
##   
crimedf <- na.omit(ggmap::crime[,c("offense","address","lon","lat")])
crime_dat = ddply(crimedf, .(lat, lon), summarise, count = length(address))
f  <- sapply(crimedf,is.factor)
crimedf[f] <- lapply(crimedf[f], as.character)
seLabels <- unique(crimedf$offense)
#
runApp(list(
  ui =   tabPanel("main", fluidPage(
    h4("Crime hotmap"),
#     column(3,
#     selectInput("slCrime", "Choose Crime Type:",
#                                             seLabels)
#     ),
    column(9,
           chartOutput('baseMap','leaflet'),
           tags$style('.leaflet {height: 500px;}'),
           tags$head(tags$script(src="http://leaflet.github.io/Leaflet.heat/dist/leaflet-heat.js"))
            , uiOutput('datamap')
    )
  )),
server = function(input, output, session) {
output$baseMap<-renderMap({
baseMap <- Leaflet$new()
mux <- mean(crimedf$lon)
muy <- mean(crimedf$lat)
baseMap$setView(c(muy,mux),9)
baseMap$tileLayer(provider="OpenStreetMap")
baseMap
 })
output$datamap<-renderUI({  
nowmax <- max(crime_dat$count)
nowjar <- toJSONArray2(crime_dat, json=F, names=F)
s2ins <- rjson::toJSON(nowjar)

tags$body(tags$script(HTML(sprintf("
var addressPoints = %s
var maxval = %f
var heat = L.heatLayer(addressPoints, {maxZoom: 9, radius: 20, blur: 40}).addTo(map)
 </script>",  s2ins, nowmax
))))
})
}
))
@wch
Copy link
Collaborator

wch commented Nov 26, 2014

I think the problem is that this line is getting executed before map is present:

var heat = L.heatLayer(addressPoints, {maxZoom: 9, radius: 20, blur: 40}).addTo(map)

If you comment out that line, then load the page, and then after it loads, run that line in the JS console, it works. I think you'll need to find a way to delay the execution of that JS code until the map is present.

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

No branches or pull requests

2 participants