1

I am creating a few csv's in python based on values in a list as below:

each_seg = ['seg1','seg2','seg3']
df_final.to_csv(str("""final_data_""" + """%s""" + """.csv""") %each_seg,index = False)

And there is a R program for feature selection as below:

df_seg = read.csv("final_data_seg1.csv") # Read in the csv created in python.

print(paste0("############ ",Sys.time()," Start of mRMR"," ##################"))

rmRMRe <- function(df_seg,noOfFeatures = 80){

  data <- df_seg

  remove(df_seg)
  # ---------------------------------------------------------------------------------------------------------------------------------------------
  # Convert to mRMR.Data type
  # ---------------------------------------------------------------------------------------------------------------------------------------------

  data <- data[, sapply(data, class) != "logical"]
  data <- data[, !(names(data)) %in% c("X")]
  data_slice <- data
  data_slice[] <- lapply(data, function(x) as.numeric(x))
  target_idx = which(names(data)=="status")
  dd <- mRMR.data(data = (data_slice))
  rm(list = c('data','data_slice'))


  # ---------------------------------------------------------------------------------------------------------------------------------------------
  # Convert to mRMR.Data type
  # ---------------------------------------------------------------------------------------------------------------------------------------------
  results <- mRMR.classic("mRMRe.Filter", data = dd, target_indices = target_idx,feature_count = 80)
  feature_indices <- solutions(results)
  feature_indices <- feature_indices[[1]][1:80]
  feature_seg <- data.frame('scores' = results@scores,'features' = dd@feature_names[feature_indices])

  return (feature_seg)
}

feature_scores = rmRMRe(df_seg,80)

Currently I first create all the csv's in python and then change the file name in R for each seg and run the feature selection method. However, I would like to call the R script from python with the file name as a parameter and generate the results for each seg. I run the R script using RScript in cmd but would like to automate this call to R from python. Below is sample of the data which is stored in the csv and called in R:

geo_CT  geo_LI  geo_NS
0       0       0
0       1       0
0       0       1
0       0       1
0       0       1
0       1       0   

Can someone please help me with this?

Since my question got marked as duplicate:

All the other answers mention passing commands whereas I need to pass filename as a parameter. I could not see a single answer which refers to the same issue. Not sure why it got marked as duplicate. I might be wrong.

I tried doing the below:

output_name = 'mrmr_R'
script_filename = 'mrmr_server.R'
param_filename = 'final_data_seg1.csv'# % input data csv name
result_filename = '%s_out.txt' % output_name 
with open(result_filename, 'wb') as result:
   process = subprocess.Popen(['Rscript', script_filename, param_filename],stdout=subprocess.PIPE)
        process.wait()

But this generates an empty file. Kindly help.

Shuvayan Das
  • 1,198
  • 3
  • 20
  • 40
  • You are going to want to use the `subprocess` module: https://docs.python.org/3/library/subprocess.html – divibisan May 24 '18 at 13:25
  • Possible duplicate of [Calling an external command in Python](https://stackoverflow.com/questions/89228/calling-an-external-command-in-python) – divibisan May 24 '18 at 13:25
  • Another alternative: [**rpy2**](https://rpy2.bitbucket.io/) – Jaap May 24 '18 at 15:46

0 Answers0