簡體   English   中英

使用bash腳本運行R命令

[英]Running R commands using a bash script

我有以下命令用於在R中繪制圖。主文本文件是cross_correlation.csv。

我怎樣才能把它放在bash腳本中,以便當我在終端上啟動它時,R命令將執行它們的工作並完成(就像所有其他shell腳本一樣)。

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腳本:

Rscript myscript.r

所以你可以將這一行放在一個bash腳本中:

#!/bin/bash

Rscript myscript1.r
Rscript myscript2.r
# other bash commands

這通常是在bash腳本中運行R腳本的最簡單方法。

如果要使腳本可執行,以便可以通過鍵入./myscript.r來運行它,則需要鍵入./myscript.r來查找Rscript的安裝位置:

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()

這個方法在這個問題中有解釋,也可能會給你一些想法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM