简体   繁体   English

R在Perl中嵌入代码,ggsave无法保存pdf文件

[英]R embedded code in perl, ggsave failing to save pdf file

I have a R code embedded in perl, the idea is to read a directory, put the files into an array, then loop through the array for each file, run the r rscript, that basically caluclates some stats, print them via write.table, and also make some plots, via ggplot and then save them via ggsave, everything works fine, at least the perl part, and the first part of the r script, it calculates the stats and write them correclty in the correct directory, however, when it comes to the plots, somehow ggsave doesn't work and i have no pdf file nowhere. 我在perl中嵌入了R代码,其想法是读取目录,将文件放入数组,然后为每个文件循环遍历该数组,运行r rscript,基本上计算出一些统计信息,并通过write.table打印它们,还可以通过ggplot进行绘制,然后通过ggsave保存它们,一切正常,至少是perl部分,以及r脚本的第一部分,它可以计算出统计信息并将它们正确地写入正确的目录中,但是,当涉及到情节时,ggsave不起作用,我也没有pdf文件。 what can the reason be? 原因是什么? Before embedding in perl i was running the r script on r studio and ggsave was working fine. 在嵌入perl之前,我在r studio上运行了r脚本,并且ggsave运行良好。 any help will be appreciated, i attach the code below: 任何帮助将不胜感激,我附上以下代码:

my $mapfile = shift;
my $OTUdir = shift;
opendir my $OpOTUdir, $OTUdir;
my @OTUtab = grep {/\.txt/} readdir $OpOTUdir;
my $OTUtab;

