There are two files c:/a/b/mycode.r and c:/a/myfunction.r on my drive. When I run mycode.r, how to read the myfunction.r by using the relative path with source()? I tried source("../myfunction.r"), but it says No such file or directory. How to solve this problem?
Try to set chdir=TRUE.
This will change the working directory to the directory of the file being sourced.
`source("../myfunction.r", chdir=TRUE)`
Related
I am trying to use the load() function to load an .RData file into my current R session. I open my code base by double clicking on the R project file in my directory and then attempt to load the .RData file by running load('./Data/mydata.rdata'). However, this returns a 'No such file or directory' error. I verified that the working directory is correct by using getwd().
I figure I must be using incorrect syntax because I have no issues loading the file when I type in the full file path.
Working directory for R Project file: "/Users/Me/Library/OneDrive/RStuff"
Directory containing .RData file: "/Users/Me/Library/OneDrive/RStuff/Data"
Code that works: load("/Users/Me/Library/OneDrive/RStuff/Data/mydata.rdata")
Code that fails: load('./Data/mydata.rdata')
Do relative paths not work with load() or is my syntax wrong?
Do relative paths not work with load() or is my syntax wrong?
It looks like there is an error with your paths. You said the working directory is "/Users/Me/Library/OneDrive/RStuff"
But according to the code that works:
load("/Users/Me/Library/OneDrive/RStuff/Data/mydata.rdata")
...the data file is in "/Users/Me/Library/OneDrive/RStuff/Data"
Therefore to use relative paths, you would use:
load("./Data/mydata.rdata")
I am using Choose File keyword to upload file. In documentation is written that I can use ${CURDIR} to set the path to my file, but it means that this file has to be in the same directory ${CURDIR}/filename.txt. But how to set the path to the file that exists in another directory?
Use ${EXECDIR} so you go back to the root and go from there.
I have a "copy deployed" installation of R with no R-specific environment variables set.
I want to source a file from within Rprofile.site:
source("how/to/find/this/file/my_settings.R")
Where could I place my my_settings.R file so that it can be found from within Rprofile.site no matter in which path Rprofile.site is installed?
BTW: I want to avoid an absolute path to my_settings.R this would work indeed. I'd prefer to use the same folder as Rprofile.site or a path relative to it to support copy deployment of R.
Edit 1: The problem is that getwd is always different depending on the current folder from which you start R
If you can't use absolute paths and that your working directory is not stable, one way is to use .libPaths or .Library.
By default your Rprofile should be in directory paste0(.Library,"/../etc/") or paste0(.libPaths()[2],"/etc/") so you can put your file there and source it with :
source(paste0(.Library,"/../etc/my_settings.R"))
source(paste0(.libPaths()[2],"/etc/my_settings.R"))
As far as I understand the first option is stable (I don't think one can change the value of .Library).
If you use the second option just make sure that if in the future you alter your .libPaths() you do it after sourcing your file.
See ?.libPaths for more info on default folders.
Log file is created if an absolute path is provided in the log4j.properties in a runnable jar created using eclipse. However, if relative file path is given , file is not created
log4j.appender.appLogger=org.apache.log4j.RollingFileAppender
log4j.appender.appLogger.File=.//logs//MyApp.log
Please correct this in your log4j.properties.
log4j.appender.appLogger.File=/logs/MyApp.log
For example:
In Ubuntu(Linux).
/home/bizruntime/Desktop/logs/MyApp.log
In Windows.
C:\\Desktop\logs\MyApp.log
If i have my log file in some folder of Desktop i need to specific the exact path.
I am currently working on a joint project, with all the files stored in a Dropbox. To avoid changing the working directory every time we change something, we use the following code:
this.dir = dirname(parent.frame(2)$ofile)
setwd(this.dir)
This is working fine when sourcing the code in Rstudio, but we are looking for a solution that is working outside RStudio.
The team is working on Mac and Windows.
For Windows, locate the file "Rprofile.site". In there, there will be set the following command setwd("PATH_TO_WD")
The same goes for Mac, the file is usually located at /etc/R/ path.
Just change the setwd in that file and everytime you launch R, the working directory will be set to the directory you want.
EDIT: In my Windows computer, the file is located at "C:\Program Files\R\R-3.2.3\etc