2017-01-10 72 views
0

有誰知道是否可以派生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。我真的很感謝一個更好的解決方案,雖然

+0

這是關係到你在找什麼 。?[RSCRIPT:確定執行腳本的路徑(HTTP://計算器。 com/questions/1815606/rscript-determine-path-of-the-the-script) –

+0

謝謝s的鏈接。這裏有很多信息,但到目前爲止,沒有任何解決方案似乎直接在RStudio或我的批處理命令文件中工作。我認爲如果R程序來自RStudio,那麼一些解決方案就可以工作,但這不是我正在尋找的。我可以調整我的批處理命令文件,使用「rterm --file =」而不是「rterm < source.r > output.r」,我還沒有嘗試過。 – ckx

回答

1

@Juan Bosco提供的鏈接Rscript: Determine path of the executing script包含我需要的大部分信息。它沒有解決的一個問題是在RStudio中運行R程序(在RStudio中進行採購已經被討論和解決)。我發現這個問題可以使用rstudioapi::getActiveDocumentContext()$path)來處理。

這也是值得注意的是,對於批處理模式的解決方案將不使用

Rterm.exe --no-restore --no-save < %1 > %1.out 2>&1 

的解決方案要求使用的--file=選項,例如工作

D:\R\R-3.3.2\bin\x64\Rterm.exe --no-restore --no-save --file="%~1.R" > "%~1.out" 2>&1 R_LIBS=D:/R/library 

以下是@aprstar發佈的get_script_path函數的新版本。這已被修改爲RStudio也工作(注意,它需要rstudioapi庫。

# Based on "get_script_path" function by aprstar, Aug 14 '15 at 18:46 
# https://stackoverflow.com/questions/1815606/rscript-determine-path-of-the-executing-script 
# That solution didn't work for programs executed directly in RStudio 
# Requires the rstudioapi package 
# Assumes programs executed in batch have used the "--file=" option 
GetProgramPath <- function() { 
    cmdArgs = commandArgs(trailingOnly = FALSE) 
    needle = "--file=" 
    match = grep(needle, cmdArgs) 
    if (cmdArgs[1] == "RStudio") { 
     # An interactive session in RStudio 
     # Requires rstudioapi::getActiveDocumentContext 
     return(normalizePath(rstudioapi::getActiveDocumentContext()$path)) 
    } 
    else if (length(match) > 0) { 
     # Batch mode using Rscript or rterm.exe with the "--file=" option 
     return(normalizePath(sub(needle, "", cmdArgs[match]))) 
    } 
    else { 
     ls_vars = ls(sys.frames()[[1]]) 
     if ("fileName" %in% ls_vars) { 
      # Source'd via RStudio 
      return(normalizePath(sys.frames()[[1]]$fileName)) 
     } 
     else { 
      # Source'd via R console 
      return(normalizePath(sys.frames()[[1]]$ofile)) 
     } 
    } 
} 

我把這個在我的.Rprofile文件。現在,我可以得到下面以分批模式或RStudio使用的文件信息。代碼我還沒有使用它,但source()應該工作太試圖

# "GetProgramPath()" returns the full path name of the file being executed 
progpath<-GetProgramPath() 
# Get the filename without the ".R" extension 
progname<-tools::file_path_sans_ext(basename(progpath)) 
# Get the file directory 
progdir<-dirname(progpath) 
# Set the working directory to the program location 
setwd(progdir)