Python Forum
R-PYTHON INTEGRATION RELATED PROBLEM
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
R-PYTHON INTEGRATION RELATED PROBLEM
#1
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:
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Active Directory integration dady 2 465 Oct-13-2023, 04:02 AM
Last Post: deanhystad
  Help with Integration Pandas excel - Python Gegemendes 5 1,728 Jun-05-2022, 09:46 PM
Last Post: Gegemendes
  Strange problem related to "python service" Pavel_47 1 1,383 Dec-07-2021, 12:52 PM
Last Post: Pavel_47
  I have an problem related to ujson in python dixitaditya248 2 2,603 Apr-12-2021, 08:11 AM
Last Post: bowlofred
Photo Integration of apache spark and Kafka on eclipse pyspark aupres 1 3,701 Feb-27-2021, 08:38 AM
Last Post: Serafim
  Although this is a talib related Q it's mostly related to python module installing.. Evalias123 4 5,585 Jan-10-2021, 11:39 PM
Last Post: Evalias123
  Super Urgent, work related. Gimp Python tsurubaso 5 2,878 Oct-19-2020, 08:18 AM
Last Post: tsurubaso
  Tableau Time Series Prediction using Python Integration tobimarsh43 0 1,888 Jul-24-2020, 10:38 AM
Last Post: tobimarsh43
  Beginner, my recursion returns None, and related problem zpacemanzpiff 2 1,741 Jul-02-2020, 04:25 AM
Last Post: zpacemanzpiff
  STATA/Python Integration jprender 0 1,804 May-03-2020, 09:38 PM
Last Post: jprender

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020