Installing packages onto R - r

For some reason I am suddenly not able to install packages in R (I have subsequently updated to the latest version of R and am running Windows 7). For example, if I type:
install.packages('beeswarm')
Installing package into ‘D:/Rlibs’ (as ‘lib’ is unspecified)
--- Please select a CRAN mirror for use in this session --- trying URL 'http://www.stats.bris.ac.uk/R/bin/windows/contrib/3.0/beeswarm_0.1.5.zip'
Content type 'text/html' length unknown opened URL downloaded 1859
bytes
Error in read.dcf(file.path(pkgname, "DESCRIPTION"), c("Package",
"Type")) : cannot open the connection In addition: Warning
messages: 1: In unzip(zipname, exdir = dest) : error 1 in extracting
from zip file 2: In read.dcf(file.path(pkgname, "DESCRIPTION"),
c("Package", "Type")) : cannot open compressed file
'beeswarm/DESCRIPTION', probable reason 'No such file or directory'
I have read that in Windows 7 there can be important restrictions on rights to writing to certain folders etc. so I've gone to some lengths to install R and library folders in non-default areas of my computer, and to allow myself rights to certain folders, but to no avail. Possibly also of importance is when I type:
.libPaths()
# [1] "D:/Rlibs"
# [2] "C:/Users/L.Halsey/Documents/R/win-library/3.0"
# [3] "C:/Users/L.Halsey/Documents/Documents/R-3.0.1/library"
I have created several folders in an attempt to create one that I could successfully install libraries into and set them up to be recognised by R using 'environment variables' from the start button. I don't know how to delete any of them though - not sure if this is relevant to my overall problem of not now being able to install/update packages for some reason.

The error being reported is inability to open a connection. In Windows that is often a firewall problem and is in the Windows R FAQ. The usual first attempt should be to run internet2.dll. From a console session you can use:
setInternet2(TRUE)
(You are correct in thinking this is not due to your library setup. The error says nothing about permissions.) I don't think just typing .libPaths should return that character vector since on my machine I would need to type .libPaths() to see something like that. If you wanted to reduce the number of places for libraries you can use the .libPaths function for setting the values. This would pick the second and third of the existing paths
.libPaths( .libPaths()[2:3] )
The inner call retrieves the path vector and the outer call sets it to a reduced vector.

Running RStudio as administrator fixed it for me!

I will probably duplicate a lot of other answers on the stackoverflow, but I got exactly the same error as OP, namely:
Warning messages: 1: In unzip(zipname, exdir = dest) : error 1 in extracting from zip file 2: In read.dcf(file.path(pkgname, "DESCRIPTION"), c("Package", "Type")) : cannot open compressed file 'zoo/DESCRIPTION', probable reason 'No such file or directory'
Turned out, while I as a user had permissions to write in a certain directory, R did not. In order to be sure you don't have something similar, do following:
get a usb drive, let's name it E
download package source as a .zip file and store it onto usb-drive in some directory, let's name it E:/source
Create directory for libraries on the usb drive, let's name it E:/libs
Install packages calling R command install.package from the R console and setting all relevant directories to point to your usb drive:
(here I use package zoo as an example)
install.packages("E:/source/zoo_1.7-12.zip",
destdir = 'E:/source', # no "/" after the path
lib = 'E:/libs',
repos = NULL)
Load the package from the directory, where you installed it:
library('zoo', lib.loc = 'E:/libs')
After you are sure, it works this way on your usb drive, you can start resolving directories permissions, and try out by changing the paths in the code above.
update:
In some windows environments even your usb-stick might be protected from read-write by the R. Make sure you check the permissions using the machine you are working from.

The following worked for me (based on the answer above)
install.packages("clustvarsel", lib = "C:/Users/dnentchev/My Programs/R-3.2.2/library")

I had the same problem. I turned the windows firewall off, and Run RStudio as administrator. so, that error fixed.

Related

cannot install tinytex package. (Maybe the repository setting should be changed.)

