I have s script that produces a lot of small PNG files that I want to remove when I close my gWidgets interface. I thought I could do that in Windows using
shell( "del *.png" )
but neither in the script nor in interactive mode in R (2.15.2), this has any effect at all (not even an error or warning). Probably I'm doing something wrong but I can't find out so far what.
Has somebody an idea for me?
I've just tested your command -- same version of R on Windows XP -- and it works exactly as you would expect. If this command is not working for you, I strongly suspect that R's working directory may be different from the directory in which you have your .png files.
You could try:
shell('dir *.png')
... to verify that the .png files are, in fact, in the current working directory before trying to delete them. If they are not there, you will get the report:
File Not Found
Warning messages:
1: running command 'C:\WINDOWS\system32\cmd.exe /c dir *.png' had status 1
2: In shell("dir *.png") : 'dir *.png' execution failed with error code 1
Also, if you've run the del command once, so there are no .png files remaining in the directory, the second time that you run that command you should get an error message like the following:
> shell("del *.png")
Could Not Find C:\usr\sjl\dev\test\R\*.png
Related
shell_exec("Rscript C:\R\R-3.2.2\bin\code.R ");
This is the call to script.On calling the above script, the error occurs.
I am trying to call my R script from the above path but no output is being shown. While checking the error logs of PHP, it says 'Rscript' is not recognized as an internal or external command, operable program or batch file.' The script is working fine on the Rstudio but not running on the command line.
Add the Rscript path to your environment variables in Windows:
Go to Control Panel\System and Security\System and click Advanced System Settings, then environment variables, click on path in the lower box, edit, add "C:\R\R-3.2.2\bin"
Restart everything. Should be good to go. Then you should be able to do
exec('Rscript PATH/TO/my_code.R')
instead of typing the full path to Rscript. Won't need the path to your my_code.R script if your php file is in the same directory.
You need to set the proper path where your RScript.exe program is located.
exec ("\"C:\\R\\R-3.2.2\\bin\\Rscript.exe\"
C:\\My_work\\R_scripts\\my_code.R my_args";
#my_args only needed if you script take `args`as input to run
other way is you declare header in your r script (my_code.r)
#!/usr/bin/Rscript
and call it from command line
./my_code.r
If you are running it in Git Bash terminal, you could follow a revised version of the idea suggested by #user5249203: in the first line of your file my_code.R, type the following
#!/c/R/R-3.2.2/bin/Rscript.exe
I assumed that your path to Rscript.exe is the one listed above C:\R\R-3.2.2\bin. For anyone having a different path to Rscript.exe in Windows, just modify the path-to-Rscript accordingly. After this modification of your R code, you could run it in the Git Bash terminal using path-to-the-code/mycode.R. I have tested it on my pc.
I faced the same problem while using r the first time in VS Code, just after installing the language package (CRAN).
I restart the application and everything worked perfectly. I think restarting would work for you as well.
I have a R script that should change the working directory using setwd().
My code:
setwd(file.path(work_dir, database_dir, study))
I can pause my script just before this line is supposed to execute (using browser()) enter the code into the console without issue, but whenever the line is executed from the script I get the error box stating that there is an "error while opening file: The system cannot find the file specified." Within my script before calling the setwd, I confirmed that the path exists using file.exists(file.path(work_dir, database_dir,study)). I also restarted R studio and closed and reopened all of my scripts to make sure that was not causing the problem.
Does anyone have any other suggestions for me to try?
Posting how I resolved the issue:
The script that changed the directory (I will call this script 2) was called by another script first (I will call this script 1). script 1 and script 2 are in the same directory, but different directories from where I where I was trying to change. script 1 called script 2 using source("script2.R"). When I changed this to source(file.path(work_dir, "script2.R")) I no longer had issues with the setwd() command. My GUESS is that when I changed directories Rstudio "forgot" what script it was supposed to be executing and that is why I got a "no file exists" error.
If anyone has any other thoughts - please share!
I am a absolute beginner with Python. What I have done so far is I have installed Python with IDLE, Pycharm, Pygame-zero and Pyinstaller too. I have a (.py) file game I would like to turn into a (.exe) file. So I can release my game for my friends to play.
I have entered this command into CMD window "pyinstaller --onefile -w gamename.py"* (*My game is not really called "gamename". I have called it that in this code above. For this example only.)
When in the file folder of my game and then Pyinstaller converts my file into the (.exe) file. But everytime I try to run this file. It fails with this error message - "Fatal Error Detected, Failed To Execute Script Error".
I have watched all kinds of YouTube videos trying to solve this problem and have tried these following fixes:
Updated Pyinstaller (4.3).
Updated Python (3.9.5).
Changed the path in Enviroment Variables to point to Python39\Scripts and resetting my computer.
Coverted the (.py) file to (.exe) by including the console window. Then after running the program after when the console window disappears. Opening the CMD window again and typing the file directly "gamename.exe" to run.
Installing auto-py-to-exe program.
Each time deleting the Main, Dis and Spec files and moving the (.exe) file into the main file folder. With the resources for the games. Music, Images and Sounds.
Testing out my Python script to check. If it doesn't have any errors while loading through IDLE and it doesn't. It works straight away, I can play my game through IDLE. There is no errors in the code of my game script.
And after trying all of these solutions it still hasn't solved this issue.I still have the same error message appearing when trying to run my game. Now I have found out what the meaning is to this error message that appears. Which is:
"Fatal Error: failed to execute
This means something has gone wrong as it's giving you a visual warning about it; this is not an error, it's a warning; the real error has been printed to stdout/stderr. If you open the executable using the terminal or something else that will preserve the console output, you will most likely see a Python error telling you what went wrong. Fixing this and repackaging is the solution to this issue".
I have remade the (.exe) file including the console window and it does explan what the error is. When I try to run my game, the error comes up as:
"FileNotFoundError: {Errno 2} No such file or directory: C:*****\Local\Temp_MEI58602\pgzero\data\icon.png {4268} Failed to execute script.
Any suggestions? On how to fix this error please. So I can run and play my game outside of Python on any PC.
A Possible Answer:
I have found a webpage that has the possible answer to my problem. But I don't know what they mean, because I am a beginner with Python. Can anybody read this and break it down for me? Here is what it say's:
"Pyinstaller packaging exe, missing icons and other issues
Reason
When the exe runs, it will decompress a resource folder named "_MEI*" to a temporary directory on the computer, and delete it when the program ends.
uses a path like ‘\icon.png’ in the program. When the exe is running, it will only search for its own directory, of course it cannot be found.
Two, the solution
Make sure the picture is in this temporary resource folder
This can be done by editing the'.spec' configuration file to add pictures.
(Note: .spec is the file generated by Pyinstaller last time, in your python project directory.)
These are the three pictures I used, which is actually adding three tuples to the "binaries" list
Before the comma is the address of the picture in the python project, after the comma is the address of the package into the ‘_MEI*’ temporary folder.
I have built an ‘img’ folder to store pictures in it. Just put a dot in the root directory, such as (’./img/info.png’,’.’)
Finally, run when packagingpyinstaller program entry.spec, You can add the picture resource.
(Note: Other external resources can also be added in this way, such as .ini, .txt, .exe, etc.)
Make sure the program can find this path
Because the name of the temporary directory is different each time, a method is needed to dynamically obtain this path.
The code is presented, and the core statement is ‘os.path.realpath(sys.path[0])’.
Python running effect is as follows:
Package it as an exe, drag it to the cmd window and run it."
Here is the link to the webpage to the article. Because it makes use of screenshots that I can't include on this webpage;
(https://www.programmersought.com/article/94965073850/)
Please read this acticle and break it down for me. It does seem to be explaining the solution to my problem. But what does it all mean? What pictures is he talking about? Please explain.
I am not sure why but PyInstaller doesn't seem to bundle everything needed for Pygame Zero, including that icon.png file. The solution is simple, though. You just need to use pyinstaller --collect-all pgzero --onefile -w <scipt_name>. If your game has sounds, images or anything like that, remember to include those specific folders as well using --add-data <file_or_folder>. Also, make sure your script includes the following lines.
import pgzrun
# GAME CODE HERE
pgzrun.go()
I hope it helps, even though it is a little late.
I'm calling an .exe from R using system("script.exe object").
I get Warning: running command had status 127. I know it means the .exe file has not been found.
I'm on windows. When I use shell instead of system it works like a charm. However, I am designing a Shiny application that will be deployed in a Linux environment (shinyapps.io). This is why I need to use system.
EDIT
On Windows, it works with system(paste("cmd.exe /c", "script.exe object"), intern = FALSE, wait = TRUE) as suggested here. But not when I deploy the app (on Linux).
HINT
Locally on Windows, if I replace system with system2: system2(paste("cmd.exe /c", "script.exe object"), wait = TRUE), it raises the status 127 warning and the output is exactly the same as in my deployed app on Linux.
It's tough to create a reproducible example here but if needed I can try. Please tell me.
Context: basically the .exe is a black box (compiled C++ code) that takes a .txt file as input and outputs another .txt file. I am using R to dump the .txt file to the current working directory, then read back in the .txt file generated by the .exe (created in the current working directory, where the .exe file is stored).
Just add \" could solve you problem, e.g.
> setwd("W:/www/ADemo/")
> system(paste0(getwd(),"/Hi 2.exe"))
Hello, world.
> setwd("W:/www/A Demo/")
> system(paste0(getwd(),"/Hi 2.exe"))
Warning message:
running command 'W:/www/A Demo/Hi 2.exe' had status 127
> system(paste0("\"",getwd(),"/Hi 2.exe","\" "))
Hello, world.
Update:
The 127 error is usually seen when there is a space in the path. One also needs to worry about the input of the application, e.g. "/path A/A 2" --in-path "/home/A/B C/d 123.dta". Here are some update comments:
system(shQuote(paste0(getwd(),"/Hi 2.exe"))) is much more convenient.
At least in R 3.2.4, the manual of system() recommends to use system2() instead to avoid path problem under Win/Linux/OSX/.
Update 2:
For Linux user, I created a function to detect the given file in your working directory is executable or not:
chkpermission<-function(file, mode='0777'){
exe_list <- system("echo `ls -l | grep -E ^-.{2}x\\|^-.{5}x\\|^-.{8}x` | awk '{print $9}'", intern=T)
if(length(exe_list)==0){
stop("no file is executable");
##Make sure you know what you are doing here, add a+x permission:
## if (!(file%in%exe_list)) Sys.chmod(file, mode = mode)
}
return(file%in%exe_list);
}
I've tested it on GNU awk/grep. The 2/5/8 indicates the executable permission of [u/2]ser, [g/5]roup, [o/8]thers., one could change it to meet the requirement.
The problem actually stemmed from the fact that .exe files are executables for Windows only. It does not work out of the box on Linux environments (you can use WINE but in my case it is not possible because I am calling the executable from within R, I don't have any sudo rights or anything on the virtual machine used by the host of my app). So I compiled the c++ code I had using g++ on a Linux virtual machine and used the .out file rather than the .exe.
Then in my R script I just needed these two calls:
system("chmod a+x script.out") # to make Linux understand that the file is an executable
system("./script.out object") # to run the script
I have R script within a plain text file named "TestFile.R".
I am running RStudio. I want to use the Windows console (cmd.exe) to pipe "TestFile.R" directly into the "R Script" editor of RStudio, without launching a new thread of RStudio.
This command in the console does exactly what I want:
rstudio -f TestFile.R
The contents of "TestFile.R" go straight into the RScript editor of the existing thread of RStudio.
It assumes, however, that "TestFile.R" is in the "bin" folder of RStudio, and also that
cmd.exe is running within that folder.
But I want to be able to do this from anywhere on my computer, using a console command like:
pathToRstudio\rstudio -f pathToTestFile\TestFile.R
To give an example, on my computer, this command fails:
C:\"Statistical packages"\RStudio\bin\rstudio -f E:\"my project"\TestFile.R
By trial and error, I discovered these solutions:
1/ omit the "C:\" part
2/ avoid quotation marks in the pathToTestFile.
So this console command works fine:
"Statistical packages"\RStudio\bin\rstudio -f E:\myproject\TestFile.R
Of course, I still am very restricted ; my default folder has to be "C:\", and I cannot have spaces in the path to TestFile.R, even though spaces within the Rstudio path are apparently OK !?
Could somebody please explain to me how to write this command in a way that is completely generic with regard to path specification?
I want to be able to run it from any folder, and have TestFile.R in any other folder. I do not want hassles about folder names containing spaces.