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 <- [email protected]
    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
  I have an problem related to ujson in python dixitaditya248 2 475 Apr-12-2021, 08:11 AM
Last Post: bowlofred
Photo Integration of apache spark and Kafka on eclipse pyspark aupres 1 718 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 1,280 Jan-10-2021, 11:39 PM
Last Post: Evalias123
  Super Urgent, work related. Gimp Python tsurubaso 5 743 Oct-19-2020, 08:18 AM
Last Post: tsurubaso
  Tableau Time Series Prediction using Python Integration tobimarsh43 0 611 Jul-24-2020, 10:38 AM
Last Post: tobimarsh43
  Beginner, my recursion returns None, and related problem zpacemanzpiff 2 562 Jul-02-2020, 04:25 AM
Last Post: zpacemanzpiff
  STATA/Python Integration jprender 0 601 May-03-2020, 09:38 PM
Last Post: jprender
  Integration of a complex function having singularities using quad amjad26 0 1,831 May-17-2019, 09:16 PM
Last Post: amjad26
  phython language java integration jammytcs123123 1 1,185 Jul-04-2018, 03:13 AM
Last Post: Skaperen
  Python version related Heptichector 2 1,585 Mar-12-2018, 08:52 AM
Last Post: Heptichector

Forum Jump:

User Panel Messages

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