I can see the table of the data set on the upper right of Data.
My code is currently:
file.exists(C:/Hollywood.xlsx)?
to see if RStudio is picking up on it.
I get an error message of
"Incomplete expression: file.exists(C:Hollywood.xlsx)?"
To take a look at the working directory, I'm doing Session>Set Working Directory>Source File Location and get "The currently active source file is not saved so doesn't have a directory to change into." If I instead try Session>Set Working Directory>To Files Pane Location, I get setwd(~) and the following is what I get and subsequent code I try:
setwd("/Users/FridasSlave/Downloads/Hollywood")
#Error in setwd("/Users/FridasSlave/Downloads/Hollywood") :
# cannot change working directory
getwd(Hollywood)
# Error in getwd(Hollywood) : unused argument (Hollywood)
getwd("Hollywood")
#Error in getwd("Hollywood") : unused argument ("Hollywood")
If I try Session>Set Working Directory>Choose Directory, I can get to my Downloads folder but the individual files are all not there.
What am I doing wrong?
(And yes, I'm very new to R.)
I have all my relevant paths and code above.
Scrolling down the list of problems:
I hope you didn't put a ? at the end of an R command. It's true that it looks like it would be a question if you were talking to a person, but that's not the case.
The argument to file.exists should be a character value. C:/Hollywood.xlsx is not recognized by the R interpreter as a character value because there are no quotes around it. It's possible that at that point you could have gotten success with
file.exists("Hollywood.xlsx") # if it is in the current working directory
From your use of "~" and setwd("/Users/FridasSlave/Downloads/Hollywood") I'm guessing that you are on a Mac. Your question should probably have included that as a specific note. (Which further raises the issue why you would use a Windows file spcification if you are on a Mac?
The reason for the error from setwd("/Users/FridasSlave/Downloads/Hollywood") is not obvious, but maybe you misspelled part of the path or maybe it should have been setwd("/Users/FridasSlave/Downloads/").
Trying getwd(Hollywood) you again failed to include quotes around the argument. And why would you even want an argument if you want information about the current working directory? Should have just been getwd()
Related
I am trying to fix an issue in an R project (that I'm not too familiar with). The test script that is executed when running "Test Package" in R-Studio uses a variable, let's call it x. From the result of the test I can tell that the data assigned to this variable is outdated and I want to update it.
Problem is, I just cannot figure out where this variable is actually defined. I have used grep for the entire source code of the package, and I only find the instance from the test script but no declaration. It is not defined in the script itself nor anywhere else in the source code.
When I just load the package, the variable is not defined. Somehow it is defined however when running the test, because only when I change the name in the test script into some dummy I get the error that it isn't defined.
Is there a specific place where I could look, or may be a simple trick how I could figure out where and how the variable is defined?
Thanks
Edit: the actual variable name is a bit complicated, it is not x
The find in files option in RStudio may help.
You can search through multiple files within a folder.
If you get too many matches to sort through (I'm really hoping your variable is not actually called x!), you can try using a regular expression.
enter image description here
Follow the pictures and you could solve the problem.
I have tried to rerun some programs which worked perfectly well before, and most of them use the same call as below but now none of them work.
It could be a latent problem with the code come to life or it is something within my overall environment that has changed.
I have tried reloading the complete directory from archive from 2 months ago when I was using the programmes and they do not work now.
After reading the previous answers I have tried
setwd("Documents/Paper1/ThirdDraft/DTW_DATA")`
to make data and program folders peers but R will not let me change it.
I am an occasional user of R and not conversant with the environment so some hints / advice on a possible approach would be very helpful.
Thank you
The data files are at Documents/Paper1/ThirdDraft/DTW_DATA/Binned_Base_Data
working directory at /Users/briank/Documents/Paper1/ThirdDraft/DTW_DATA/DTW_R_Programmes`
#
# Import Data
#
chan11Data <- read.csv("Documents/Paper1/ThirdDraft/DTW_DATA/Binned_Base_Data/Channel_11.csv",
+ header = TRUE, fill = TRUE)
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
cannot open file 'Documents/Paper1/ThirdDraft/DTW_DATA/Binned_Base_Data/Channel_11.csv': No such file or directory
If your working directory is
/Users/briank/Documents/Paper1/ThirdDraft/DTW_DATA/DTW_R_Programmes
Then R won't find this file
"Documents/Paper1/ThirdDraft/DTW_DATA/Binned_Base_Data/Channel_11.csv"
but it should be able to find
"/Users/briank/Documents/Paper1/ThirdDraft/DTW_DATA/Binned_Base_Data/Channel_11.csv"
or
"../Binned_Base_Data/Channel_11.csv"
You are mixing up absolute and relative file paths, and that as a consequence, you are implementing bad coding practices that result in the kind of trouble that you are facing right now.
To be clear,
/Users/briank/Documents/Paper1/ThirdDraft/DTW_DATA/
is an absolute file path that starts at the root of your disk, whereas
Documents/Paper1/ThirdDraft/DTW_DATA
is a relative file path that will start in your working directory.
Your current predicament
Since everything that you are doing happens in the following directory, that directory should be your working directory:
/Users/briank/Documents/Paper1/ThirdDraft/DTW_DATA/
If you are using RStudio, which I recommend you do, then I recommend that you create a Project in that directory. Opening the Rroject (e.g. DTW_DATA.Rproj) will automatically set the working directory to the path above (i.e. what you did with setwd).
Now, within that directory, you seem to have two directories:
DTW_R_Programmes – which is where I suppose your R scripts are located
Binned_Base_Data – which is where your data, e.g. Channel_11.csv, are located
A possible fix
If you opened your R script by double-clicking it, it is likely that your working directory was set to DTW_R_Programmes -- in which case you need to "go back" one level to find your data, as in
../Binned_Base_Data/Channel_11.csv
Another possible fix
Instead of using ../ as above, the solution I would like to recommend is to move all your R scripts to the root directory of your project, i.e. DTW_DATA. That should limit confusion in the future, and make your project more manageable.
What you want to have is
DTW_DATA/
1_a_script.r
2_another_script.r
etc.
Binned_Base_Data/
Channel_11.csv
Channel_12.csv
etc.
Then, in your script(s), just indicate
d <- read.csv("Binned_Base_Data/Channel_11.csv")
… and enjoy the sheer simplicity of it.
If the first line of your #rstats script is "setwd(..." I will come into your lab and SET YOUR COMPUTER ON FIRE. -- Twitter
I am running R in batch mode using Rwui to display visual contents.
In the last section of this document:
http://sysbio.mrc-bsu.cam.ac.uk/Rwui/tutorial/Technical_Report.pdf, it is said that the "The R script must not set the working directory" -
I don't understand what they do mean by that. I have checked the help but it's still obscure for me. Could someone briefly explain me what this sentence means?
I checked my R script and it seems to create a directory. setwd("a path").
I would take it to mean that your script should not set a working directory. You probably need to remove the setwd() command and instead read in your files using the full path as the filename
I am just starting to explore the rmarkdown package. I don't use Rstudio. I use the default R environment. What I did was as follows.
I created a new R document.
Started typing few lines in rmarkdown format.
Saved the file with Rmd extension.
I saved the file in the working directory.
I installed the pandoc using the pkg file.
I installed 'rmarkdown' package. Loaded the package.
Used the following command to render the Rmd file.
rmarkdown::render("Untitled.Rmd")
I get the following error.
Error in tools::file_path_as_absolute(input) : file 'Untitled.Rmd'
does not exist
I tried all the possible ways such as giving the exact path instead of filename etc. But nothing worked out. I googled the error message and found that none had similar error. Can someone help me with this. What I am missing. What the error message mean?
Most of the time the error file not found is either a type error or a real missing file (as in your case, the real one is named in another way).
In order to discard those possibilities:
Copy the fullpath from your filebrowser.
Make sure the file exists, inside R you could type:
file.exists("/fullpath/to/file")
If that return TRUE and the error persists, then you suspect another thing is going on.
I have seen many related answers here,but i didn't get a proper way to solve my problem under windows system...
I know the link the similar question
I got that setwd() can locate the directory what i want,however,my R script may move to another directory without any modification,so I want to know the current file directory,becase there are expression like source(...),this called source file and the execution file under the same parent directory in a R project,how I can do?
any help appreciated.
You can get your current directory using the getwd() function and give it a name, say:
cpath = getwd()
Another useful function is the file.path, which can help you specify new directories with simple syntax. For example, you want to get the directory that is one level "above" the current directory, you can use:
upp.dir = file.path("..", "cpath")
This gives upp.dir as "../Your_Current_Dir". How about changing to another folder (called Folder_A) in current directory? Use:
folderA = file.path("cpath", "Folder_A")
These may help easy navigate the file system.
Basically, if you write scripts and those scripts depend on where they are, then you are Doing It Wrong.
Write code in packages. Parameterise functions to make them generally applicable. If you have folders with data in, then make one of those parameters a folder.
A script called with source() cannot reliably locate itself, but that shouldn't be a problem, because WHATEVER CALLED THE SCRIPT knows where the script is (it has to, or how else can it call it?) so it could pass that as a parameter. Something like:
> youarehere = "C:\foo\"
> source("C:\foo\bar.R")
and now bar.R can do setwd(youarehere) and it will work, even if it is badly written such that it relies on sourcing other code in its containing folder.
Or you can do:
> setwd(youarehere)
> source("bar.R")
in your calling function.
But really, its a fail, its a sign of badly written code. Use functions, write packages, use devtools, its really not that hard, then your code will work anywhere and you wont be writing stupid scripts that are a twisty turny maze of source() calls.
Stay classy.