Rich Shepard
2018-Oct-26 16:30 UTC
[R] source() fails in same directory as script: cannot find file
I'm not seeing my error even after reading ?source and a couple of web fora threads. Running R-3.5.1 on Slackware-14.2. The scripts/ directory has the file 'input-summarize.R' with these lines: sites <- read.table('../data/stations.dat', header=T, sep=',', stringsAsFactors=T) sink('../analyses/location-summaries.txt') print(summary(sites) sink(NULL) hg_conc <- read.table('../data/concentrations.dat', header=T, sep=',', stringsAsFactors=T) hg_conc$sampdate <- as.Date(as.character(hg_conc$sampdate)) sink('../analyses/concentration-summaries.txt') print(summary(hg_conc) sink(NULL) Sourcing the script within the same directory (confirmed using getwd()) fails with this error message:> source('input-summarize.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 'input-summarize.R': No such file or directory I've used this same syntax to source scripts within the past month and would like to understand this error so I can fix it now and avoid it in the future. TIA, Rich
Ista Zahn
2018-Oct-26 18:06 UTC
[R] source() fails in same directory as script: cannot find file
Hi Rich, I'm confused. It seems the error is that the file can't be found; if so, what does it matter what is in the file? As far as I can see you are either not in the directory you think you are, or b) the file is not named what you think it is. Best, Ista On Fri, Oct 26, 2018 at 12:30 PM Rich Shepard <rshepard at appl-ecosys.com> wrote:> > I'm not seeing my error even after reading ?source and a couple of web > fora threads. Running R-3.5.1 on Slackware-14.2. > > The scripts/ directory has the file 'input-summarize.R' with these lines: > > sites <- read.table('../data/stations.dat', header=T, sep=',', stringsAsFactors=T) > sink('../analyses/location-summaries.txt') > print(summary(sites) > sink(NULL) > > hg_conc <- read.table('../data/concentrations.dat', header=T, sep=',', stringsAsFactors=T) > hg_conc$sampdate <- as.Date(as.character(hg_conc$sampdate)) > sink('../analyses/concentration-summaries.txt') > print(summary(hg_conc) > sink(NULL) > > Sourcing the script within the same directory (confirmed using getwd()) > fails with this error message: > > > source('input-summarize.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 'input-summarize.R': No such file or directory > > I've used this same syntax to source scripts within the past month and > would like to understand this error so I can fix it now and avoid it in the > future. > > TIA, > > Rich > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Rich Shepard
2018-Oct-26 18:11 UTC
[R] source() fails in same directory as script: cannot find file
On Fri, 26 Oct 2018, Ista Zahn wrote:> I'm confused. It seems the error is that the file can't be found; if > so, what does it matter what is in the file?Ista, Beats me.> As far as I can see you are either not in the directory you think you > are, or b) the file is not named what you think it is.Yes, the error seems that R cannot find the file, but it's in the same directory and the file does exist:> getwd()[1] "/home/rshepard/documents/white-papers/geochemistry/willamette-river-mercury/scripts" ~/documents/white-papers/geochemistry/willamette-river-mercury/scripts]$ ls input-summerize.R input-summerize.R So, R is running in the scripts/ directory and the script is there, too. This is why I asked for help as the error makes no sense to me. Regards, Rich