R嵌入了perl中的代码,ggsave无法保存pdf文件

时间:2017-07-20 15:03:21

标签: r perl pdf

我有一个嵌入在perl中的R代码,想法是读取一个目录,将文件放入一个数组,然后遍历每个文件的数组,运行r rscript,基本上计算一些统计数据,通过write.table,并制作一些图,通过ggplot然后通过ggsave保存,一切正常,至少是perl部分,以及r脚本的第一部分,它计算统计数据并将它们写在正确的目录中然而,当谈到这些情节时,某种方式ggsave不起作用,我没有任何pdf文件。 原因是什么?在嵌入perl之前我在r studio上运行r脚本并且ggsave工作正常。 任何帮助将不胜感激,我附上以下代码:

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\")

1 个答案:

答案 0 :(得分:0)

调试存储为带有插值变量的字符串的程序非常困难。最好将R代码移动到自己的R文件create_plots.R中,然后直接传入变量。以下是两种可行的方法:

1 - 重写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 - 调用Rscript并从perl脚本传递参数

在create_plots.R

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

在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' : $!";