Read.csv returns an error: Cannot Open Connection [duplicate] - r

This question already has answers here:
read.table() and read.csv both Error in Rmd
(5 answers)
Closed 5 years ago.
I have a strange problem. I want to read a CSV file, and I am perfectly able to do so in my console, but when I do the exact same line in my R markdown file, I get the error:
Error in file(file, "rt") : cannot open the connection
Why can I read the file in the console but not in the R Markdown file?

Working directory of your Rmd differs from the ones of your R console. You should explicitly define the path to your file in the Rmd to avoid the error.
Another option would be moving your csv files to the folder that your markdown is existed.
In sum, You need to consider where the Rmd is, where the current directory of your console is and where your files are. If necessary move the files or change the directory or use explicit path.
Using getwd() you can get the current working directory for your markdown or your console.

Related

R markdown can't save file [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Tried to save my R markdown document as a file in a folder. File to save as and then put it in my lab folder. Titled the r document R-Lab6/7.rmd and it does not let me save the file. The error is "Error file not found".
The folder/directory must already exist before you can save a file into it. You can either create the folder outside of R (in your operating system's file browser, or in RStudio's "files" panel), or you can use dir.create("R-Lab6") from the R console.
Here's an illustration of the problem, using command-line functions:
save("x", file="a/b.rda")
Error in gzfile(file, "wb") : cannot open the connection
In addition: Warning message:
In gzfile(file, "wb") :
cannot open compressed file 'a/b.rda', probable reason 'No such file or directory'
Now create the directory a:
dir.create("a")
and saving works
save("x", file="a/b.rda")
So it has now saved a file called b.rda in directory a (not a file called a/b.rda).
I originally thought/misunderstood that you wanted to include a / in your file name.
While modern tools allow you to use almost any characters you like in a file name, slashes are almost never allowed, because they are used by the operating system to denote that a file is contained in a directory (in Windows and on Linux and almost always on MacOS).
Once you start dealing with programming, it's safest not only to avoid slashes (which are impossible) but also other "special" characters: use only numbers, letters, and simple punctuation (dash -, underscore _, and dot .) in your file names ...

How to Read in xls file that requires permission to open in R

I have several xls files I need to read in and combine into one dataframe. I try
df <- readxl::read_excel("file.xls")
or
df <- readxl::read_xls("file.xls")
but neither works. I get the following error
Error:
filepath: /Users/.../.../file.xls
libxls error: Unable to open file
I believe the issue is that every time I open the file in Excel, I am asked if I trust this file before I can open it. Is there anyway around it?
I also am operating on a mac, and I want to avoid library(xlsx) or other packages that have Java dependencies.
UPDATE: I had the idea of just going into each file to click "Save As..." and change the format to xlsx instead of xls, but the default file format that showed was an Excel 2004 XML Spreadsheet (.xml). Does that suggest that my file is actually a xml file even though the extension in the name is .xls?

R won't let me open saved file, says "no such file or directory" [duplicate]

This question already has answers here:
How to open CSV file in R when R says "no such file or directory"?
(16 answers)
Closed 2 years ago.
I just started using R a couple of weeks ago, I'm a new student in a research group that uses the program.
My supervisor gave me a file with the ending .idx to open in R and work on. I was eventually able to open it yesterday, but it took me 45 minutes! I thought I'd figured out how to do it yesterday, but again today R is telling me the file doesn't exist.
Yesterday I used list.files and file.exists and the file magically seemed to exist.
Please can anybody tell me how to reopen saved files from R back on to the console?
R won't let me open saved file, says “no such file or directory”
It sounds like you just don't have the working directory set to the directory that contains the file in question. Try typing getwd() at the R command prompt and see what path it returns. Chances are, it's not the path were your file is. In that case, you can either move the file into R's current working directory, or you can type setwd("/path/to/the/file") at an R command prompt to set the path (obviously, /path/to/the/file should be replaced with the path to the directory containing your file).

R-Studio cannot read .txt file

I have a problem that I cannot solve, and it is the following: I am writing a document with R-Studio, but every time I try to run it, the program says to me that there is an error. The error apparently looks like to be linked to the fact that R studio is not able to load a table from a .txt file.
This is what I have done:
I opened R-Studio and I set the working directory (with the setwd() command) in the folder where the .txt file from which I want to source the table is located;
I opened the R-Markdown script file (with .Rmd extension) and I run it ('knit HTML' button);
R-Studio gave me the following error:
Errore in eval(expr, envir, enclos) : oggetto "mydata" non trovato; Calls: ... handle -> withCallingHandlers -> withVisible -> eval -> eval
How can I solve this? I cannot figure out why R-Studio is not able to read the table and load the data.frame, while the Console inside R-Studio is.
Thank you for any help you can provide.
When you knit a document in R-Studio, this happens in a separate environment. This means that variables that are available while you type in the console are not available when you knit a document. Similarly, when you set the working directory in the console, this has no effect on knitting a document.
So the solution to your problem could be to simply add the setwd() command to your .Rmd file.
In your R Markdown file, you have to specify the path to the txt file relative to the location of the Rmd file.

Error processing gps file in R script

Newbie R question: I have been trying to test the R script posted in FlowingData, but the script spit out the following error:
Error: XML content does not seem to be XML: 'NA'
I am running R on my windows box, with the .gpx files in the same directory as the script. Any help is appreciated.
Not sure if you ever found the answer to this or not, but the XML error relates to the fact that R does not know where your .gpx files are. While the FlowingData script indicates that the script will work if the .gpx files are in the same folder as your saved R script copy/pasted from FlowingData, that is not true. You must also set your working directory to this path as well, then R will see your .gpx files. If you FlowingData R script file and .gpx files are in: C:\Users\leon\Documents\R then add this line under the library(plotKML) line to set your working directory: setwd("C:\\Users\\leon\\Documents\\R")
Another word of note, make sure you only use the RunKeeper gpx files for a fairly small geographic area or the plotted data will be insanely small.

Resources