im using R and quite new at rmarkdown. i want to knit my rmarkdown into pdf formatted file and i already read that i need to install tinytex package.
i enter the code as instructed on https://yihui.org/tinytex/
install.packages('tinytex')
tinytex::install_tinytex()
and i got this error code
Starting to install TinyTeX to C:\Users*****\AppData\Roaming\TinyTeX. It will take a few minutes.
Automated TeX Live installation using profile: ../tinytex.profile
Loading https://mirror.unpad.ac.id/ctan/systems/texlive/tlnet/tlpkg/texlive.tlpdb
C:\Users*****\AppData\Local\Temp\RtmpW6xVT8\install-tl-20191206\install-tl: TLPDB::from_file could not initialize from: https://mirror.unpad.ac.id/ctan/systems/texlive/tlnet/tlpkg/texlive.tlpdb
C:\Users*****\AppData\Local\Temp\RtmpW6xVT8\install-tl-20191206\install-tl: Maybe the repository setting should be changed.
C:\Users*****\AppData\Local\Temp\RtmpW6xVT8\install-tl-20191206\install-tl: More info: https://tug.org/texlive/acquire.html
TinyTeX installed to C:\Users*****\AppData\Roaming\TinyTeX
Please quit and reopen your R session and IDE (if you are using one, such as RStudio or Emacs) and check if tinytex:::is_tinytex() is TRUE.
Warning message:
In file.remove("TinyTeX/install-tl.log") :
cannot remove file 'TinyTeX/install-tl.log', reason 'No such file or directory'
i restart my r and rstudio, run tinytex::is_tinytex(), returned FALSE.
i guess the repository cannot be accessed anymore so i tried to change the repo but still it doesnt change.
can anyone help me with solution? thanks
UPDATE :
i tried to run this as Yihui Xie told me :
tinytex::install_tinytex(repository = 'http://dante.ctan.org/tex-archive/')
and
tinytex::install_tinytex(repository = 'http://mirrors.ibiblio.org/CTAN/')
and return this
trying URL
'http://dante.ctan.org/tex-archive/systems/texlive/tlnet/install-tl.zip'
length 22539829 bytes (21.5 MB) downloaded 21.5 MB trying URL
'https://yihui.name/gh/tinytex/tools/pkgs-custom.txt' Content length
81 bytes downloaded 351 bytes trying URL
'https://yihui.name/gh/tinytex/tools/tinytex.profile' Content length
81 bytes downloaded 295 bytes Starting to install TinyTeX to
C:\Users\Kandu\AppData\Roaming/TinyTeX. It will take a few minutes.
Automated TeX Live installation using profile: ../tinytex.profile
Loading
https://mirror.unpad.ac.id/ctan/systems/texlive/tlnet/tlpkg/texlive.tlpdb
C:\Users\Kandu\AppData\Local\Temp\Rtmp8OFCvK\install-tl-20191209\install-tl:
TLPDB::from_file could not initialize from:
https://mirror.unpad.ac.id/ctan/systems/texlive/tlnet/tlpkg/texlive.tlpdb
C:\Users\Kandu\AppData\Local\Temp\Rtmp8OFCvK\install-tl-20191209\install-tl:
Maybe the repository setting should be changed.
C:\Users\Kandu\AppData\Local\Temp\Rtmp8OFCvK\install-tl-20191209\install-tl:
More info: https://tug.org/texlive/acquire.html TinyTeX installed to
C:\Users\Kandu\AppData\Roaming/TinyTeX Please quit and reopen your R
session and IDE (if you are using one, such as RStudio or Emacs) and
check if tinytex:::is_tinytex() is TRUE. Warning messages:
1: In
file.remove("TinyTeX/install-tl.log") : cannot remove file
'TinyTeX/install-tl.log', reason 'No such file or directory'
2: In
in_dir(target, { : The repository
http://dante.ctan.org/tex-archive/systems/texlive/tlnet does not seem
to be accessible. Reverting to the default CTAN mirror.
idk why but it keep trying to download from https://mirror.unpad.ac.id.
any explanation?
At the moment, the mirror https://mirror.unpad.ac.id does not seem to have a valid SSL certificate, so the site is not accessible. You can find other accessible mirrors at https://ctan.org/mirrors/mirmon. To specify the mirror, use the repository argument. Below are two possible examples:
tinytex::install_tinytex(repository = 'http://mirrors.ibiblio.org/pub/mirrors/CTAN/')
tinytex::install_tinytex(repository = 'http://dante.ctan.org/tex-archive/')

R installed.packages() randomly stopped working on windows 7

installed.packages() command in R lists your installed packages. Mine was working for almost a year and then this command randomly started throwing an error. As this is a built-in command, I am not even sure how to "reinstall" it or address this. Any ideas how to fix the error and get the command working again?
> installed.packages()
Error in gzfile(file, mode) : cannot open the connection
In addition: Warning message:
In gzfile(file, mode) :
cannot open compressed file `'C:\Users\Mitch\AppData\Local\Temp\Rtmp6Dawpa/libloc_190_4464fd2b.rds', probable reason 'No such file or directory'`
One suggestion on here involved this in combination:
.libPaths()
installed.packages(lib.loc = 'my path')
The results of this produced yet another error as shown here. Looks like an issue with the installed file still but how to address is the question:
> installed.packages(lib.loc = 'C:/ProgramFilesCoders/R/R-3.3.2/library')
Error in gzfile(file, mode) : cannot open the connection
In addition: Warning message:
In gzfile(file, mode) :
cannot open compressed file 'C:\Users\Mitch\AppData\Local\Temp\Rtmp6Dawpa/libloc_190_4464fd2b.rds', probable reason 'No such file or directory'
>
That is odd.
What version of R are you running, standard R or Microsoft R? And did you recently update?
If you did recently update, perhaps your packages did not get copied over, hence the 'No such file or directory' statement.
If you haven't updated, I would install a newer version and see if it fixes the issue.
If your uncertain, you can always use the updateR function to check if you have the latest version and choose to install it or not.
library(installr)
updateR()
Good luck,
I think the issue lies in terms of the where the function is looking for the package information. installed.packages() needs an argument lib.loc.
From official documentation
lib.loc character vector describing the location of R library trees to search through
Looks like the function for some reason is looking in AppData\Local\Temp which is the download location and not the installed location.
Without looking at your R_Home and .libPaths() is difficult to nail down where the problem is, however running .libPaths() should give you one or more paths as shown in the below example. None of these should be temp locations.
>.libPaths()
[1] "C:/Users/UserName/Documents/R/win-library/3.4"
[2] "C:/Program Files/R/R-3.4.0/library"
If not, you can set the path within the .libPaths("your path") or pass the path of the library as part of installed.packages(lib.loc = 'your path') and try again.
Sometimes the most simple obvious solution is what works:
I closed my RStudio environment saving it to .RData
I re-opened RStudio and tried the command again
it worked
For the future, some good ideas got posted on here before I thought to try the above. Here are the suggestions that others included in case the above does not work if this problem is encountered by anyone in the future:
Use .libPaths() to find out proper path where this is installed, and then re-run the command with the path included in it like so: installed.packages(lib.loc = 'your path')
Try debugging it with: debug(installed.packages); Expectation is that we will likely find something wrong with .readPkgDesc(lib, fields) while stepping through debug. This was not tried yet so you may encounter things not written up here when you do try it.
Try Updating R in case it is out of date with these commands: library(installr) and updateR().

Linking R to ImageMagick

Trying to use the awesome gganimate package in R, but was having trouble getting my animation to run. Reverted to trying to run the base example but couldn't get it to link up with ImageMagick.
Input:
library(gapminder)
b = ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, color = continent, frame = year))+ geom_point() +scale_x_log10()
gg_animate(b)
And the output:
I cannot find ImageMagick with convert = "convert"
Error in file(file, "rb") : cannot open the connection
In addition: Warning messages:
1: running command 'C:\windows\system32\cmd.exe /c convert --version' had status 4
2: In find_magic() : ImageMagick not installed yet!
3: In im.convert(img.files, output = movie.name, convert = convert, :
Please install ImageMagick first or put its bin path into the system PATH variable
4: In normalizePath(path.expand(path), winslash, mustWork) :
path[1]="filed42411bd2b88.gif": The system cannot find the file specified
From the research I have done it seems that the issue has something to do with declaring convert.exe and the windows path, I'm just not sure what to fix to get this code to work.
I have installed ImageMagick-7.0.2-Q16 for Windows (I am running Windows 7).
What am I missing?
You don't need to declare convert.exe; Windows has a native cli tool with the same name to convert a FAT partition into an NTFS partition, not what you want.
(NB: I forget to which directory IM installs itself in windows, so please verify the path I provide before blindly using this code.)
If this is a one-time thing, you can make it available with something like:
Sys.setenv(PATH = paste("c:/Program Files/ImageMagick/bin",
Sys.getenv("PATH"), sep = ";"))
If you want a more permanent solution, you have two options:
Save the above code in a .Rprofile file, either in your home directory (affects all of your future R sessions) or in the directory of your R project needing this tool. (You either need to also run this code on the console or restart the R session for the change to be noticed in your code.)
Add it to the windows path. There are lots of places to find help with this, including https://stackoverflow.com/a/28545224/3358272.
Edit: it came to my attention that the windows install no longer provides the convenience applications (e.g., convert.exe, mogrify.exe) that the unixy install does. (I'm using IM on windows installed via msys2, so I guess it's preferring the unixy-method ... I should have known.) So that suggests a third option:
Since gapminder is using gganimate which is calling animation::im.convert, its source says you can specify the command executable via something like ani.options(convert = shQuote('c:/program files/imagemagick/magick.exe')).

r - How to specify the path in normalizePath, or get around this error associated with it?

I'm learning R and just have it installed on my office computer. I don't have the administrator right on the computer (as I even have to call IT for installation).
Then I install a package. At first it doesn't work when typing, for example:
install.packages("thepackage")
The error message is this:
Error in normalizePath(path.expand(path), winslash, mustWork) :
path[1]="\\company\5050\Users\myusername\Documents\R\win-library\3.3": Access is denied
In addition: Warning message:
In normalizePath(path.expand(path), winslash, mustWork) :
path[1]="\\company/5050/Users/myusername/Documents/R/win-library/3.3": Access is denied
I do some homework and find that a potential solution is to "Map a network drive to your network folder". I'm not sure what it means, but I try this:
install.packages("thepackage",lib="H:/Documents/R/win-library/3.3")
because it looks like I have more "control" of H drive (it has my username on it). And it works:
package ‘thepackage’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\myusername\AppData\Local\Temp\Rtmp4MNURu\downloaded_packages
I then fail to load the package,
library(thepackage)
saying:
Error in normalizePath(path.expand(path), winslash, mustWork) :
path[1]="\\company/5050/Users/myusername/Documents/R/win-library/3.3": Access is denied
But again this works:
library(thepackage,lib="H:/Documents/R/win-library/3.3")
So how can I set the normalizePath to the one that works to avoid additional and rather unnecessary specification of library directory?
You can put in your home directory's .Rprofile file (just create it if it's not there yet) the following line:
.libPaths("H:/Documents/R/win-library/3.3")
That way this location will be used by default. The .Rprofile is run every time you're opening any new R session. You can copy the existing content of the folder from which you don't have write access to this folder to include all pre-installed packages.
I think you are looking for:
system("net use D: \\\\company\\path\\")
to map to the virtual D drive. I would then use file.path when accessing the stuff on D:. It looks that you may benefit from changing R defalut library path in Rprofile.site, by adding the line:
.libPaths("Path to your libs")
When you type .libPaths() can you read and write to that directory with no problems?
I was with the same problem them I found this solution by the user Mike M.
In Windows 10/11 you can also set the R Home Directory just for R
without changing your system HOME with a special environment variable
R_USER. Adding this to your Environment Variables with the path you
want for your R Home will set the R Home path without changing your
system HOME. RStudio looks for R_USER first (and then moves on to
HOME).
It worked for me.

