Hi there,
Using this Gapminder example, I attempted to connect a SpatialPolygonDataFrame on a leaflet map to its underlying dataframe (displayed in a datatable) using the crosstalk and DT packages.
I get the following error: Error in inherits(x, "SharedData") : attempt to apply non-function when trying to reproduce the Gapminder example.
The line that is causing the error is sd$transform(as.data.frame).
I have a feeling I may need to update something somewhere..
If you copy and paste this code it should replicate the error I am getting.
library(leaflet)
library(crosstalk)
library(dplyr)
library(rgdal)
library(DT)
# install.packages('GWmodel')
# Just using this package for the dublin shapefile to use as an example
library(GWmodel)
data(DubVoter)
dub <- Dub.voter
# The Dub.voter data set has has no CRS
# Coordinates based on the Irish Grid / TM65 - epsg:29902
# epsg:4326 is the WGS84 World Grid that Leaflet uses. Need to re-project to this.
proj4string(dub) <- CRS("+init=epsg:29902")
newProj <- CRS("+init=epsg:4326")
dubTemp <- dub
dub <- spTransform(dubTemp, newProj)
sd <- SharedData$new(dub)
bscols(
leaflet(sd) %>% addProviderTiles("CartoDB.Positron") %>% addPolygons(data = sd),
datatable(sd$transform(as.data.frame), extensions="Scroller", style="bootstrap", class="compact", width="100%",
options=list(deferRender=TRUE, scrollY=300, scroller=TRUE))
)
Here is my session info if that helps.
> sessionInfo()
R version 3.3.2 (2016-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
locale:
[1] LC_COLLATE=English_Ireland.1252 LC_CTYPE=English_Ireland.1252 LC_MONETARY=English_Ireland.1252
[4] LC_NUMERIC=C LC_TIME=English_Ireland.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] GWmodel_2.0-3 Rcpp_0.12.8 robustbase_0.92-7 maptools_0.8-40 DT_0.2.12
[6] rgdal_1.2-4 sp_1.2-3 dplyr_0.5.0 crosstalk_1.0.0 leaflet_1.0.2.9010
loaded via a namespace (and not attached):
[1] magrittr_1.5 xtable_1.8-2 lattice_0.20-34 R6_2.2.0 tools_3.3.2 grid_3.3.2
[7] DBI_0.5-1 htmltools_0.3.5 rgeos_0.3-21 yaml_2.1.14 assertthat_0.1 digest_0.6.10
[13] tibble_1.2 shiny_0.14.2 htmlwidgets_0.8 mime_0.5 DEoptimR_1.0-8 jsonlite_1.1
[19] httpuv_1.3.3 foreign_0.8-67
>
Thanks very much for any help.
Sorry, the SharedData$transform method was from an older version of
Crosstalk and was removed--there were some situations where it didn't seem
to work quite right. It may come back later if needed.
You can achieve the same effect by creating a new SharedData object that
uses the same group name as the original SharedData object, e.g.
sd$transform(as.data.frame) becomes SharedData$new(as.data.frame(dub),
group = sd$groupName()).
On Wed, Apr 26, 2017 at 7:48 AM BilboBaagins notifications@github.com
wrote:
Hi there,
Using this Gapminder example
https://rstudio-pubs-static.s3.amazonaws.com/215948_95c1ab86ad334d2f82856d9e5ebc16af.html,
I attempted to connect a SpatialPolygonDataFrame on a leaflet map to its
underlying dataframe (displayed in a datatable) using the crosstalk and DT
packages.I get the following error: Error in inherits(x, "SharedData") : attempt
to apply non-function when trying to reproduce the Gapminder example.
The line that is causing the error is sd$transform(as.data.frame).
I have a feeling I may need to update something somewhere..If you copy and paste this code it should replicate the error I am getting.
library(leaflet)
library(crosstalk)
library(dplyr)
library(rgdal)
library(DT)install.packages('GWmodel')
Just using this package for the dublin shapefile to use as an example
library(GWmodel)
data(DubVoter)
dub <- Dub.voterThe Dub.voter data set has has no CRS
Coordinates based on the Irish Grid / TM65 - epsg:29902
epsg:4326 is the WGS84 World Grid that Leaflet uses. Need to re-project to this.
proj4string(dub) <- CRS("+init=epsg:29902")
newProj <- CRS("+init=epsg:4326")
dubTemp <- dub
dub <- spTransform(dubTemp, newProj)sd <- SharedData$new(dub)
bscols(
leaflet(sd) %>% addProviderTiles("CartoDB.Positron") %>% addPolygons(data = sd),
datatable(sd$transform(as.data.frame), extensions="Scroller", style="bootstrap", class="compact", width="100%",
options=list(deferRender=TRUE, scrollY=300, scroller=TRUE))
)Here is my session info if that helps.
sessionInfo()
R version 3.3.2 (2016-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1locale:
[1] LC_COLLATE=English_Ireland.1252 LC_CTYPE=English_Ireland.1252 LC_MONETARY=English_Ireland.1252
[4] LC_NUMERIC=C LC_TIME=English_Ireland.1252attached base packages:
[1] stats graphics grDevices utils datasets methods baseother attached packages:
[1] GWmodel_2.0-3 Rcpp_0.12.8 robustbase_0.92-7 maptools_0.8-40 DT_0.2.12
[6] rgdal_1.2-4 sp_1.2-3 dplyr_0.5.0 crosstalk_1.0.0 leaflet_1.0.2.9010loaded via a namespace (and not attached):
[1] magrittr_1.5 xtable_1.8-2 lattice_0.20-34 R6_2.2.0 tools_3.3.2 grid_3.3.2
[7] DBI_0.5-1 htmltools_0.3.5 rgeos_0.3-21 yaml_2.1.14 assertthat_0.1 digest_0.6.10
[13] tibble_1.2 shiny_0.14.2 htmlwidgets_0.8 mime_0.5 DEoptimR_1.0-8 jsonlite_1.1
[19] httpuv_1.3.3 foreign_0.8-67
>Thanks very much for any help.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/rstudio/leaflet/issues/423, or mute the thread
https://github.com/notifications/unsubscribe-auth/AAH6D1NAl7EdER5DFqtFBjjy3vvv5dSmks5rz1lMgaJpZM4NI-bW
.
Hey Cheng, thanks for getting back so promptly.
When I make that change, my html page will render but there seems to be no connection between the polygons on my leaflet map and the data table.
I have altered the code a little bit too in order to try and mirror the Gapminder example more closely.
The main functionality I am trying to implement is upon selecting a row from the datatable in Gapminder example, the polygon associated with that row is singled out and all others become transparent/greyed out.
As you can see by running this code below, the same effect does not happen.
I wonder is this something that is possible? (Hopefully I am making a silly mistake somewhere)
Is it maybe the case that I am running the code at the console and the example was created as flexdashboard Rmd output in RStudio?
library(leaflet)
library(crosstalk)
library(dplyr)
library(rgdal)
library(DT)
# install.packages('GWmodel')
# Just using this package for the dublin shapefile to use as an example
library(GWmodel)
data(DubVoter)
dub <- Dub.voter
# The Dub.voter data set has has no CRS
# Coordinates based on the Irish Grid / TM65 - epsg:29902
# epsg:4326 is the WGS84 World Grid that Leaflet uses. Need to re-project to this.
proj4string(dub) <- CRS("+init=epsg:29902")
newProj <- CRS("+init=epsg:4326")
dubTemp <- dub
dub <- spTransform(dubTemp, newProj)
# Create new column in dub called 'split' and give meaningless values
# This is to try and mirror Gapminder example more closely
dub@data$split <- paste("One")
dub@data$split[10:60] <- paste("Two")
dub@data$split[60:160] <- paste("Three")
# Create a colour palette
pal <- colorFactor(c("#1f77b4", "#ff7f0e", "#2ca02c"), domain = c("One", "Two", "Three"), ordered = FALSE)
sd <- SharedData$new(dub)
sd_df <- SharedData$new(dub@data, group = sd$groupName())
bscols(
leaflet(sd) %>%
addProviderTiles("CartoDB.Positron") %>%
addPolygons(data = dub, color = "#969696", weight = 1, fillColor = "#808080") %>%
addPolygons(color = "#969696", weight = 2, fillColor = ~pal(split), fillOpacity = 0.8),
datatable(sd_df, rownames = FALSE, extensions = 'Scroller',
options = list(scrollY = 200, scroller = TRUE, columnDefs = list(list(className = 'dt-left', targets = 0:3))))
)
Thanks a mill for your help.
This would be a very helpful addition to crosstalk.
Most helpful comment
Hey Cheng, thanks for getting back so promptly.
When I make that change, my html page will render but there seems to be no connection between the polygons on my leaflet map and the data table.
I have altered the code a little bit too in order to try and mirror the Gapminder example more closely.
The main functionality I am trying to implement is upon selecting a row from the datatable in Gapminder example, the polygon associated with that row is singled out and all others become transparent/greyed out.
As you can see by running this code below, the same effect does not happen.
I wonder is this something that is possible? (Hopefully I am making a silly mistake somewhere)
Is it maybe the case that I am running the code at the console and the example was created as flexdashboard Rmd output in RStudio?
Thanks a mill for your help.