使用bash脚本运行R命令 [英] Running R commands using a bash script
问题描述
我有以下命令可用于在R中进行绘图。主要文本文件为cross_correlation.csv。
I have the commands below that I use to make plots in R. The main text file is cross_correlation.csv.
如何将其放入bash脚本中,以便在终端上启动它时,R命令将执行其工作并完成操作(就像所有其他shell脚本一样)。
How can I put it in bash script so that when I launch it on the terminal, the R commands will perform their jobs and finish (like all other shell scripts).
cross_correlation <- read.table(file.choose(), header=F, sep="\t")
barplot(cross_correlation$V3)
dev.copy(png,"cc.png",width=8,height=6,units="in",res=100)
dev.off()
hist(cross_correlation$V3, breaks=15, prob=T)
dev.copy(png,"hist_cc.png",width=8,height=6,units="in",res=100)
dev.off()
推荐答案
如果安装了R,则还应该安装程序 Rscript
,该程序可用于运行R脚本:
If you have R installed, you should also have the program Rscript
installed, which can be used to run R scripts:
Rscript myscript.r
因此您可以将此行放入bash脚本中:
So you can put this line in a bash script:
#!/bin/bash
Rscript myscript1.r
Rscript myscript2.r
# other bash commands
这通常是在bash脚本中运行R脚本的最简单方法。
This is usually the easiest way to run R scripts inside bash scripts.
如果要使脚本可执行,则可以通过键入来运行它./myscript.r
,您需要输入以下内容来找出 Rscript
的安装位置:
If you want to make the script executable so you can run it by typing ./myscript.r
, you need to find out where your Rscript
is installed by typing:
which Rscript
# /usr/bin/Rscript
然后您的 myscript.r
看起来像这样
#!/usr/bin/Rscript
cross_correlation <- read.table(file.choose(), header=F, sep="\t")
barplot(cross_correlation$V3)
dev.copy(png,"cc.png",width=8,height=6,units="in",res=100)
dev.off()
hist(cross_correlation$V3, breaks=15, prob=T)
dev.copy(png,"hist_cc.png",width=8,height=6,units="in",res=100)
dev.off()
此方法此问题,可能还会给您一些想法。
This method is explained in this question, which might also give you some ideas.
这篇关于使用bash脚本运行R命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!