有誰知道是否可以派生R程序的文件名/文件路徑?我正在尋找與SAS中的「%sysfunc(GetOption(SYSIN))」類似的東西,它將返回SAS程序的文件路徑(以批處理模式運行)。我能做類似於R的任何事嗎?在R程序中使用文件名或文件路徑
到目前爲止我所能想出的最好的方法是在我使用的文本編輯器(PSPad)中使用快捷鍵添加文件名和當前目錄。有沒有更簡單的方法來做到這一點?
這裏是我的例子:
progname<-"Iris data listing"
# You must use either double-backslashes or forward slashes in pathnames
progdir<-"F:\\R Programming\\Word output\\"
# Set the working directory to the program location
setwd(progdir)
# Make the ReporteRs package available for creating Word output
library(ReporteRs)
# Load the "Iris" provided with R
data("iris")
options('ReporteRs-fontsize'=8, 'ReporteRs-default-font'='Arial')
# Initialize the Word output object
doc <- docx()
# Add a title
doc <- addTitle(doc,"A sample listing",level=1)
# Create a nicely formatted listing, style similar to Journal
listing<-vanilla.table(iris)
# Add the listing to the Word output
doc <- addFlexTable(doc, listing)
# Create the Word output file
writeDoc(doc, file = paste0(progdir,progname,".docx"))
這工作得相當好,無論是在批和RStudio。我真的很感謝一個更好的解決方案,雖然
這是關係到你在找什麼 。?[RSCRIPT:確定執行腳本的路徑(HTTP://計算器。 com/questions/1815606/rscript-determine-path-of-the-the-script) –
謝謝s的鏈接。這裏有很多信息,但到目前爲止,沒有任何解決方案似乎直接在RStudio或我的批處理命令文件中工作。我認爲如果R程序來自RStudio,那麼一些解決方案就可以工作,但這不是我正在尋找的。我可以調整我的批處理命令文件,使用「rterm --file =」而不是「rterm < source.r > output.r」,我還沒有嘗試過。 – ckx