This question already has answers here:
R: building a simple command line plotting tool/Capturing window close events
(1 answer)
Running R Scripts with Plots
(5 answers)
Closed 9 years ago.
I am using Python which calls a plot from R. Python issues the Rscript command. Everything works, except that the plot instantly disappears.
I tried several things on the R side:
par(ask=TRUE)
Sys.sleep(5)
par does not work this way; it will just ignore it.
With sleep the problem is that R will hang the python script for 5 seconds while sleeping, but also that sometimes I want to be able to close the plot immediately: when I do python just keeps waiting until the 5 seconds are over.
Could it be Python related, or is there a solution in R?
Minimum Working Example:
R:
foo.R
plot(1:10)
Sys.sleep(5)
Python:
foo.py
import os
os.system("Rscript foo.R")
Thanks to mathematical-coffee as from the comments and thanks to Dirk for a similar answer in another thread:
> library(tcltk)
> tk_messageBox(message="Press a key")
Related
This question already has answers here:
Percentage chance to make action
(4 answers)
Closed 10 months ago.
I feel this should be an obvious answer, however, I am having issues coding a personal project of mine. How would I be able to have certain lines of code be executed randomly?
Not from the project itself but the principles are still, say for example I would want the following code to be executed every 1/1000 times or so.
print("Lucky!")
How would I exactly be able to do that?
Set a trace function that will have a 1/1000 chance to print:
import random, sys
def trace(frame, event, arg):
if random.random() < 1/1000:
print("Lucky!")
return trace
sys.settrace(trace)
You're welcome to test it with a 1/2 chance.
Generate a random integer in the range [0,1000). Pick any single value in the range, and check for equality to see if you should print.
import random
def lucky():
if random.randrange(1000) == 42:
print("Lucky!")
This question already has answers here:
Pipe output(stdout) from running process Win32Api
(3 answers)
Closed 7 years ago.
How can i read the output of a running console process ? i found a snippet that shows how to do it for a starting process by using ReadFile() on the process handle obtained by CreateProcess(), but my question is, how can i achieve this for a running process ? thanks.
What I have tried is, using OpenProcess() on the Console app (i hardcoded the pid just to test) and then i used ReadFile() on it, but i get gibbrish letters or not showing me anything at all.
Edit: Here's the code i tried, PID is hardcoded just for test.
procedure TForm1.Button1Click(Sender: TObject);
var
hConsoleProcess: THandle;
Buffer: Array[0..512] of ansichar;
MyBuf: Array[0..512] of ansichar;
bytesReaded: DWORD;
begin
hConsoleProcess := OpenProcess(PROCESS_ALL_ACCESS, False, 6956);
ReadFile(hConsoleProcess, Buffer, sizeof(Buffer), bytesReaded, nil);
OemToCharA(Buffer, MyBuf);
showmessage(string(MyBuf));
// ShellExecute(Handle, 'open', 'cmd.exe', '/k ipconfig', nil, SW_SHOWNORMAL);
end;
It's unrealistic to expect to be able to do this. Perhaps it possible to hack it, but no good will come of doing so. You could inject into the process, obtain its standard output handle with GetStdHandle. And read from that. But no good will come of that, as I said.
Why will no good come of this? Well, standard input/output is designed for a single reader, and a single writer. If you have two readers, then one, or both, of the readers are going to miss some of the text. In fact I'd be surprised if two blocking synchronous calls to ReadFile were allowed by the system. I'd expect the second one to fail. [Rob's comment explains that this is allowed, but it's more like first come, first served.]
What you could perhaps do is to create a multi-casting program to listen to the output of the main program. Pipe the output of the main program into the multi-caster. Have the multi-caster echo to its standard output and to one or more other pipes.
The bottom line here is that whatever your actual problem is, hooking up multiple readers to the standard out is not the solution.
This question already has answers here:
Skip the input function with timeout
(2 answers)
Closed 1 year ago.
I want to take a timed input. If the user doesn't give the input under a limited amount of seconds, the program moves on. So, I learned about inputimeout() but even when I am giving the input within the time limit, it just waits for the timeout. (Also I am not able to solve the problem from other similar questions and that is why I decided to mention this problem)
from inputimeout import inputimeout, TimeoutOccurred
try:
something = inputimeout(prompt = 'Enter: ', timeout=5)
except TimeoutOccurred:
print('Time Over')
Output for the above code:
Enter: e
Time Over
Process finished with exit code 0
Even if I give the input within the time limit, it shows Time Over. Can anyone please help me out?
Keeping it simple, it is a module that reads input from the user, but with a twist, it has a timeout placed by the developer, if the program doesn't detect the information from the user, it skips the input.
A simple way to use it would be:
timer = 2
var = inputtimeout(prompt='Enter: ', timeout=timer)
That would give the user 2 seconds to type, you can also increment with a trycatch block to give a message to the user in case of a timeout.
This question already has answers here:
Syntax error on print with Python 3 [duplicate]
(3 answers)
Closed 5 years ago.
I am a bit new to python and
import serial
import time
ser = serial.Serial('COM3', 9600, timeout=0)
while 1:
try:
print ser.readline()
time.sleep(1)
except ser.SerialTimeoutException:
print('Data could not be read')
time.sleep(1)
I installed pyserial. Why such a simple program like this is giving "invalid syntax" error at the line ser.readline(). Why has been python designed like this that it always makes beginners life difficult. Even here at stackoverflow why syntaxing code is so difficult? everyline has to be indented here.Why cant a simple do the job here.Well this is all together a different topic but why such a simple python program is creating errors?????
If you used the python 3.x, you must used the print function with the (), for example, you want print the hello world, you need write:
print("Hello World")
To you code, you need change the print ser.readline() to print(ser.readline())
At Python world you need user four space character for code hierarchy.
This question already has answers here:
How do I make a time delay? [duplicate]
(13 answers)
Closed 8 years ago.
I have a query for a small program that I am running. I was wondering if there was any way to pause a while loop in python for an amount of time? Say for example if I wanted it to print something out, then pause for say (1) second, then print it out again?
There is not really a point to this program I am more doing it for something to do while bored.
I have checked other questions and none seemed to really answer what I was asking.
import time
while ...
print your line
time.sleep(1) # sleep for one second
time sleep method
Yes. There is a function called sleep that does exactly what you want:
while true:
print "Hello!\n"
time.sleep(1)