paste0 broken after updating R - r

I have been running a very simple code in R for scraping a csv and saving it, and I am pretty sure it was working all fine until I updated R this morning.
My workspace clearly does contain folder 'reports/google'(), but I am getting a destination error. A code written as such:
download.file('https://www.file.csv',
destfile = paste0('reports/google/google_', today_date, '_report.csv'))
produces an 'Error in download.file("https://www.file.csv", :
cannot open destfile 'reports/google/google_2020-06-07_report.csv', reason 'No such file or directory'
This occurs despite the folders existing and csv being online. Is there a package which I missed installing during update for paste0? I would have thought it updated with R itself as it is a base suite package. Any advice would be welcome.
Edit: if I only paste0 as
destfile = paste0('google_', today_date, '_report.csv'))
it works just fine. But I would really like to save it to subfolder.

Strangely enough, restarting my PC resolved the issue, without any change in code, for future reference of anyone finding this problem after updates.

Related

I am getting an error that I do not understand when I try to knit documents on RStudio

I am a relatively new R user. I've been knitting documents in the past using RStudio to create HTML and PDF outputs of my R files.
I have no idea what changed, but approximately 1 month ago I tried to knit a document and got the error portrayed in the picture. I am borderline-competent at R on my best days but otherwise have virtually no knowledge of anything "computer" or "coding" outside of what I've learned from DataCamp regarding R. I have been trying to figure out what happened so that I can continue knitting files, but legitimately do not understand.
I would be more than happy to provide any additional information/context that you need to help me solve this problem but, unfortunately, I just don't know where to start or what's required.
I know this is a bit of a bullshit question and I've tried to avoid asking about it for a while b/c I know I don't know enough to ask it correctly, but I'm at a point where I just want to start knitting again and I'll do whatever it takes to get back to that!
The things I've tried so far are:
Downloading and installing the newest version of R
Downloading and installing RStudio again
Downloading and installing MacTeX again
Trying random solutions from Stack Overflow that involve writing things in my "Terminal" (which I subsequently erase when they don't work).
I am using a MacBook Pro running macOS Catalina 10.15.7 for what it's worth.
I've copied and pasted the text of the error message below:
Error in file(filename, "r", encoding = encoding) :
cannot open the connection
Calls: source -> file
In addition: Warning message:
In file(filename, "r", encoding = encoding) :
cannot open file 'renv/activate.R': No such file or directory
Execution halted
I had the same error and didn't want to run my Rmd files from the console, so there is the solution for using the button again (which I found here):
> usethis::edit_r_profile()
* Modify 'C:/Users/User/Documents/.Rprofile'
* Restart R for changes to take effect
The output then shows you the path to your file. Just copy that path to the address bar in your explorer and choose the program you want to open it with. I chose the normal text editor. Delete "source('renv/activate.R.')" from this file, restart R, and everything should work fine again.

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().

What is this error: File does not exist error with roxygenise?

I have been using roxygen2::roxygenise quite successfully with my package to build exported and imported functions. However, I recently ran into this error which I am unable to resolve:
> roxygen2::roxygenise()
First time using roxygen2. Upgrading automatically...
Error: File file does not exist
This is not the first time I have used roxygen2 with this package. In addition, I am not quite sure what "File file does not exist" means. Has anyone else seen this and been able to resolve it?
I think you have to set the working directory of R to be the package folder
I got the same error when using:
roxygen2::roxygenise("mypackage")
But, I didn't get the error when I instead used:
library(roxygen2)
roxygenise()
I know this is no explanation and I can't comment upon whether this works beyond my case.

Error in fetch(key) : lazy-load database

I don't know what is going on, everything was working great but suddenly I started to have this error message on the documentation:
Error in fetch(key) : lazy-load database '......descopl.rdb' is
corrupt
I removed almost all my code and build again then publish to Github, but when I use the other laptop to download the package, the package is being downloaded and loaded but I can't call any of the functions, and the documentation states that error.
I don't know what caused the problem, I am using roxygen to generate the documentation.
https://github.com/WilliamKinaan/descopl
It seems that the error arises when the package cannot be decompressed by R (as #rawr established, it is corrupt). This solutions have worked for me:
1) Check for possible errors in the creation of the .Rdb files
2) Try restarting your R session (e.g. .rs.restartR() if in RStudio)
3) The package might have been installed in your computer (even though it does not work). Remove it using ?remove.packages()
I have had this problem with roxygen2 as well. Couldn't see any problem with any of my functions. In the end deleting the .rdb file and then getting roxygen2 to rebuild it seemed to solve the problem.
I think the explanation for what is causing this is here.
It's related to devtools.
Per #Zfunk
cd ~/Rlibs/descopl/help
rm *.rdb
Restart R. Look at the help for the package again. Fixed!
I received this error after re-installing a library whilst another R session was running.
Simply restarting the existing R session(s) solved for me (i.e. running .rs.restartR() to restart the sessions)
If you are using R-studio:
1) ctrl+shift+f10 to restart r session
2) tools -> Check for package updates -> update all packages
3) library(ggmap)
Problem is solved.
Basically all answers require restarting R to resolve the issue, but I found myself in an environment where I really didn't want to restart R.
I am posting here a somewhat hack-ish solution suggested by Jim Hester in a bug report about the lazy-load corruption issue.
The gist of it is that the package may have some vestigial S3 methods listed in session's .__S3MethodsTable__. environment. I don't have a very systematic way of identifying which S3 methods in that environment come from where, but I think a good place to start is the print methods, and looking for S3method registrations in the package's NAMESPACE.
You can then remove those S3 methods from the .__S3MethodsTable__. environment and try again, e.g.
rm(list="print.object", envir = get(".__S3MethodsTable__.", envir = baseenv()))
You may also need to unload some DLLs if some new messages come up like
no such symbol glue_ in package /usr/local/lib/R/site-library/glue/libs/glue.so
You can check getLoadedDLLs() to see which such files are loaded in your session. In the case of glue here, the following resolved the issue:
library.dynam.unload('glue', '/usr/local/lib/R/site-library/glue')
I got this error on RStudio on mac OS - updating all the packages and restarting r session did the trick.

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