Chcę ustawić bieżącą lokalizację pliku jako katalog roboczy.Jak ustawić aktualną lokalizację pliku jako domyślny katalog roboczy w programowaniu R?
Korzystanie Rstudio (działa!):
# Author : Bhishan Poudel
# Program : writehere.r
# Source : Rscript writehere.r
# set working directory here
this.dir <- dirname(parent.frame(2)$ofile) # frame(3) also works.
setwd(this.dir)
# Sample data to test this code
mydata <- seq(1:10)
write.csv(mydata,"writehere.dat")
#This works flawlessly in MacOS 10.9 and Ubuntu 15.1.
pomocą polecenia z terminala: Rscript writehere.r (! Nie działa)
Error in dirname(parent.frame(2)$ofile) :
a character vector argument expected
Execution halted
------------------
(program exited with code: 1)
pomocą polecenia z terminala: Rscript writehere.r (Działa teraz!)
# Author : Bhishan Poudel
# Program : writehere.r
# Source : Rscript example.r
# set working directory here
this_dir <- function(directory)
setwd(file.path(getwd(), directory))
# Sample data to test this code
mydata <- seq(1:10)
write.csv(mydata,"writehere.dat")
Korzystanie z funkcji wewnątrz ~/.Rprofile dla Rstudio (działa!):,
##############################################
# inside ~/.Rprofile
# set up working directory
setwd_thisdir <- function() {
this.dir <- dirname(parent.frame(3)$ofile)
setwd(this.dir)
}
##############################################
Następnie w dowolnym katalogu powiedzmy mam writehere.r pliku, teraz działa.
# Author : Bhishan Poudel
# Program : writehere.r
# Compile : Rscript writehere.r
# set working directory here
setwd_thisdir
# Sample data to test this code
mydata <- seq(1:10)
write.csv(mydata,"writehere.dat")
Pytanie: Dlaczego funkcja
this.dir <- dirname(parent.frame(2)$ofile) # frame(3) also works.
setwd(this.dir)
nie działa dla edytorów tekstowych innych niż Rstudio?
kilka przydatnych linków są następujące:
R setting working directory to source file location?
R command for setting working directory to source file location
get filename and path of `source`d file
setwd() in the current working dir
Command for "Set working directory to source file location"
SublimeText and R: Setting Current File Directory
Setting working directory through a function
What is a fool-proof way of permanently setting R working directory?
R setting working directory to source file location?
How to get into the directory of a file in R?
co to jest "class (parent.frame (2) $ ofile)"? – MichaelChirico
@MichaelChirico Nie wiem, skorzystałem z sugestii z linku http://stackoverflow.com/questions/34843889/r-setting-working-to-source-file-location –
dodaj wiersz 'print (class (parent.frame (2) $ ofile)) 'i spróbuj uruchomić twoją funkcję.l – MichaelChirico