I want to read in a file with fread in R. I have 7z installed.
I tried
fread(shell(cmd = '7z l test.txt.gz'), shell = 'cmd.exe'))
However I get the error
Error in fread(shell(cmd = paste0("7z x ", "\"", dest, "\""), shell = "cmd.exe")) :
'input' must be a single character string containing a file name, a command, full path to a file, a URL starting 'http[s]://', 'ftp[s]://' or 'file://', or the input data itself
I'm looking for something similar to:
fread(shell(cmd = 'unzip -cq test.zip', shell = 'cmd.exe'))
One work-around is to copy 7z.exe to the project folder. And try:
DT = fread(cmd = '7z e -so "test.zip"')
Some people suggested adding 7-zip to Windows PATH Environment Variable. But it did not work for me.
Related
I have a file called goodfile. Lets say the contents are
badline
goodline
badline
goodline
badline
badline
On a windows machine I want to filter this file to get only the "goodline"s before reading it to save on memory costs. Thankfully, the rtools installation comes with grep that should allow me to do that. I should be able to do
if(!pkgbuild::has_rtools()){
stop('install rtools')
}
rtoolsPath = pkgbuild::rtools_path()
grep = file.path(rtoolsPath,'grep.exe')
command = paste(grep, "goodline goodfile")
system(command)
and get
goodline
goodline
However when I try to pipe the output to a file by doing
command = paste(grep, "goodline goodfile > betterfile")
system(command)
I get
goodfile:goodline
goodfile:goodline
/usr/bin/grep: >: No such file or directory
/usr/bin/grep: betterfile: No such file or directory
This error message and the "betterfile" is not generated.
If I take the same command and run it on my command line, it just works, if I do the same system call with regular grep on R in linux machine it just works, so I can't see what the problem is.
I was able to find an alternative way to get the file by doing
system2(grep,
args = c('goodline','goodfile'),stderr = 'betterfile',stdout = 'betterfile')
but still curious why the pipe doesn't work
I wish to call an executable file from Julia via Base.run (documented here) and pass command line options to that executable, but I can't figure out how to do that. In my specific example the executable is Notepad++ and the command line options are
-alwaysOnTop -nosession
This example code works, but doesn't pass the command line options:
function open_file_in_notepadpp()
exepath = "C:/Program Files (x86)/notepad++/notepad++.exe" #Default location on 64 bit Windows
command_line_options = "-alwaysOnTop -nosession "
filetoopen = "c:/temp/foo.txt"
Base.run(`$exepath $filetoopen`, wait = false)
end
I've tried incorporating command_line_options a fair number of ways using backticks, double quotes etc. to no avail, so for example neither of the lines below work:
Base.run(`$exepath $filetoopen`, `$command_line_options`,wait = false)
Base.run(`$exepath $command_line_options $filetoopen`,wait = false)
In the Windows Command Prompt the following works correctly:
"C:/Program Files (x86)/notepad++/notepad++.exe" -alwaysOnTop -nosession "c:/temp/foo.txt"
Could someone explain what I'm missing?
If you substitute a string that contains spaces to a command it will get quoted. Hence, your command line arguments will be quoted and you get
julia> `$exepath $filetoopen $command_line_options`
`'C:/Program Files (x86)/notepad++/notepad++.exe' c:/temp/foo.txt '-alwaysOnTop -nosession '`
I guess what you really need is
julia> command_line_options = ["-alwaysOnTop", "-nosession"]
2-element Array{String,1}:
"-alwaysOnTop"
"-nosession"
julia> `$exepath $filetoopen $command_line_options`
`'C:/Program Files (x86)/notepad++/notepad++.exe' c:/temp/foo.txt -alwaysOnTop -nosession`
Running the latter with run should work. Unfortunately I can't test it on my machine.
crstnbr's answer was correct, but he was unable to test on his machine. Here is the corrected code:
function open_file_in_notepadpp()
exepath = "C:/Program Files (x86)/notepad++/notepad++.exe" #Location if one follows the defaults in the notepad++ installer on 64 bit Wndows
command_line_options = ["-alwaysOnTop", "-nosession"] #Use an array to prevent the options being quoted
filetoopen = "c:/temp/foo.txt"
Base.run(`$exepath $filetoopen $command_line_options`,wait = false)
end
How can I rename a directory from R?
I tried:
Warning message:
In file.rename(from = sprintf("content/%s-content", pu_name), to = sprintf("content/%s", :
cannot rename file 'content/pu.train2-content' to 'content/train2', reason 'Directory not empty'
You could use shell in order to pass an OS specific command to change it:
shell(paste('rename',
sprintf("content/%s-content", pu_name),
sprintf("content/%s", other_name)))
And it should work without needing an empty directory.
I'd like to create a soft symbolic link to a file from within R on Windows (with Mklink). It fails because I cannot tell R to "run it as administrator". Is there any way I can do this?
I did manage to create hard symbolic file links, however:
path_src <- file.path(tempdir(), "test.txt")
write("Hello World!", file = path_src)
path_tgt <- file.path(tempdir(), "test_symlink.txt")
shell(sprintf("mklink /H %s %s",
normalizePath(path_tgt, mustWork = FALSE),
normalizePath(path_src)
))
Note how the file at path_tgt reflects the changes made to path_src:
write("HELLO WORLD!", file = path_src, append = TRUE)
Yet, this fails:
path_tgt_2 <- file.path(tempdir(), "test_symlink_2.txt")
> shell(sprintf("mklink /D %s %s",
normalizePath(path_tgt_2, mustWork = FALSE),
normalizePath(path_src)
))
Ihre Berechtigungen reichen nicht aus, um diesen Vorgang auszufhren.
Warning messages:
1: running command 'C:\Windows\system32\cmd.exe /c mklink /D C:\Users\Thyson\AppData\Local\Temp\Rtmpum73ZU\test_symlink_2.txt C:\Users\Thyson\AppData\Local\Temp\Rtmpum73ZU\test.txt' had status 1
2: In shell(sprintf("mklink /D %s %s", normalizePath(path_tgt_2, mustWork = FALSE), :
'mklink /D C:\Users\Thyson\AppData\Local\Temp\Rtmpum73ZU\test_symlink_2.txt C:\Users\Thyson\AppData\Local\Temp\Rtmpum73ZU\test.txt' Ausführung mit Fehlerkode 1 fehlgeschlagen
Note
Due to a German version of Windows I can't seem to get the errors in English. The first line translates to somewhat along the lines of "You don't have enough authorization in order to carry out this process"
Run R as administrator. Then when you run "Mklink" from within R, you are the administrator.
Actually, you can also use the R function file.symlink to create symbolic links.
Since 2016 (Windows 10 Insiders build 14972), you can also make symbolic links without administrator rights if you enable the developer mode (in Settings>Update and Security>For developers).
This works in the Windows console with mklink, but it doesn't seem that R's file.symlink() works (as of R 4.0.3), possibly because of the dwFlags that needs to be set.
For calling it from R, I needed to add " when the path contains spaces:
shell(sprintf('mklink "%s" "%s"',
normalizePath(link, mustWork = FALSE),
normalizePath(original)
))
For directories, you can also consider the use of Sys.junction() which is somewhat equivalent to symlinks.
When I want to load a file into R, what would be a way to do so programmatically (or semi programmatically) instead of just manually copy paste the path and switch \ to / so R could read it (because \ is an escape character in R)
One method mentioned by #Roland is using scan
list.files(scan(file=file(description = "clipboard"), what = "character", allowEscapes = FALSE))
Which will need modifications if the path contains a space like
"C:\Users\David Arenburg\"
normalizepath maybe what you are looking for:
> normalizePath(c(R.home(), tempdir()))
[1] "C:\\Program Files\\R\\R-3.1.0"
[2] "C:\\Users\\john\\AppData\\Local\\Temp\\RtmpysKuFi"
> normalizePath(c(R.home(), tempdir()), winslash = '/')
[1] "C:/Program Files/R/R-3.1.0"
[2] "C:/Users/john/AppData/Local/Temp/RtmpysKuFi"
Use readClipboard if you are on windows and have problems copying and pasting filepaths. For example I copy a file path from a windows folder so it is on the clip board then I can use:
> readClipboard()
[1] "C:\\Users\\john\\AppData\\Local\\Temp"
You can then use `normalizePath to correct the windows flavour of paths.
Perhaps use choose.files() to pick the file followed by normalizePath() if you want to convert its path name to R's forward slashes.