Error in gzfile(file, "wb"): cannot open the connection or compressed file

I'm trying to run two things: first, I'm creating a PDF with 4x5, ending with dev.off(), and then trying to create a new graph. However, after starting the second plot, I get:
Error in gzfile(file, "wb") : cannot open the connection
In addition: Warning message:
In gzfile(file, "wb") :
cannot open compressed file '/var/folders/n9/pw_dz8d13j3gb2xgqb6rfnz00000gn/T/RtmpTfm1Ur/rs-graphics-822a1c83-b3fd-46c3-8028-4e0778f91d0c/4db4b438-ac35-403b-b791-e781baba152c.snapshot', probable reason 'No such file or directory'
Graphics error: Error in gzfile(file, "wb") : cannot open the connection
What is this error? The working directory is one I have read/write access to, and my hard drive isn't full.
Also, I'm using RStudio.
This is a bit late but for anyone coming here for help, I got this error when I was trying to write a file from RStudio and my destination file path was very long. I realized this could be a problem because when I wrote the file to another location with a shorter name and tried to copy it into my original destination, Windows gave me an error saying "File path too long". You might need to save the original file into another location with a shorter absolute path.
Maybe you should look here. At the end it says
Note:
The most common reason for failure is lack of write permission in the current directory. For save.image and for saving at the end of a session this will shown by messages like
Error in gzfile(file, "wb") : unable to open connection
In addition: Warning message:
In gzfile(file, "wb") :
cannot open compressed file '.RDataTmp',
probable reason 'Permission denied'
So rapidly, if you try getwd(), look at where is your working directory set. If you're trying to save your document in a place where it's not in your current working directory, it will throw you this error.
At the end of your error message, it says probable reason 'No such file or directory'
Graphics error: Error in gzfile(file, "wb") : cannot open the connection
My diagnosis would be simply that it's trying to save your item in the wrong place and RStudio is not able to find the right place.
This burned me so hopefully saves someone else some toil. The issue was that the classifiers loaded just fine on OS X but on the Linux deployment system they would fail with the error listed in the question. The issue was the the files on the disk had extension abc.RData but the code modelAbc <- readRDS(file="abc.Rdata"). The difference in the upper and lowercase D in the .RData vs .Rdata extension would fail on Linux. It was not very noticeable but check your extensions for case.
You may have no permission to save file in the directory.
On RStudio, get your working directory by getwd().
Then, go to the directory in linux and observe its owner by ls -l.
Now you can change the owner of the directory by chown -R username directoryname.
But you must be root.
Problem resolved by specifying full file path:
saveRDS(df,'C:\\users\\matt\\desktop\\code\\df.Rdata')
I faced this issue lately. Try turning off your anti-virus and build the package, it might help. It worked for me. Usually anti-virus blocks the permissions and you could avoid it by disabling for sometime just before building a package.
I was trying to save an RDS file to my local Dropbox folder so it syncs with my Dropbox.
I figured out I got the same error because I was trying to create a new folder and looks like saveRDS cannot create a new folder, but it can add files to existing folders. So I changed the path to add the file into an existing folder and it worked!
In my case it was Windows Defender which was preventing Rstudio to write any file on hard drive. Either you need to turn Controlled Folder Access off or add Rstudio in the exclusion list.
I also had this problem when working with RStudio and R Markdown. I was getting this error message and had an annoying number of fatal errors which closed RStudio. My issue was that I was working off a network drive and either the name was too long, as in #AHedge above or my network firewalls were giving me trouble. For the moment, I have moved my working files to my desktop and things seem to be working fine. Not sure what this means for my file management over time.
Just want to add more clarity(scenarios in my experience) to what M Beausoleil mentioned.
When you are using a shared-working-directory and trying to rewrite the RDS files which are already existing in a working-directory written by some other user, you get this error.
As some people have already quoted that deleting the existing RDS files or changing the working directory works. It's not a magic. It just works because you are writing a new RDS file and not trying to re-write the old ones.
I came into the same problem after I re-install a new version of RStudio.
The Rmarkdown file I created using old version of RStudio shows the same problem.
When I use ggplot() to draw a picture the error code are as follow:
Warning in gzfile(file, "wb") :
cannot open compressed file 'I:/Rlearning/.Rproj.user/shared/notebooks/58A1385C-PCA作图/1/2C15461A183AC56C/cco192gb0pow1_t\_rs_rdf_32004888ecb.rdf', probable reason 'No such file or directory'
Error in gzfile(file, "wb") : cannot open the connection
Solution:
Create a new Rmarkdown file
Delete all codes
Copy your old Rmarkdown code into it.
I had the same problem.For me, it was caused due to not having enough disk space on the drive where R studio was installed.Freeing up space works.
The reason for the error is that your username is Chinese.Please create new user folder with English in the user directory.For example, you could name the folder for "DavidSmith".Then, you need create three folders("AppData","Local","Temp").File directory C:\Users\DavidSmith\AppData\Local\Temp.
In the Advanced system settings which will modify the environment variables TMP and TEMP C:\Users\DavidSmith\AppData\Local\Temp.Save them.
After modification, open RStudio and try again.
Notice:TMP and TEMP are modified in the USER VARIABLE.
I just ran into this problem after changing my system locale.
Check your locale using Sys.getlocale().
Change it to appropriate one using Sys.setLocale("LC_ALL","ENG") (replace "ENG" with appropriate one)
I can't say with certainty which locale would be appropriate, but it seems to be coherent with default OS one.
Hope this helps!
I had this error because of an invalid character in the filename to be used to save the file, in my case "/" (there are many such characters that cannot be used in a filename). I removed the character and it was solved.
In my case, I received the error "Error in gzfile(file, "wb") : cannot open the connection" when trying to exit R in the Anaconda Prompt and saving workspace image. I am using Windows 10 and R-3.5.2. To fix it, I had to go to the Program Files folder, right click and the R folder, then selected Properties. Selected the Security tab, then, in the Group or user names box, selected Users, then clicked Edit. In the Permissions for Users, I checked Full control and Modify and saved the changes. Then I was able to save the workspace image.
I have another instance of this error which seems to be new (or at least not listed here or here: apparently it's not OK to save a file with the name aux.RData. I guess it's a reserved filename.
x <- rnorm(9000)
save(x, file = "aux.RData")
Error in gzfile(file, "wb") : no se puede abrir la conexión
Also: Warning message:
In gzfile(file, "wb") :
cannot open compressed file 'aux.RData', probable reason 'No such file or directory'
But when I change the filename saves with no problem:
save(x, file = "aux_file.RData")
Haven't seen this case in the other answers:
if this seems to happen all the time, and to be very persistent when it does happen, check the default directory in your file handling software connection.
In my case FileZilla was logging on to my DigitalOcean droplet as "root" and whenever I used FileZilla to create a directory it was setting write permissions to "root", whereas my RStudio on the same droplet read/wrote as "My_Name". Anytime I set something up in FZ (e.g. large imported files, renamed or copied) the permissions would switch and I'd get this error.
If this is what is causing frequent error messages it can be solved instantly with chown -R My_Name directoryname but in the longer run, if you are going to be using your file handler to define and create a lot of directories, it will pay to create a connection whose default name is the same name you use for RStudio.
In my case, when it happened first, months ago, the solution here worked.
But recently, it came back, constantly... What solved this time was to change the anti-virus. I have not just the Windows defender, but also a 2nd anti-virus, the same in both times. I ended up deinstalling it and installing another antivirus... After this, the problem did not happen again...
After several days trying to solve this same ERROR or problem in my case (Windows 10 and R), I tried to save my file(file.RData) in D disk instead of C disk (where I always was working and I have installed R) and it was fine, without problems,my file was saved in D:/Users.When I tried many times to save it in C disk, always gave me Permission denied.
save(Myfile, file="D:/Users/Myfile.RData")
I encountered this same issue when trying to save an Rds file from an Markdown file. Changing my relative file path to an absolute file path worked for me.
In my case, this error was because the file that I wanted to re-write, was read-only (for whatever reason, I didn't do it myself). I just right-click on the file's name in the folder and unchecked the read-only property. After that it worked.

Resources