why does `source` change the working directory? [closed] - r

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
Working with R 3.4.2. The problem is that using source with a relative path changes the working directory. Here's a minimal example:
The file tree: the project folder has two subfolders src1 and src2. My working directory is src1 but I'd like to use a function located in src2.
setwd("project/src1") #sets working directory where I want it
getwd() #prints "project"
source("../src2/myFunction.r") #loads the file with function I want
getwd() #prints "project" not "project/src1"
This only happens when navigating upwards. If I start with "project" as the working directory and do source("src/src2/myFunction.r") it doesn't change the working directory at all.
Is this a bug? If I specified a relative path it should be pretty clear that I do not want to change the working directory. Am I missing something?

Related

Is this R file path incorrect? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 days ago.
Improve this question
I'm trying to write a simple file path to my downloads folder on my macbook but I'm getting this error:
filePath <- file.path(Sys.getenv("HOME"), "Downloads")
transactionData <- fread(paste0(filePath,"QVI_transaction_data.csv"))
customerData <- fread(paste0(filePath,"QVI_purchase_behaviour.csv"))
Error in fread(paste0(filePath, "QVI_transaction_data.csv")) :
File '/Users/anitaabbot/DownloadsQVI_transaction_data.csv' does not exist or is non-readable. getwd()=='/Users/anitaabbot/Downloads'
Both files "QVI_transaction_data.csv" and "QVI_purchase_behaviour.csv" are saved in the download folder and so is the .rmd file. So I'm not sure what the problem is. Any ideas?

Reading arrays from binary file: "read end of file" error [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am reading/writing an array to file in Julia. For example:
test_arr=zeros(3,3)
io = open("logs/test","w")
write(io,test_arr)
close(io)
To read the file I do:
load_arr=Array{Float64}(undef,3,3)
io = open("logs/test","w")
read!(io,load_arr)
close(io)
This gives a "read end of file" error.
I am a bit perplexed as this is how I have been writing/reading other arrays to/from binary files without issue. But this time I get an error.
When opening the IOStream again, I think we want a read instead of another write:
io = open("logs/test","r")
For completeness from the comments above, opening a file with just w will truncate the file, so attempting to read from it will lead to an EOF error. More examples of what different modes do can be found here: https://docs.julialang.org/en/v1/base/io-network/#Base.open

R function can't be found [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I have an R script where I use the following code:
gspcRets = diff( log( Cl( qxts ) ) )
When i run the script from RStudio everything works fine.However,when i run the script from Terminal with the command:
RScript my_script.R
I get the following error:
Error in Cl(qxts) : could not find function "Cl"
My script loads all the necessary libraries.I assume it has something to do with the fact that my system needs some kind of access to the RStudio environment.Any ideas?
I forgot to load the "quantmod" package.An RStudio's active session continues to use the packages that have been loaded previously even after i used the command:
rm(list=ls(all=TRUE))

calling a function from another script [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I want to use a function from one script in another script but i either get an erorr or nothing is happend, depend on my code syntax. When i use the source("file_name) i get an erorr, and when i add the if(exists("function_name", mode = "function")) i get nothing..
hope you can help
have a good day
Or
In a different way you can choose your code script file using file.choose (avoiding problems related to the working directory) like this:
source(file.choose())
Terru_theTerror is absolutely right: it looks like there is something wrong with your source folder.
You may check the current name of your working directory with getwd() and check what contains this directory by dir(). If there your source file is placed elsewhere, your should change your current directory or to include the path to your source file by using source():
source_dir_name <- "D:/Work/Sources"
source_file_name <- "file_Name.R"
source_with_path <- paste(source_dir_name,"/", source_file_name, sep = "")
#
setwd(source_dir_name)
source(source_file_name)
# or
source(source_with_path)

Delete PDF file made in R [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have created a couple of pdf's using the pdf() device and I can't seem to delete them from my computer. I received the following error when I ran my code.
> pdf(file="Appendix_B_1.pdf")
> plot(1:10)
> dev.off
function (which = dev.cur())
{
if (which == 1)
stop("cannot shut down device 1 (the null device)")
.External(C_devoff, as.integer(which))
dev.cur()
}
<bytecode: 0x0000000008c7ecd0>
<environment: namespace:grDevices>
Above is my attempt to trick the computer into thinking it is a new file. It changed the file and I am able to open it. I just can't delete it.
I don't know what this means and all I want to do is delete the files. Is there some workaround for either turning off the device or deleting the files from my hard drive?
dev.off is the name of a function. When you type the name of a function, the source code for that function is printed. If you want to run the function, add parenthesis: dev.off()

Resources