r system doesn't work when trying 7zip - r

Hi I'm trying to use R to control unzipping a file.
I've added 7z into PATH, and did
7z e hat.2015-09-26T01-10-02.gz
and it worked.
Now in R, i'm in the same directory, and i tried
> command1 = paste0('7z e ', drop.file)
> command1
[1] "7z e hat.2015-09-26T01-10-02.gz"
> system(command1, intern=T)
Error in system(command1, intern = T) : '7z' not found
> system2(command1)
Warning message:
running command '"7z e hat.2015-09-26T01-10-02.gz"' had status 127
> shell(command1)
'7z' is not recognized as an internal or external command,
operable program or batch file.
Warning messages:
1: running command 'C:\Windows\system32\cmd.exe /c 7z e hat.2015-09-26T01-10-02.gz' had status 1
2: In shell(command1) :
'7z e hat.2015-09-26T01-10-02.gz' execution failed with error code 1

It's little bit raw, but try this out (:
Only Windows. It's uses CMD
ZiparEm7zip = function(sQualPasta)
{
sWDTava = getwd()
setwd(dirname(sQualPasta))
sQuem = gsub("[/]", "\\\\", sQualPasta)
dirname(sQuem)
NomeArquivo = paste0("eufaco7zip",".bat")
sNomePasta7zip = basename(sQuem)
sArquivoSaida = basename(sQuem)
sQualPasta7zip = sQuem
if(dir.exists("C:/Program Files/7-Zip/"))
{
sTexto = "set PATH=%PATH%;C:\\Program Files\\7-Zip\\"
}else if(dir.exists("C:/Program Files (x86)/7-Zip/"))
{
sTexto = "set PATH=%PATH%;C:\\Program Files (x86)\\7-Zip\\"
}else
{
stop("Error, o 7zip not installed?")
}
sTexto2 = paste0("\npushd ",sQualPasta7zip)
sTexto3 = paste0("\n7z a -r ../",sArquivoSaida," *")
sTexto4 = '\n( del /q /f "%~f0" >nul 2>&1 & exit /b 0 )'
cat(sTexto,file=NomeArquivo,append = TRUE)
cat(sTexto2,file=NomeArquivo,append = TRUE)
cat(sTexto3,file=NomeArquivo,append = TRUE)
cat(sTexto4,file=NomeArquivo,append = TRUE)
shell.exec(NomeArquivo)
while(file.exists(NomeArquivo))
{
Sys.sleep(10)
}
setwd(sWDTava)
return(TRUE)
}
Try like:
ZiparEm7zip("F:/MYFOLDER")

Related

unix - sed replace file of file with error undefined label

I'd like to replace the entire line
TEMPLATE_FILE = file.tpl with
TEMPLATE_FILE = tester.sh
I have a file called file.config
test0
TEMPLATE_FILE = file.tpl
test1
test2
OUTPUT_DIR = out
I would like the replace
TEMPLATE_FILE = file.tpl to TEMPLATE_FILE = tester.sh
OUTPUT_DIR = out to OUTPUT_DIR = in
sed -i 's/(OUTPUT_DIR).*/(OUTPUT_DIR = in)/g' file.config
I get this error
sed: 1: "file.config": undefined label 'pc.config'
The end result would be
test0
TEMPLATE_FILE = tester.sh
test1
test2
OUTPUT_DIR = in
Any help would be greatly appreciated.
sed -i -E "s/(TEMPLATE_FILE = file.tpl)/TEMPLATE_FILE = tester.sh/" bpc2.config
Judging from the error message, you may using MacOS. Then the -i option requires a string as an argument. Would you please try:
sed -E -i '' 's/(TEMPLATE_FILE).*/\1 = tester.sh/g; s/(OUTPUT_DIR).*/\1 = in/g' file.config

The functions system or shell in R Studio do not work on my string on Windows 10. Running command had status 1

When I run the string below in my command line, it works fine. But when I run it from R, it does not.
I run this:
>cmd <- "python -c 'import sys; import pandas; import scrublet; df = pandas.read_csv(\"C:\\Users\\User\\AppData\\Local\\Temp\\RtmpOmMtOp\\file383068ca4fa\"); scrub = scrublet.Scrublet(df); doublet_scores, predicted_doublets = scrub.scrub_doublets(); pandas.DataFrame(doublet_scores).to_csv(\"C:\\Users\\User\\AppData\\Local\\Temp\\RtmpOmMtOp\\file383068ca4fa.doubletScores\");'"
tmp <- system(cmd, intern=T)
Warning message:
In system(cmd, intern = T) :
running command 'python -c 'import sys; import pandas; import scrublet; df = pandas.read_csv("C:\Users\User\AppData\Local\Temp\RtmpOmMtOp\file383068ca4fa"); scrub = scrublet.Scrublet(df); doublet_scores, predicted_doublets = scrub.scrub_doublets(); pandas.DataFrame(doublet_scores).to_csv("C:\Users\User\AppData\Local\Temp\RtmpOmMtOp\file383068ca4fa.doubletScores");'' had status 1

