How to run an R script and show a plot? - rscript

How to wait for a keypress in R?
I am attempting to construct a simple script, which creates a graph; closes it when a key is pressed. My code:
#!/usr/bin/Rscript
library(tidyverse)
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy), colour = "blue")
I have tried the answers from the linked question to pause the script after the plot is drawn. However:
No plot appears, alto the code works fine from the interactive R shell.
readline() works only in interactive mode, as per the documentation, and scan() is line-buffered, instead of reacting to a single keypress
How does this work? My environment is Debian.

It seems the problems I was trying to solve is non-existent. The mentioned above functions have a very good reason not to work in non-interactive mode - they are not needed.
Long story short, ggplot2 automatically exports plots to a .pdf when run in non-interacive mode!

Related

Delete current terminal input line

I am trying to code a little console chat-program in C on linux.
So far I coded it in a way that both chatting partners are only able to alternately send/recv, because these function calls are blocking by default.
Now I would like to modify that program, so that both are able to send and receive simultaneously.
The problem that I find is, that once you typed some input to the terminal, I don't know how to output received messages, without messing up the current input line of the terminal.
If there was a way to delete that current input line, you could temporarily save that line, print the new message and put the input line right back.
However, I was not able to find a solution for this problem on the internet.
Is it possible to delete the current input line, and if not, how else could I achieve what I want?
I think you should look into ncurses as Edd said in his comment.
It would allow you to easily manage contents in your terminal window, which sounds like a good idea for your chat program.
All you'd need to do is store your messages in 2 character arrays:
char incoming[MSG_MAX]
and
char outgoing[MSG_MAX]
Then you can output those messages wherever you want in your terminal window, since ncurses allows you to specify x,y coordinates on where to put your text.
Then a simple wrapper for one of ncurses erase() family functions would allow you to delete characters from specify x,y coordinates in your terminal window.
Edit: MSG_MAX is not an actual ncurses macro.

Getting the console color from a child process

I'm trying to write a Windows-equivalent to the watch command-line tool in C.
For spinning up a child process and getting its output, I'm using the approach described in this article.
This works ok for me, but now the output has no colors. For example, a regular git status will use green and red to indicate staged and unstaged changes. However, when run as watch git status it's all white text.
I'm aware that I can avoid this problem by not redirecting the output. This is not an option however, since I want it to be able to buffer it in my program and potentially manipulate it, before clearing the console and displaying it.
Is it possible to capture the color information from the child process, so I can replicate it in my own output - and if so, how?

Why does the terminal show "^[[A" "^[[B" "^[[C" "^[[D" when pressing the arrow keys in Ubuntu?

I've written a tiny program in Ansi C on Windows first, and I compiled it on Ubuntu with the built-in GCC now.
The program is simple:
read the line from console with scanf().
Analyze the string and calculate.
But something weird happens. When I try to move the cursor, it prints four characters:
pressing Up prints "^[[A"
pressing Dn prints "^[[B"
pressing Rt prints "^[[C"
pressing Lt prints "^[[D"
How can this be avoided?
Why does it print these 4 characters instead of moving the cursor?
Because that's what the keyboard actually sends to the PC (more precisely, what the terminal prints for what it actually receives from the keyboard). bash for example gets those values, deciphers them and understands that you want to move around, so it will either move the cursor (in case of left/right) or use its history to fetch previous commands (up/down). So you can't expect your program to magically support arrow keys.
However, reading from standard input from the terminal already supports left/right arrow keys (I believe, but I'm not in Linux right now to test and make sure). So my guess is that there is another issue interfering. One possible cause could be that one of your modifier keys is stuck? Perhaps ALT, CTRL or SUPER?
For those who are coming from the osx (mac) try changing the shells to bash
Terminal -> Preferences -> Shells open with -> [select] Command (complete path)
then paste
/bin/bash
This might be because the user account is created in shell. You can change it to bash by two ways.
Permament solution is -
sudo chsh -s /bin/bash ${username}
To get this solution working you will have to logout and login
Temporary solution is everytime when you login into the ubuntu server type bash and hit return.
If it's under a docker container, run /bin/bash . This helped me solve the problem.
Additionally to what Shahbaz mentioned, I realized that pressing enter (thus sending an empty command) can fix the problem. This is usually necessary after using CTRLC to cancel a command.
On MacOS Terminal for me was enough to uncheck "Scroll alternate screen" for the issue to disappear. See screenshot of the preferences below.
You can (re)bind keys. Add this at the bottom of your .profile, .zshrc or whatever shell config you have.
bindkey -e
bindkey '\e\e[C' forward-word
bindkey '\e\e[D' backward-word
i think simple way is we can just do
kill %%
because this sometimes happen because of background processes.

Opening a .hs file with ghci in Terminal?

I'm using Mountain Lion. I open the terminal, then I load ghci, I write :l and then I try to load my file (which is in my desktop) by dragging it with the mouse from my desktop to the terminal, so I know that the location is correct and I get this, thank you in advance:
Prelude> :l /Users/myusername/Desktop/Test.hs
[1 of 1] Compiling Main ( /Users/myusername/Desktop/Test.hs, interpreted )
/Users/myusername/Desktop/Test.hs:1:7: parse error on input `\'
Failed, modules loaded: none.
Prelude>
Edit: The file Im trying to open (written in Text Edit) is:
double :: Int -> Int
double x = x + x
TextEdit is not a plaintext editor (unlike e.g. Windows Notepad), so by default it will include formatting junk in your files that GHC obviously isn't happy about. Apparently you can still use TextEdit if set up correctly, but it's quite recommendable to use a proper programming editor. Like any Unix, OSX comes with a vi flavour, which takes some time to get used to but isn't that hard and works fine; at least you can use it to check what's really in your file.
vi /Users/myusername/Desktop/Test.hs
or, even simpler
cat /Users/myusername/Desktop/Test.hs
will just give you the exact contents of your file.
For the choice what editor to use best, consider this question.
I tried the same procedure on Windows and it worked perfectly.
Have you tried to go to the directory inside GHCi and open it?
The procedure would be:
Prelude> :cd /Users/myusername/Desktop/
Prelude> :l Test.hs
For me, copying/pasting the code you have posted, both situations worked on Windows.
The presence of a \ implies you've got an RTF (rich text) file from TextEdit. RTF is TextEdit's default, and it's a format that annotates plain text with information about text font, size, etc.
I'd recommend vi or emacs, but to fix your immediate problem, open the file in TextEdit and hit Cmd-Shift-t to convert your file to plain text.

Silverlight 4 - send text to Notepad

I try this tutorial
http://elegantcode.com/2010/02/20/silverlight-4-com-interop-and-the-cool-stuff-you-can-do-with-it/
I need open Notepad and send text into. I use this code:
using (dynamic shell = AutomationFactory.CreateObject("WScript.Shell"))
{
shell.Run(#"C:\windows\notepad.exe"); //you can open anything
shell.SendKeys(txtTextToSend.Text);
}
When I start aplication the notepad has appeared but there is no text in it.
What do I wrong. My system is W7 64bit.
Thanks
The problem is that Run is a non-blocking operation that will return as soon as it has spun up the process. The UI for notepad probably hasn't appeared and put the focus into its input area by the time your code is calling SendKeys.
Try introducing a Thread.Sleep for a couple of seconds to prove whether this is true.

Resources