source(..., chdir=TRUE) does not seem to change the directory - r

I'm pretty new to R and I'm trying to source a file which is again sourcing files. So I have a file, lets call it mother.R which contains a source call:
source ("grandmother.R")
mother.R and grandmother.R are in the same directory.
I would now like to source mother.R:
source ("C:/Users/whatever/R/mother.R", chdir=T)
My assumption was that the chdir=T would cause the source within the source to be looked for under C:/Users/whatever/R/, but it does not find grandmother.R when sourcing like this. Do I missunderstand chdir? Is there a way to do this without have to use absolute paths in mother.R?

Your understanding of how source works seems correct to me. But let's write an example so you can compare with your setup and maybe find where you went wrong.
Let the /Users/me/test/mother.R file contain the following:
print("I am the mother")
print(paste("The current dir is:", getwd()))
source("grandmother.R") # local path
and let the /Users/me/test/grandmother.R file contain the following:
print("I am the grandmother")
Where you start will help understand:
> getwd()
[1] "/Users/me"
This does not work:
> source("/Users/me/test/mother.R")
[1] "I am the mother"
[1] "The current dir is: /Users/me"
Error in file(filename, "r", encoding = encoding) :
cannot open the connection
In addition: Warning message:
In file(filename, "r", encoding = encoding) :
cannot open file 'grandmother.R': No such file or directory
because R is looking for grandmother.R in the getwd() dir...
Instead,
> source("/Users/me/test/mother.R", chdir = TRUE)
[1] "I am the mother"
[1] "The current dir is: /Users/me/test"
[1] "I am the grandmother"
works because source() temporarily changes the current directory to /Users/me/test/ where it can find grandmother.R.
When source gives you the handle back, you end up where you started, meaning the chdir is local to the source call like #CarlWitthoft pointed out.
> getwd()
[1] "/Users/me"

Related

Finding and setting the working directory within a super computer

