我在python中根据列表中的值创建了一些csv,如下所示:
each_seg = ['seg1','seg2','seg3']
df_final.to_csv(str("""final_data_""" + """%s""" + """.csv""") %each_seg,index = False)
还有一个用于特征选择的R程序如下:
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)
目前我首先在python中创建所有csv,然后在每个seg中更改R中的文件名并运行特征选择方法。 但是,我想从python调用R脚本,文件名作为参数,并为每个seg生成结果。 我在cmd中使用RScript运行R脚本,但是想从python自动调用R。 下面是存储在csv中并在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
有人可以帮帮我吗?
由于我的问题被标记为重复:
所有其他答案都提到传递命令,而我需要传递filename作为参数。我看不到一个引用相同问题的答案。 不知道为什么它被标记为重复。我可能错了。
我尝试过以下操作:
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()
但这会生成一个空文件。 请帮助。