问题描述
我有以下命令,用于在 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=" ")
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
将如下所示
Then your myscript.r
will look like this
#!/usr/bin/Rscript
cross_correlation <- read.table(file.choose(), header=F, sep=" ")
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 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,WP2