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

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)

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

Error while trying to write a csv file

I'm trying to write a csv file in RStudio on a MacOS, but I'm getting this error:
Error: Failed to open '/Users/some.one/Documents/data'
and this is my code:
write_csv(dat, path_dat, na = "NA", append = FALSE, col_names = TRUE)
can someone maybe tell me what might cause such an error?
Edit:
dat ........... is a data frame
path_dat ...... is the path in the error : /Users/some.one/Documents/data
I would check that you have access to this directory. You can do via getwd()
If you don't, you can change the directory you're writing to via setwd(path_to_directory) and then run your write.csv function.

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

Cannot read data from an xlsx file in RStudio

I have installed the required packages - gdata and ggplot2 and I have installed perl.
library(gdata)
library(ggplot2)
# Read the data from the excel spreadsheet
df = data.frame(read.xls ("AssignmentData.xlsx", sheet = "Data", header = TRUE, perl = "C:\\Strawberry\\perl\\bin\\perl.exe"))
However when I run this I get the following error:
Error in xls2sep(xls, sheet, verbose = verbose, ..., method = method, :
Intermediate file 'C:\Users\CLAIRE~1\AppData\Local\Temp\RtmpE3UYWA\file8983d8e1efc.csv' missing!
In addition: Warning message:
running command '"C:\STRAWB~1\perl\bin\perl.exe" "C:/Users/Claire1992/Documents/R/win-library/3.1/gdata/perl/xls2csv.pl" "AssignmentData.xlsx" "C:\Users\CLAIRE~1\AppData\Local\Temp\RtmpE3UYWA\file8983d8e1efc.csv" "Data"' had status 2
Error in file.exists(tfn) : invalid 'file' argument
Thanks to #Stibu I realised I had to set my work directory. This is the command you use to run in Rstudio; setwd("C/Documents..."). The file path is where the excel file is located.
I had the issue but I solved it differently.
My problem was because my file was saved as Excel (extension .xls) but it was a txt file.
I corrected the file and I did not meet any other error with the R function.

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

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"

Resources