When I run this
zip('C:\\path\\to\\dir\\out'
, files = paste0('C:\\path\\to\\dir\\', c('one.xlsx', 'two.xlsx')))
it zips the files, but unzipping them gives a folder that contains the "path" folder, which contains the "to" folder, etc (the full directory path). So after unzipping, the files are now in C:\\path\\to\\dir\\out\\path\\to\\dir\\.
How can I zip the files without the resulting zip file containing all the parent directories?
I'm using Windows 10
platform x86_64-w64-mingw32
arch x86_64
os mingw32
system x86_64, mingw32
status
major 3
minor 4.2
year 2017
month 09
day 28
svn rev 73368
language R
version.string R version 3.4.2 (2017-09-28)
nickname Short Summer
According to the documentation, the R zip function's utilizes R_ZIPCMD, which is set in etc/Rcmd_environ. This is set to the command line zip windows utility by default. The R function provides the input parameter flags to pass additional input parameters to the underlying command line zip function. The manual which describes
flags can be downloaded here.
The -j flag allows just the file names to be stored rather than the full file path.
f_path <- 'C:\\path\\to\\dir\\out'
zip(f_path,
files = paste0(f_path, c('one.xlsx', 'two.xlsx')),
flags = '-r9Xj')
The -r9X portion of the flags input are the default parameters passed to underlying utility function and specify that the zip command should recursively search sub-directories, use maximum compression, and remove depreciated file fields.
This has only been tested using the windows zip utility. The necessary flag(s) may differ when using the unix utility.
Related
I am using R Studio 2022.02.3 Build 492 with R 4.2.0 -- "Vigorous Calisthenics" on a Win 10 PC.
When I double click on a R file, i.e. a R script, R studio opens and a 0 KB git.exe file is generated in the folder where my .R file was located.
I can not find any relevant information about this (my searches points towards using Git from RStudio). This started yesterday without any conscious change from my part and updating R and RStudio did not help.
Is this something I should be worried about?
How can stop this from happening?
Some more system info/specification:
it is a git.exe file (0 KB). Nor a git folder or anything else.
Date/time of git.exe (as shown in windows explorer) is similar to when I double click on the .R file
R is in its default place: Windows/Program files/R
RStudio is in its default place: Windows/Program files/RStudio
In RStudion, Git/SVN options are default, i.e.: Git executable: (Not Found); SVN executable (Not Found), SSH RSA key: (None)
I have both python2 and python3 installed on my desktop. If I do
python -V in the terminal I get Python 3.6.0 :: Anaconda 4.3.0 (x86_64).
However if I use the system2 command from R
system2("python", args = "-V")
then it reports Python 2.7.10
If I specify the full path it I get the right version
system2("//anaconda/bin/python", args = "-V")
Python 3.6.0 :: Anaconda 4.3.0 (x86_64)
But I'd like system2 to just use python3 by default. Is there someway to change which version it uses? This is for Mac OSX
When running R from the R application or RStudio, system calls access a different environment than they do when you run R from terminal. Because of that, the PATH environment variable you have configured to run the correct version of a unix executable in a shell program is different than the one used in a system2()or system() call in an R session in either of these applications. To solve this, you need to set the path in your R environment.
In an interactive session, you can do this:
# Reproducing your problem (in the R application or RStudio)
system2("python", args="-V")
# Python 2.7.10
# set a new PATH in the environment accessed by R
# This is the line you can also add to your .Rprofile
Sys.setenv(PATH = paste(c("//anaconda/bin", Sys.getenv("PATH"),
collapse = .Platform$path.sep))
# For users other than the OP, you'll want to use the directory
# where your preferred installation of python is. For the OP that's
# //anaconda/bin
# Confirm
system2("python", args="-V")
# Python 3.6.0 :: Anaconda 4.3.0 (x86_64)
The system command python should now be found in the directory //anaconda/bin, rather than /usr/bin. This, of course, depends on where these unix executables are found in your system, so for readers other than the OP, you'll need to use the directory that holds your desired version of python.
This PATH will remain valid through the rest of your R session. To change your path in all R sessions, update (or create, if you haven't yet) your .Rprofile file. An .Rprofile file can be (or go) in your HOME directory or R_HOME. If you add the above line to .Rprofile, each time R is initialized, they will execute at the beginning of each R session.
I have a problem downloading R in my mac which I have been dealing with for many hours now.
My purpose is to use propensity score matching in SPSS (from source forge). I am using SPSS version 22 on my mac OS X yosemite 10.10.5.
After reading a lot on the different sites I need to use R.2.15 for SPSS 22 in order to use the psmatching 3.03 on source forge.
However, I cannot seem to install R 2.15 when I run the installation package. When I am going to select destination on the macintosh HD, it says that R2.15.0 for Mac OS X 10.5 or higher can’t be installed on this disk. How is this possible? My OS X version is higher than the demanded version for R 2.15.
Any ideas how to make the installation?
I've run into this problem myself: SPSS versions require very specific versions of R to be installed for the R Integration plug-in to work. An older or newer version of R will not suffice.
I think the installation error is due to the old R installer's attempt to verify a sufficiently high OS X system version. But it doesn't recognize 10.10+ as being higher than 10.9 (or 10.5 for that matter).
The link below has information about disabling OS X El Capitan's System Integrity Protection, in order to change the OS X file /System/Library/CoreServices/SystemVersion.plist, so that a false system version can be reported to the installer.
https://forums.developer.apple.com/thread/4138
A safer method might be to manually place the files from R's 2.15 installer. There's an apparent complication with my attempt at that, though. I opened the R 2.15.pkg installer file, found a file 'payload' inside, used 'tar -xzvf Payload' to extract contents, and moved those contents into /Library/Frameworks/R.framework/Versions/2.15. In retrospect, it may be most useful to temporarily re-alias "Current" in the R.frameworks folder so that it points to 2.15 version while the installer completes. Instead, I did the following to get around installer error messages I found in the installer log file, which worked for me:
The SPSS Essentials for R installer reported that the install location does not contain R 2.15. Examining the error log files led me to believe that the installer's attempt to verify the R version inappropriately navigated to the "Current" version (aliased to a specific version, likely not R 2.15 if 2.15 was installed manually), even if the directory for 2.15 was specifically entered into the installer. It then seeks a couple of i386 subdirectories that no longer exist as of R 3.2.2. So (for my case) I manually created "/Library/Frameworks/R.framework/Versions/3.2/Resources/bin/exec/i386" and copied version 2.15 of the R unix executable, and did similarly for "/Library/Frameworks/R.framework/Versions/3.2/Resources/etc/i386", copying in the ldpaths file that the installer seeks. Doing so finally allowed the SPSS Essentials for R installer to complete. In retrospect, temporarily re-aliasing "Current" in the R.frameworks folder might have been a more direct approach, but I haven't tried that as of right now.
For what it's worth, I was able to install an older version of R (for a different purpose, but I found this page while trying to figure it out), without having to temporarily change the version of OS X (or macOS) on my system. That seemed dangerous to me, and I wanted a simpler fix.
What worked for me was extracting the installer pkg, modifying the version check in there, and then packaging it up again.
Downloading the pkg installer from CRAN
open Terminal and cd to the directory where the installer is: e.g.,
cd ~/Downloads
use pkgutil to expand the pkg file you downloaded
pkgutil --expand R-2.15.0.pkg R-2.15.0
This creates a folder named R-2.15.0 (You can name it whatever you want in the command above). In that folder is a text file called Distribution.
Open the Distribution file in a text editor (e.g., TextEdit), and
modify the function at the beginning that checks the version of OS
X. for example, if it requires Leopard, there will be a line that
looks like this:
if(!(my.target.systemVersion.ProductVersion >= '10.5.0')) {
...
}
Notice that version string for comparison is a string (not a number!), so any version past 10.9 (10.10 and up) will fail that check ('10.10' sorts between '10.1' and '10.2', which is less than '10.5').
All I did was change the minimum version here ('10.5.0') to '10.1'. So the same line reads:
if(!(my.target.systemVersion.ProductVersion >= '10.1')) {
Save the Distribution file.
Go back to terminal and re-package the directory you created in step 3:
pkgutil --flatten R-2.15.0 R-2.15.0-HACKED.pkg
You can call the new pkg whatever you want, as long as you will recognize it.
The resulting package (R-2.15.0-HACKED.pkg, in this example) will now run on any version of OS X higher than or equal to the version you specified (in this case, 10.1 and up).
Disclaimer: I actually tested this with R-2.12.0 (and it worked), but I provided example code for R-2.15.0 to answer the original question. It should be similar with any R installer pkg for OS X that uses a similar version string to check for compatibility before installation.
I have a huge raw data file which I do not intend to change or copy. And I have two projects in RStudio and both need to be able to access it.
I originally created the alias (in MacOS) in the following way
right click the file ~/A/data.csv in finder, and click "make alias". Then copy the alias to ~/B/ and rename it ~/B/data.csv
I also tried the following command later
ln -s ~/A/data.csv ~/B
For project A, I put the actual data file in A/data/data.csv.
For project B, I created an alias under B/data/.
But when I try fread('B/data/data.csv'), it complains:
sh: ./data/data.csv: Too many levels of symbolic links
Error in fread("./data/data.csv") :
File is empty: /var/folders/4h/2jg64xk52mv3fyq4sb7s371w0000gn/T//Rtmp7cWNN3/filebf3013ad9194
I think I can use a hard link to solve this issue, but just want to see if I can use alias to make it work.
=====
I don't think it matters, but for completeness, see the following for my OS and R version:
platform x86_64-apple-darwin10.8.0
arch x86_64
os darwin10.8.0
system x86_64, darwin10.8.0
status
major 3
minor 1.0
year 2014
month 04
day 10
svn rev 65387
language R
version.string R version 3.1.0 (2014-04-10)
nickname Spring Dance
I'm not entirely sure why using aliases in this specific case:
note that for small files (e.g. < 1 MB), the alias can have a way bigger memory footprint. For example, for a simple text file containing "test" (echo "test" > test.txt) the alias will be 274k times bigger:
test.txt: 5 bytes
test.txt alias: 1372636 bytes
since RStudio is good at using absolute paths, why not directly link to ~/A/data.csv directly rather than linking to it's alias?
Two alternative solutions (not directly answering question) could be to (1) copy the file or (2) create a kind of symbolic link.
Copying the file
#!/bin/bash
mkdir ~/B/data/
cp ~/A/data.csv ~/B/data/
Or in R, using system (on Mac):
system("mkdir ~/B/data/")
system("cp ~/A/data.csv ~/B/data/")
Creating a symbolic link
This can be done by simply saving the path of the file ~/A/data.csv in ~/B/data/.
In shell:
#!/bin/bash
mkdir ~/B/data/
echo "~/A/data.csv" > ~/B/data/data.csv
(this part can be also done in R using system() as above)
And then, in R:
## Reading path in B/data/
PATH <- scan(file = "~/B/data/data.csv", what = character())
## Opening the file (~/A/data/data.csv)
my_csv <- read.csv(PATH)
How to get current working dir?
I guess there should be a command like getcwd(), however, I can't find something like this in the documens.
How to change to another dir?
How to set default working dir when I start R?
version
_
platform x86_64-w64-mingw32
arch x86_64
os mingw32
system x86_64, mingw32
status
major 3
minor 1.0
year 2014
month 04
day 10
svn rev 65387
language R
version.string R version 3.1.0 (2014-04-10)
nickname Spring Dance
Thanks!
It is getwd()
It is setwd("path/to/new/dir")
Either via a Windows property, or via .Rprofile etc, or (as I recall) by defining $HOME which Windows does not set by default. See help(Startup).
As a complement to #Dirk's answer, I would like to add something that might be useful and is frequently overlooked.
It is possible to save the current working directory and set the new one at the same time.
Function setwd returns the working directory so all you have to do is to save its value. This can be useful if you, at a later moment, need to return to the original working directory.
old_dir <- setwd("/path/to/new/dir")
#[run the code you want]
setwd(old_dir)