Error in RStudio with read.table(url...) - r

I tried to run the code below in RStudio, it always returns the error message of connection failure. It works in RGui. Any idea why this is and how to fix it? Is it problem with my Rstudio (I'm running Windows 8)?
survey <- read.table(url("ftp://ftp.ics.uci.edu/pub/machine-learning-databases/adult/adult.data"),
header=FALSE, sep=",", quote="", stringsAsFactors=FALSE)
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
InternetOpenUrl failed: 'The FTP session was terminated
b'
Alternative code I have tried:
survey <- read.table(url('ftp://ftp.ics.uci.edu/pub/machine-learning-databases/adult/adult.data'),
header=FALSE, sep=",", quote="", stringsAsFactors=FALSE)
survey <- read.table('ftp://ftp.ics.uci.edu/pub/machine-learning-databases/adult/adult.data',
header=FALSE, sep=",", quote="", stringsAsFactors=FALSE)
Thanks!

RStudio and RGUI may be using different defaults for setInternet2(), so try running setInternet2(use=FALSE) at the beginning of your session.
Here is an explanation from RStudio (2013):
Also we call setInternet2(use=TRUE) on startup which takes proxy
settings from Internet Explorer to make proxies work in the majority
of cases. We also have an open bug to allow users to turn this off on
startup, but for now you'll have to call setInternet2(use=FALSE)
manually for each session. For more information on the command, call
the following from the console: ?setInternet2()
https://support.rstudio.com/hc/communities/public/questions/200657716-Rstudio-with-aproxy
For Windows, you may have the option to turn this off within RStudio, as someone with Windows mentioned the solution below (2014):
One solution was to go to Tools > Global Options > Packages, and
unselect "Use Internet Explorer library/proxy for HTTP".
https://support.rstudio.com/hc/communities/public/questions/201327633-Bug-report-RStudio-causes-download-file-to-fail-with-FTP-downloads

Related

Connecting to Spark with Sparklyr gives Permission Denied Error

After installing sparklyr package I followed the instruction here ( http://spark.rstudio.com/ ) to connect to spark. But faced with this error. Am I doing something wrong. Please help me.
sc = spark_connect( master = 'local' )
Error in file(con, "r") : cannot open the connection
In addition: Warning message:
In file(con, "r") :
cannot open file 'C:\Users\USER\AppData\Local\Temp\RtmpYb3dq4\fileff47b3411ae_spark.log':
Permission denied
But I am able to find the file at the stated location. And on opening, I found it to be empty.I
First of all, did you install sparklyr from github devtools::install_github("rstudio/sparklyr") or CRAN?
There were some issues some time ago with Windows installations.
The issue you have seems to be related to TEMP and TMP folder level permission on Windows or to file creation permission. Every time you start sc <- spark_connect(), it tries to create a folder and file to write the log files.
Make sure you have a write access to these locations.
I could observe the same error message with version 2.4.3 and 2.4.4
in different cases:
When trying to connect to a non "local" master, using spark_connect(master="spark://192.168.0.12:7077", ..),
if the master is not started or not responding at the specified master url.
when setting a specific incomplete configuration
in my case trying to set dynamicAllocation to true, without other required dynamicAllocation settings:
conf <- spark_config()
conf$spark.dynamicAllocation.enabled <- "true"

Launching R help: Error in file(out, "wt") : cannot open the connection

I want to launch R help, I type ?dir.create. It is giving me this strange error:
Error in file(out, "wt") : cannot open the connection
In addition: Warning message:
In file(out, "wt") :
cannot open file
'C:\Users\XYZ~1\AppData\Local\Temp\Rtmp86bEoJ\Rtxt32dcef24de2': No
such file or directory
Please help me. What do I do to overcome this problem? Even googling this problem didn't give me much info.
This happens when the temporary directory used by R is deleted. This usually happens when user or the system cleans up the tmp directory while the R session is running. Typically happens if the R session is open for a long time.
Remedy is very simple. You don't actually need to restart R. Just re-create the temporary directory by:
tempdir()
# [1] "C:\Users\XYZ~1\AppData\Local\Temp\Rtmp86bEoJ\Rtxt32dcef24de2"
dir.create(tempdir())
NOTE: this happens to me more and more often in Windows 10... even within like 8 hour session only! Not sure why this happens. See this question: https://superuser.com/questions/1502272/windows-10-cleaning-up-my-temp-dir-too-often
Ok.Thank you all.I was able to resolve this by changing the directory and exiting R and re-logging into swirl.

Download.file fails in RStudio

file<-tempfile(fileext=".csv")
download.file(url="ftp://pubftp.spp.org/Markets/DA/LMP_By_SETTLEMENT_LOC/2014/03/28/DA-LMP-SL-201403280100.csv",destfile=file,mode="wb")
This works in R proper (I'm not sure what to call it). However in RStudio it hangs for several minutes and then I get the following
trying URL 'ftp://pubftp.spp.org/Markets/RTBM/LMP_By_SETTLEMENT_LOC/2014/03/25/11/RTBM-LMP-SL-201403251015.csv'
using Synchronous WinInet calls
Error in download.file(url = "ftp://pubftp.spp.org/Markets/RTBM/LMP_By_SETTLEMENT_LOC/2014/03/25/11/RTBM-LMP-SL-201403251015.csv", :
cannot open URL 'ftp://pubftp.spp.org/Markets/RTBM/LMP_By_SETTLEMENT_LOC/2014/03/25/11/RTBM-LMP-SL-201403251015.csv'
In addition: Warning message:
In download.file(url = "ftp://pubftp.spp.org/Markets/RTBM/LMP_By_SETTLEMENT_LOC/2014/03/25/11/RTBM-LMP-SL-201403251015.csv", :
InternetOpenUrl failed: ''
It is a small file so it shouldn't time out but I really don't know what the problem is.
I found two solutions.
1) Go to Tools > Global Options > Packages, and unselect "Use Internet Explorer library/proxy for HTTP".
2) This worked for another user, but not for me: setInternet2(use=FALSE)
(https://support.rstudio.com/hc/communities/public/questions/200656136-Issue-With-RStudio-and-GEOquery)
Note: when in RGUI I entered setInternet2(use=TRUE), then tried the download, it gave the "using Synchronous WinInet calls" messages and hung; but then Windows Firewall popped up, and when I allowed RGUI through it, the download began.
I have the same problem when I download a file in RStudio when using
> source("http://www.statmethods.net/RiA/wmc.txt")
Error in file(filename, "r", encoding = encoding) :
cannot open the connection
In addition: Warning message:
In file(filename, "r", encoding = encoding) :
InternetOpenUrl failed: '安全频道支持出错'
Then I try
> options(download.file.method="libcurl", url.method="libcurl")
> source("http://www.statmethods.net/RiA/wmc.txt")
> wmc
It worked
I had a similar issue using R's download.file in a for loop in RStudio. It would download the url for the first several links and then I'd get "InternetOpenUrl failed: 'The operation timed out'" for all subsequent downloads. I tried the the suggestion by sssheridan to unselect the Internet Explorer option in R global options which did not work. I also tried setInternet2(use=T) but this is no longer available in R.
What worked for me was to remove the cache by including cacheOK = F as an argument in download.file. I think this is because I had previously hit the links that were timing out.
Go to Tools > Global Option > Packages > uncheck "Use secure download method for HTTP".

Importing a csv into R using read.csv in Ubuntu

I have just installed Ubuntu on my computer and I am re-running some codes that previously worked in Windows. I have copied my directories into Ubuntu with all my files.
When I run this line of code to import a database into R, I get the following error:
Annot <- read.csv("~/Documents/DATABASES/Functional_Annotations/Salar_2_Annot_light.csv", header = TRUE)
Error in file(file, "rt") : cannot open the connection
In addition: Warning message: In file(file, "rt") : cannot open file
'/home/cd46/Documents/DATABASES/Functional_Annotations/Salar_2_Annot_light.csv':
No such file or directory
The code is right, hasn't changed since before. In fact if I run:
setwd("~/Documents/DATABASES/Functional_Annotations")
It works fine and recognize the directory. And the file it there too.
I am not sure what this can be, does anyone have a suggestion? The only thing I have done was to switch over to Ubuntu, so I would imagine the problem would lie there.
I have installed the readr package in R
then simply write this :
df <- read_csv("/your path/file.csv")
And this work for me and solved the problem.

Why I get this error writing data to a file

I have this code
myvector <- c(3.45235, 1.32525, ... , 2.41351) # some numbers
write(myvector, "C:/mypath/myfile.txt") # I use "/" instead of "\"
and I get the following error:
Error in file(file, ifelse(append, "a", "w")) : cannot open the
connection In addition: warning message: In file(file, ifelse(append,
"a", "w")) : cannot open file 'C:/mypath/myfile.txt' : No such file
or directory
I read this tutorial, but I can't understant what's wrong with my code. Any idea?
edit:
As #dickoa pointed out, I need an existing path to write a file, so I tried to simplify in the following way:
file.exists("C:/")
write(myvector, "C:/myfile.txt")
Surprisingly :P the path "C:/" exists (the result is TRUE) but I get a similar error:
Error in file(file, ifelse(append, "a", "w")) : cannot open the
connection
In addition: warning message: In file(file, ifelse(append,
"a", "w")) : cannot open file 'C:/mypath/myfile.txt' : Permission denied
I know #dickoa answered the question in the comments, but in order to provide at least one answer here, I wanted to go through a few simple gotchas with R on Windows.
When you are using Windows, you still have to use forward slashes for paths. In R, backslashes are reserved for escaping values. So a path in R looks like:
C:/path/to/my/directory
In newer variants of Windows, the C:\ is protected from writes by user accounts. If you want to write to the C:\, you must be an administrator. You can accomplish this by right-clicking on the R icon in Windows and choosing "Run as an administrator." This should also be done when you're installing packages. You may not have rights to install packages on certain Windows versions if you don't run it as an administrator.
If you don't want to run R as an administrator, and you want to write to files, you will by default have rights to the C:/Users/username/ directory.
All credit to #dickoa again for his answer in first.
Best of luck!
just adding to answers here.
The reason I was facing this error was, the path I was trying to save in exceded 256 characters, and hence the error.
Problem was sorted once I reduced the path size.
I just shared this answer with a bit of a better explanation here, but the gist of it is:
Try opening the file in Excel to see if it's locked by another user. I was receiving the same error messages and was able to figure out that a colleague had the file open on their computer which had locked me out of the ability to edit it.
Sometimes the problem is in the naming of the file. For example,I have encountered this issue when in the name of the file there was as "\", as there was a dynamic list with names. You can pass by such thing by using something like: sometext = gsub("/"," ", sometext).
It happens when you open the myfile.txt and run the code. Try to close the myfile.txt in your machine and run the command. It solves your problem.
Just another possibility. I have encountered this problem when running the following code. Because I have successfully run this code before this starting RStudio, I run successfully after restarting RStudio this time. So, sometimes, restarting will solve the problem, although I haven't figured out what happened behind.
DT::datatable(
dt.cmbn,
extensions = c('ColReorder','FixedColumns'), rownames = FALSE,
options = list(
colReorder = TRUE,
#dom = 'Bfrtip',
#buttons = I('colvis'),
scrollX = TRUE,
fixedColumns = TRUE
)
)

Resources