Obviously one can find and set working directories with getwd() and setwd(). This question is a bit more complicated. I'm running two files on a super computer (one is a regular R file which calls the other file (a .stan file). The way I submit work to the super computer is that I have to zip a folder containing the data, the .R file, and the .stan file. I upload this folder, and I pull this folder by setting it as one of the parameters in the super computer. I call the data using the standard read.csv() command and everything is hunky dory.
However, when I call the .stan file from the .R file, it can't access it because it needs to know the working directory.
This is the error I get:
> fit <- stan(file="ace_thresholds.stan", data=stanData, cores = 4)
Error in file(fname, "rt") : cannot open the connection
In addition: Warning messages:
1: In normalizePath(file) :
path[1]="ace_thresholds.stan": No such file or directory
2: In file(fname, "rt") :
cannot open file 'ace_thresholds.stan': No such file or directory
Error in get_model_strcode(file, model_code) :
cannot open model file "ace_thresholds.stan"
Calls: stan -> stan_model -> stanc -> get_model_strcode
Execution halted
When I tried setting the working directory to the unzipped NSG_stan folder (which is what I assumed the wd to be, I received this error:
fit <- stan(file="NSG_stan/ace_thresholds.stan", data=stanData, cores = 4)
Error in file(fname, "rt") : cannot open the connection
In addition: Warning messages:
1: In normalizePath(file) :
path[1]="NSG_stan/ace_thresholds.stan": No such file or directory
2: In file(fname, "rt") :
cannot open file 'NSG_stan/ace_thresholds.stan': No such file or directory
Error in get_model_strcode(file, model_code) :
cannot open model file "NSG_stan/ace_thresholds.stan"
Calls: stan -> stan_model -> stanc -> get_model_strcode
Execution halted
So I tried running print(getwd()) within the script and in the printout I see that the wd is
"/projects/ps-nsg/home/nsguser/ngbw/workspace/NGBW-JOB-RTOOL_TG-EBE9CDBF28BF42AF8CB6EC9355006B3E/NSG_stan"
which means that the working directory will shift with every job. So to accurately set the working directory, I'd need to set it to the current folder within the script. I looked for various posts on how to do this like the following
# install.packages("rstudioapi") # run this if it's your first time using it to install
library(rstudioapi) # load it
# the following line is for getting the path of your current open file
current_path <- getActiveDocumentContext()$path
# The next line set the working directory to the relevant one:
setwd(dirname(current_path ))
# you can make sure you are in the right directory
print( getwd() )
The issue with this is that it's a super computer, so it's difficult to install packages, because every time I want to install something, I have to email the folks associated with the super comp and that all takes time.
I've looked over this thread as well: R command for setting working directory to source file location in Rstudio. Appears to be a lot of dissent over what works. I tried a couple of them, and they didn't work.
setwd(getSrcDirectory()[1])
this.dir <- dirname(parent.frame(2)$ofile)
setwd(this.dir)
I've included the .R file below, in the event that it helps, but I think this is probably a pretty easy answer for someone with a decent amount of coding experience.
.R file (so it's the "ace_thresholds.stan" file that I either need to link to the current wd or to include the code that would set the wd, such that this "ace_thresholds.stan" call would work. Does that make sense?
Thanks much!
dat <- ace.threshold.t2.samp
dat <- subset(dat, !is.na(rw))
dat$condition <- factor(dat$condition)
dat$pid <- factor(dat$pid)
nTotal <- dim(dat)[1]
nCond <- length(unique(dat$condition))
nSubj <- length(unique(dat$pid))
intensity <- dat$rw
condition <- as.numeric(dat$condition)
pid <- as.numeric(dat$pid)
correct <- dat$correct_button == "correct"
chancePerformance <- 1/2
stanData <- list(nTotal=nTotal, nLevels=nCond, nSubj = nSubj, subject = pid, intensity=intensity, level=condition, correct=correct, chancePerformance=chancePerformance)
fit.rw <- stan(file="ace_thresholds.stan", data=stanData, cores = 4, control=list(max_treedepth=15, adapt_delta=0.90))

Issue when creating zip file from directory

I have trouble with creating zip file from R. The same code worked perfectly at work with R version 3.4.2, 32 bit computer.
Now I am trying to run the same thing on R version 3.5.1, 64 bit computer, and the zip() command does not seem to work. What is going on?
zip(zipfile = "test.zip",files=list.files(getwd()))
#create zip from whole directory, on 1st machine it works, now nothing happens
I checked the source code for zip() and when I debug it, I found out that system2 command does nothing.
zip <- function (zipfile, files, flags = "-r9X", extras = "", zip = Sys.getenv("R_ZIPCMD",
"zip"))
{
if (missing(flags) && (!is.character(files) || !length(files)))
stop("'files' must a character vector specifying one or more filepaths")
args <- c(flags, shQuote(path.expand(zipfile)), shQuote(files),
extras)
if (.Platform$OS.type == "windows")
invisible(system2(zip, args))
else invisible(system2(zip, args))
}
# I run this manually when trying to debug, nothing happens;
system2(zip, args) ## zip is a parameter here, not a function
####
Browse[2]> zip
[1] "zip"
Browse[2]> args
[1] "-r9X" "\"bla.zip\""
[3] "\"[Content_Types].xml\"" "\"_rels\""
[5] "\"docProps\"" "\"xl\""
[7] ""
For example absurd call does not give an error.
system2("blablađ",2) ## does nothing but no error or warning either
I am stuck trying to understand how does system2() function works and what do I need to change to create a compressed folder.
Thanks
EDIT: After taking the account the help from comment, I got following error:
Browse[2]> system2(zip, args,stderr = T)
Error in system2(zip, args, stderr = T) : '"zip"' not found
SOLVED: After installing Rtools for version 3.5 it worked.
From the zip help:
zip(zipfile, files, flags = "-r9X", extras = "",
zip = Sys.getenv("R_ZIPCMD", "zip"))
zip A character string specifying the external command to be used.
As you can see, the zip function has an argument zip to specify the external command to be used. On my machine it is:
λ where zip
C:\Oracle\Ora11\BIN\zip.exe
C:\Program Files\Rtools\bin\zip.exe
The zip program is available in Rtools, but it is also available on any (Windows?) machine, usually.
To check whether zip is found by R, type:
> Sys.which("zip")
zip
"C:\\Oracle\\Ora11\\bin\\zip.exe"
If you get "", that means zip is not in the path, and if it is neither in the environment variable R_ZIPCMD, you have to specify its path in the zip argument.

R Script .bat file - Adding Source before executing

I have an 'R' file which inserts reads data from a database, does some calculations, then re-inserts data back into a table.
Before i execute the script, I run 'Source' as below..
I want to use Windows Task Scheduler to auto schedule this script to run. I am following the guide https://trinkerrstuff.wordpress.com/2015/02/11/scheduling-r-tasks-via-windows-task-scheduler/ - When creating the .BAT file it should look something like:
echo off
CMD BATCH C:\PATHNAME\RSCRIPT.R
What should i insert here to make sure it runs the 'Source' first?
In the R code i have
In the code i have:
#use a relative path to locate our common utilities file and source it
source("..//R-Utilities//Utilities.R")
# use check_install_package function from Utilities.R to install and load
packages
check_install_package("lubridate")
check_install_package("plyr")
check_install_package("dplyr")
check_install_package("dtplyr")
check_install_package("ISOweek")
check_install_package("stringi")
check_install_package("RODBC")
#give us access to the library of functions this script uses
source("CTB_functions.R")
But I need to click the source button before running my whole code, or i get an error (As below).
> #this automatically sets the working directory to be where this file is
> setwd(getSrcDirectory(function(x) {x}))
Error in setwd(getSrcDirectory(function(x) { :
cannot change working directory
>
> #use a relative path to locate our common utilities file and source it
> source("../R-Utilities/Utilities.R")
Error in file(filename, "r", encoding = encoding) :
cannot open the connection
In addition: Warning message:
In file(filename, "r", encoding = encoding) :
cannot open file '../R-Utilities/Utilities.R': No such file or directory
>
> # use check_install_package function from Utilities.R to install and load
packages
> check_install_package("lubridate")
Error: could not find function "check_install_package"
> check_install_package("plyr")
Error: could not find function "check_install_package"
> check_install_package("dplyr")
Error: could not find function "check_install_package"
> check_install_package("dtplyr")
Error: could not find function "check_install_package"
> check_install_package("ISOweek")
Error: could not find function "check_install_package"
> check_install_package("stringi")
Error: could not find function "check_install_package"
> check_install_package("RODBC")
Error: could not find function "check_install_package"
>
> #give us access to the library of functions this script uses
> source("CTB_functions.R")
Error in file(filename, "r", encoding = encoding) :
cannot open the connection
In addition: Warning message:
In file(filename, "r", encoding = encoding) :
cannot open file 'CTB_functions.R': No such file or directory
Given your script, there should be no need to click the “Source” button first: in fact, it would execute your script twice.
A few things about your scripts:
CMD BATCH C:\PATHNAME\RSCRIPT.R
This is missing R in front of CMD BATCH. However, You should probably use Rscript.exe instead of R CMD BATCH. It’s a modern replacement.
source("..//R-Utilities//Utilities.R")
There’s no need for double slashes: single slashes work.
source("../R-Utilities/Utilities.R")
More fundamentally, using source in this manner can quickly become complex and error-prone due to several shortcomings (e.g circular inclusion, relative paths etc). A better way of achieving this is via the ‘box’ package, which provides a vastly improved replacement for the source function, which seems to fit your use-case very closely.
In particular, your script will probably not work: source won’t find the files R-Utilities/Utilities.R and CTB_functions.R because it searches these relative to the current working directory, not relative to the script directory. Using ‘box’ fixes this.

Issue in opening a txt file in a specific directory in R, Ubuntu 14.04 LTS, R version 3.2.4 Revised

I got a very strange problem when trying to read via read.table a txt file in a folder. It recognizes the existence of the file (I debugged via print method) but it is not able to read the file into a table.
Anybody has an idea of what is going wrong?
I already looked at the other related topics but I didn't find any answer which fits my problem.
Here is my code:
path1 = "/home/yoda/Desktop/thesis/TullyFisher/Galac.RC_Dwarfs/TFRCHI/bins_29_04/7bins_TF/datasets/TFR/"
out.file<-""
file.names1 <- dir(path1, pattern =".txt")
listofdfs<-list()
for(i in 1:length(file.names1))
{
print(file.names1[i])
file <- read.table(file.names1[i])
df<-data.frame(as.numeric(file[[1]]),as.numeric(file[[2]]),as.numeric(file[[3]]),as.numeric(file[[4]]))
listofdfs[[i]]<-df
#write.table(listofdfs[[i]],file=paste0("outliers_",file.names1[i],quote=F,row.names = F, col.names = F))
}
It returns :
[1] "toplot1_normalTF.txt"
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
cannot open file 'toplot1_normalTF.txt': No such file or directory
It must be the file path. That directory is not the working directory and dir() is only returning the filenames and not the full path . Using full.names argument should solve this. For example
dir("~/Desktop", full.names = T)
Error is because the files you are trying to read are not in current directory.
R always try to read file from current directory.
To know your current directory try :
getwd()
it is different from the path1 for you.
So as menioned above by #R.S. please use full.names. Try below:
path1 = "/home/yoda/Desktop/thesis/TullyFisher/Galac.RC_Dwarfs/TFRCHI/bins_29_04/7bins_TF/datasets/TFR/"
out.file<-""
file.names1 <- dir(path1, pattern =".txt",full.names = T)

error loading csv file for R

when i loading csv file for R, i can see the error
but i don't know why this happening
i wrote following code:
setwd("C:\\Users\\규남\\Desktop\\twitter")
library(KoNLP)
useSejongDic()
txt <- readLines(file("test.csv"))
and, this error appear
txt <- readLines(file("test.csv"))
Error in readLines(file("test.csv")) : cannot open the connection
In addition: Warning message:
In readLines(file("test.csv")) :
cannot open file 'test.csv': No such file or directory
why this happening?
file directory is not wrong, and that file in the folder
[enter image description here][1]
please see this
i restart Rstudio, even notebook power
but error appear again
how to i load that csv file?
and why this happening?
here is result useing getwd() function
[1] "C:/Users/규남/Desktop/twitter"
Warning message:
closing unused connection 3 (test.csv)
[1]: http://i.stack.imgur.com/xkFkt.png
When working through these problems I like to use the file.path() function. Look at the documentation, but it makes certain that the separator characters that are used in the string are what R is expecting.
Try:
path <- file.path("C:", "Users", "규남", "Desktop", "twitter")
setwd(path)
library(KoNLP)
useSejongDic()
txt <- readLines(file("test.csv"))

Resources