2017-01-10 5 views
0

Rプログラムのファイル名/ファイルパスを派生させることができるかどうか知りませんか?私は "%sysfunc(GetOption(SYSIN))" SASプログラム(バッチモードで実行されている)のファイルパスを返す、SASの何かに似たものを探しています。 Rで同様のことをすることはできますか?Rプログラムのファイル名またはファイルパスを使用する

これまでのところ、私が使用しているテキストエディタ(PSPad)にショートカットキーを使ってファイル名と現在のディレクトリを追加することができました。これを行う簡単な方法はありますか?これは、バッチにし、RStudioの両方で、かなりうまく機能

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

はここに私の例です。私は本当に良い解決に感謝します。

+0

あなたが 探しているものに関連して、この[Rscript:実行中のスクリプトのパスを決定します]です。?(のhttp://のstackoverflow。 com/questions/1815606/rscript-determined-the-executing-executing-script) –

+0

ありがとうsをリンクに使用します。そこには多くの情報がありますが、これまではRStudioやバッチコマンドファイルで直接動作するソリューションはありませんでした。私は、RプログラムがRStudioでソースされている場合、いくつかのソリューションがうまくいくと思いますが、それは私が探しているものではありません。私はバッチコマンドファイルを "rterm < source.r > output.r"の代わりに "rterm --file ="を使用するように調整することができましたが、まだ試していません。 – 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機能の新しいバージョンがあります。これは、rstudioapiライブラリを必要とすることに注意してください(もRStudioで動作するように変更されました。

# 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) 
関連する問題