convert tex to pdf using texi2pdf in Rstudio

I got error msg when converting tex to pdf using texi2pdf function in Rstudio.
tools::texi2pdf('test.tex')
and the error msg I got:
Error in system(paste(shQuote(texi2dvi), if (quiet) "--quiet" else "", :
(converted from warning) running command '"C:\CTEX\MiKTeX\miktex\bin\texi2dvi.exe" --quiet --pdf "test.tex" --max-iterations=20 -I "C:/Program Files/R/R-3.5.1/share/texmf/tex/latex" -I "C:/Program Files/R/R-3.5.1/share/texmf/bibtex/bst"' had status 1
1: tools::texi2pdf("test.tex")
2: texi2dvi(file = file, pdf = TRUE, clean = clean, quiet = quiet, texi2dvi = texi2dvi, texinputs = texinputs, index = index)
3: system(paste(shQuote(texi2dvi), if (quiet) "--quiet" else "", if (pdf) "--pdf" else "", shQuote(file), extra), intern = TRUE, ignore.stderr = TRUE)
4: .signalSimpleWarning("running command '\"C:\\CTEX\\MiKTeX\\miktex\\bin\\texi2dvi.exe\" --quiet --pdf \"test.tex\" --max-iterations=20 -I \"C:/Program Files/R/R-3.5.1/share/texmf/tex/latex\" -I \"C:/Program Files/R/R
5: withRestarts({
.Internal(.signalCondition(simpleWarning(msg, call), msg, call))
.Internal(.dfltWarn(msg, call))
}, muffleWarning = function() NULL)
6: withOneRestart(expr, restarts[[1]])
7: doWithOneRestart(return(expr), restart)
I am running windows10, R version 3.5.1 , RStudio 1.1.463 and CTeX_2.9.2.164.
Anyone could help me with this problem? Thanks.

run system() command from R script

I have R, R Studio and R-tools installed on Windows 7.
I am trying to: cd into a directory and execute a command, this is what I am getting from R, it returns M, what does that mean ?
> system(paste("cmd.exe", input = "cd c:/Users/X123/Desktop/FASTLOAD/", "&& fastload < load_journey.fl > output.txt", sep=" "))
M
>
what am I doing wrong ? I never see the command executed, thanks!
I changed it from system() to shell() and it works now, but get a bunch of warning messages, is that ok or should I do something for them to go away ?
> shell(paste("cd c:/Users/X123/Desktop/FASTLOAD/ ", "&& fastload < load_journey.fl > output.txt", sep=" "))
Warning messages:
1: running command 'C:\Windows\system32\cmd.exe /c cd c:/Users/X123/Desktop/FASTLOAD/ && fastload < load_journey.fl > output.txt' had status 4
2: In shell(paste("cd c:/Users/X123/Desktop/FASTLOAD/ ", "&& fastload < load_journey.fl > output.txt", :
'cd c:/Users/X123/Desktop/FASTLOAD/ && fastload < load_journey.fl > output.txt' execution failed with error code 4
>

find the number of lines in a file

Under Linux, I can find the number of lines in a file by doing a system call to wc:
CountLines <- function(file) {
count.file <- system(sprintf("wc -l %s", file), intern = TRUE)
count <- as.integer(strsplit(count.file, " ")[[1]][1])
return(count)
}
How can I do this efficiently under Windows? By "efficient" I mean fast and light on resources, as I may be using it on large files.
As much as possible, I'd prefer a solution that does not require installing extra packages or tools.
Take a look at this link:
https://isc.sans.edu/diary/Finding+Files+and+Counting+Lines+at+the+Windows+Command+Prompt/2244
The last line works for me:
c:\> type c:\windows\win.ini | find /c /v "~~~"
# 32
Update:
if you want to use it as R function, try this:
CountLines <- function(file) {
stopifnot(file.exists(file))
unlikely.pattern <- paste(sample(LETTERS), collapse = "")
cmd <- sprintf('type %s | find /c /v "%s"', file, unlikely.pattern)
res <- shell(cmd, intern = TRUE)
return(as.integer(res))
}
CountLines("c:\\windows\\win.ini")
[1] 32
.
.
.
I found another way to do this more efficient, but I leave this to you for perfection:
> system("POWERSHELL Get-Content c:\\windows\\win.ini | Measure-Object -word -line -character", intern=TRUE)
[1] ""
[2] " Lines Words Characters Property "
[3] " ----- ----- ---------- -------- "
[4] " 32 38 414 "
[5] ""
[6] ""

Resources