foreach $OTUtab (@OTUtab){
my @splitname = split (/\_/, $OTUtab);
my $primer = $splitname[0];
my $dirout = "Subplot_dir_OTU".$primer.".out";
my $dir = "./".$primer."_plots/";
my $SubPlotdir = "bsub -o $dirout  mkdir $dir";
my $run1 = system ('bash','-c',"$bsub && $SubPlotdir ") == 0
 or die "Can't create the directories per primer set";
sleep 1 until -e "$dirout";

my $Rcmd_pure_OTU = "      #here starts the r script
library (\'vegan\')
raw_data <- read.csv(\"".$OTUdir."$OTUtab\", row.names =1, sep =\"\t\",   dec=\".\", header =T, skip =1)
pre_OTU_tab <- raw_data[,-which(names(raw_data) == \"taxonomy\")]
OTU_tab <- t(pre_OTU_tab)
log_OTU <- log10(OTU_tab + 1)

map <- read.csv(\"$mapfile\", sep =\"\", row.names =1, header = T)
ordered_Map <- map[match(row.names(OTU_tab), row.names(map)),]
re_ordered_Map <- ordered_Map[complete.cases(ordered_Map),]
dist_bray <- vegdist(log_OTU, method= \"bray\", binary=FALSE)
dist_bray_binary <- vegdist(log_OTU, method=\"bray\", binary=TRUE)
cap_bray <- capscale(dist_bray ~ 1)
cap_bray_bin <- capscale(dist_bray_binary ~ 1)
cap_bray_year <- capscale(dist_bray ~ as.factor(re_ordered_Map\$Origin))
cap_bray_bin_year <- capscale(dist_bray_binary ~     as.factor(re_ordered_Map\$Origin))
CA1perc_log <- (cap_bray\$CA\$eig[1]/sum(cap_bray\$CA\$eig))*100
CA1perc_log <- round(CA1perc_log, digits = 1)
CA2perc_log <- (cap_bray\$CA\$eig[2]/sum(cap_bray\$CA\$eig))*100
CA2perc_log <- round(CA2perc_log, digits = 1)
CA1perc_log_bin <- (cap_bray_bin\$CA\$eig[1]/sum(cap_bray_bin\$CA\$eig))*100
CA1perc_log_bin <- round(CA1perc_log_bin, digits = 1)
CA2perc_log_bin <- (cap_bray_bin\$CA\$eig[2]/sum(cap_bray_bin\$CA\$eig))*100
CA2perc_log_bin <- round(CA2perc_log_bin, digits = 1)
CAperc_log_year <- round((sum(cap_bray_year\$CCA\$eig)/sum(cap_bray_year\$CA\$eig, cap_bray_year\$CCA\$eig))*100, digits = 1)
CAperc_log_bin_year <- round((sum(cap_bray_bin_year\$CCA\$eig)/sum(cap_bray_bin_year\$CA\$eig, cap_bray_bin_year\$CCA\$eig))*100, digits = 1)
#print the stats 
#Year
SigTest1 <- anova(cap_bray_year, by=\"term\", step=9999, perm.max=9999)
CapResults1 <- c(CAperc_log_year, SigTest1\$'Pr(>F)'[1])
Results1 <- rbind(CapResults1) 
colnames(Results1) <- c(\"Percent_Correlated\", \"pval\")
write.table(Results1,      file=\"".$dir."Year_pvalue_constraints_".$primer.".txt\", sep=\"\t\", quote=F, col.names=NA)
#binary year
SigTest2 <- anova(cap_bray_bin_year, by=\"term\", step=9999, perm.max=9999)
CapResults2 <- c(CAperc_log_bin_year, SigTest2\$'Pr(>F)'[1])
Results2 <- rbind(CapResults2) 
colnames(Results2) <- c(\"Percent_Correlated\", \"pval\")
write.table(Results2,    file=\"".$dir."Binary_year_p_value_constraints_".$primer.".txt\", sep=\"\t\", quote=F, col.names=NA)

data_for_plot <- cbind(cap_bray\$CA\$u,re_ordered_Map)
data_for_plot_bin <- cbind(cap_bray_bin\$CA\$u,re_ordered_Map)

data_for_plot_year <- cbind(cap_bray_year\$CA\$u,re_ordered_Map)
data_for_plot_bin_year <- cbind(cap_bray_bin_year\$CA\$u,re_ordered_Map)
cbbPalette <- c(\"#000000\", \"#E69F00\", \"#56B4E9\", \"#009E73\", \"#F0E442\", \"#0072B2\", \"#D55E00\", \"#CC79A7\")
ColorCount <- length(unique(re_ordered_Map\$Origin))
GetPalette <- colorRampPalette(cbbPalette, bias =3, interpolate = \"spline\", alpha = TRUE)
plot_unco <- ggplot(data_for_plot) + 
geom_point( size=4, aes(x=MDS1, y=MDS2, shape= inf_uni, color = Origin),  position = position_jitter(w = 0.1, h = 0.1))+
geom_vline(xintercept = 0, size = 0.3) +
geom_hline(yintercept = 0, size = 0.3) +
scale_color_manual(values = GetPalette(ColorCount)) +
theme (
panel.background = element_blank(),
legend.key = element_rect (fill = \"white\"),
legend.text = element_text (size = 15),
legend.title = element_text (size = 17, face = \"bold\"),
axis.text = element_text(size =17),
axis.line = element_line(color= \"black\", size =0.6),
axis.title = element_text(size =19, face = \"bold\")    
)
ggsave(\"".$dir."My_".$primer."_Uncostrained.pdf\", plot = plot_unco, width=12, height=12, units = \"in\")
plot_bin_unco <- ggplot(data_for_plot_bin) + 
geom_point( size=4, aes(x=MDS1, y=MDS2, shape= inf_uni, color = Origin),   position = position_jitter(w = 0.1, h = 0.1))+
geom_vline(xintercept = 0, size = 0.3) +
geom_hline(yintercept = 0, size = 0.3) +
scale_color_manual(values = GetPalette(ColorCount)) +
theme (
panel.background = element_blank(),
legend.key = element_rect (fill = \"white\"),
legend.text = element_text (size = 15),
legend.title = element_text (size = 17, face = \"bold\"),
axis.text = element_text(size =17),
axis.line = element_line(color= \"black\", size =0.6),
axis.title = element_text(size =19, face = \"bold\")    
)
ggsave(\"".$dir."My_".$primer."_Uncostrained_bin.pdf\", plot =plot_bin_unco, width=12, height=12, units = \"in\")

It is very difficult to debug a program that is stored as a string with interpolated variables. 调试使用内插变量存储为字符串的程序非常困难。 It would be better to move that R code into its own R file create_plots.R and then just pass in variables directly. 最好将R代码移到其自己的R文件create_plots.R ,然后直接传递变量。 Here are 2 approaches that would then work: 以下是两种可行的方法:

1 - Rewrite all of the file / directory operations in R 1-重写R中的所有文件/目录操作

inside create_plots.R 在create_plots.R内部

# Find files:
files <- list.files(pattern = "\\.txt$")

# Create a directory:
dir.create(file.path(mainDir, subDir))
setwd(file.path(mainDir, subDir))

# Other stuff ...

2 - Invoke Rscript and pass it parameters from your perl script 2-调用Rscript并从perl脚本中传递参数

inside create_plots.R 在create_plots.R内部

# Collect command line arguments
args  <- commandArgs( trailingOnly = TRUE )
foo   <- args[1]
bar   <- args[2]
baz   <- args[3]

inside create_plots.pl 在create_plots.pl中

# Invoke from perl script:
my $cmd = '/path/to/Rscript create_plots.R param1 param2 param3'
(system( $cmd ) == 0)
    or die "Unable to run '$cmd' : $!";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM