Hello all,
I am having problem while doing r-python integration and I want to return a rscript where a function is written namely map_plot() which will return a map,I just want the output in python. As,I am using rpy2 package for r-python integration but the problem is that I cannot see the map in python windows.I want to know how to do it in python through rpy2 package.
Python Code:
Python Output:
any help would be highly appreciated.
Thanks,
Arnab Basak
I am having problem while doing r-python integration and I want to return a rscript where a function is written namely map_plot() which will return a map,I just want the output in python. As,I am using rpy2 package for r-python integration but the problem is that I cannot see the map in python windows.I want to know how to do it in python through rpy2 package.
Python Code:
from rpy2.robjects import globalenv import rpy2.robjects as robjects r_source = robjects.r['source'] r_source('D:\Dash_Arnab\R_app\shapefile_r_python.R') r_getname = robjects.globalenv['map_plot'] print(r_getname.r_repr())But after writing this code,it is printing the whole r-script not returning the map.
Python Output:
function () { setwd("D:/Dash_Arnab/R_app") library(dplyr) library(rgdal) library(rgeos) library(maptools) library(raster) library(leaflet) crop_df <- read.csv("crop_data.csv") crop_df$productivity <- crop_df$Production/crop_df$Area crop_df$Season <- as.character(crop_df$Season) trim.trailing <- function(x) sub("\\s+$", "", x) crop_df$Season <- trim.trailing(crop_df$Season) india_dis <- shapefile("gadm36_IND_2") crop_df_req <- crop_df[(crop_df$Crop_Year == 2013 & crop_df$Crop == "Rice" & crop_df$Season == "Kharif"), ] req_df_map <- crop_df_req[, c("State_Name", "District", "Production")] colnames(req_df_map)[1] <- "State" d1 <- india_dis@data india_dist <- data.frame(cbind(d1$NAME_1, d1$NAME_2, d1$GID_2)) colnames(india_dist) <- c("State", "District", "Code") india_dist1 <- india_dist new_data_map <- full_join(india_dist1, req_df_map, by = c(State = "State", District = "District")) new_data_map <- new_data_map[!is.na(new_data_map$Code), ] india_dist <- data.frame(cbind(d1$NAME_1, d1$NAME_2, d1$GID_2, new_data_map$Production)) colnames(india_dist) <- c("State", "District", "Code", "Production") map_data <- india_dist map_data$Production <- as.character(map_data$Production) map_data$Production <- as.numeric(map_data$Production) bins <- c(min(map_data$Production, na.rm = TRUE), quantile(map_data$Production, 0.25, na.rm = TRUE), median(map_data$Production, na.rm = TRUE), quantile(map_data$Production, 0.75, na.rm = TRUE), max(map_data$Production, na.rm = TRUE)) new_pal <- colorBin(palette = "YlGnBu", domain = map_data$Production, bins = bins) my_map <- leaflet(options = leafletOptions(zoomControl = FALSE)) %>% addProviderTiles("MapBox", options = providerTileOptions(id = "mapbox.dark", accessToken = Sys.getenv("MAPBOX_ACCESS_TOKEN"))) %>% addPolygons(color = "#444444", data = india_dis, weight = 2, fillColor = ~new_pal(map_data$Production), fillOpacity = 1, label = map_data$District, highlightOptions = highlightOptions(color = "red", weight = 3, bringToFront = TRUE), layerId = map_data$GID_2) %>% addLegend(pal = new_pal, values = round(map_data$Production, 0), opacity = 0.7, position = "bottomright") return(my_map) }But I want the map to be shown in python.
any help would be highly appreciated.
Thanks,
Arnab Basak