I want to do these steps in a macro:
Open Rstudio if it's not open yet.
Activate Rstudio
Put a command into the Rstudio's console.
Hit enter, telling Rstudio to run the given command.
I can do (1) and I can theoretically do (3) with Sendkeys if I can do (2), but (2) isn't working.
These two lines are the basic idea but using notepad instead:
PID = Shell("C:\Windows\system32\notepad.exe", vbNormalFocus)
SendKeys "source('C:/Users/Athena/Documents/helloworld.r')", True
Here a step (2) isn't even necessary since VBA sends the string in sendkeys directly into notepad like how I want. But if I try the same code with RStudio, the string gets pasted into the VBA code instead (or into the excel book if I used a button to activate the macro).
So in my mind, I want to force VBA to put focus on RStudio, then sendkeys will work as intended, and then I can do (4) somehow, telling RStudio to run the code I sent it.
Related
I try using a very light-weighted editor BowPad to edit and run codes.
Take running R as an example, which has been added to Windows PATH Environment Variable D:\R\R-3.6.2\bin\x64
I try to take some following steps:
I built a test.r writing some testing line codes as 1+2; plot(1:10)
Menu --> Run --> Configure custom commands
Command Name: run R
Command Line: D:\R\R-3.6.2\bin\R.exe $(SEL_TEXT)
$(LINE) The line where the cursor is
$(POS) the position where the cursor is
$(TAB_PATH) the path to the file of the active tab
$(TAB_NAME) the file name without extension of the current tab
$(TAB_EXT) the file extension of the current tab
$(TAB_DIR) the directory of the file of the active tab
$(SEL_TEXT) the selected text or the word at the cursor position
$(SEL_TEXT_ESCAPED) like $(SEL_TEXT) but escaped, useful for urls
When I press to choose run R to run the code, something wrong happens!
It will open a Rterm(64-bit) window each time when I run R!
it shows RGUMENT '1+2' __ignored__ or ARGUMENT 'plot(1:10)' __ignored__
Did I missing something ? BTW, can we set some shortcuts keys for running Python, R, Octave or Julia?
Also, when I reach the homepage of BowPad for more information, little usage of this software can be found!
R.exe refers to Rterm. You can use Rgui.exe instead but that won't automatically process arguments. I think what you likely want is to send the selected text to a running R session rather than starting up a new R session each time.
To do that
download this file and optionally place it on your path https://raw.githubusercontent.com/ggrothendieck/batchfiles/master/clip2r.js
in the Bowpad Run | Configure window configure a Run R command as the following (or if clip2r.js is not on your path use the entire pathname). It should be just like this with no arguments.
clip2r.js
From the Windows cmd line if Rgui is running we see that tasklist | findstr Rgui finds Rgui as the R gui process and you can create additional js scripts by editing clip2r.js replacing Rgui in the js script with whatever is the appropriate word to locate python, julia, octave or other R front end assuming again that they are running.
Now to invoke it from within Bowpad:
ensure that your Rgui session is already running and
from within Bowpad select the code you want to run and copy it to the clipboard and then invoke Run R. For example, to run everything use ctrl A ctrl C ctrl R 0 assuming that Run R is in position 0 in the configure menu. Alternately use the mouse with the Ribbon.
Regarding the comment about difficulty finding help, it looks like Bowpad is based on Scintilla so the documentation for it and the related SciTE editor likely apply to Bowpad too. You can also check the Bowpad source code on github.
I am trying to run code in R and I have repeatedly run into the very frustrating issue that, unlike in Terminal when you can just press control-C on the keyboard, or Python when you can just enter exit(), in R it does not appear to be possible to exit a command you are currently typing to get back to the basic command line-type interface. That is, when I'm typing some code in R, I have a bunch of plus signs to the left of what I'm typing rather than > symbols, and, rather than execute what I have typed, I want to exit it and return to having > symbols and thus be able to type a whole new command while keeping the R window and workspace open, I find it impossible to do so. Instead I end up having to just close the entire R window, save the workspace, and reopen it which is way more steps than I'm sure are necessary. I already looked around online trying to find a way to exit R commands in the situation I described, and I found:
1. "stopifnot("something that is not true")", which worked the first time I tried it but not the second (why? no idea)
2. "stop("string")", which did nothing when I tried it
3. "quit()", which also did nothing
ctrl+R no longer works for executing script lines.
This is not a hardware/keyboard problem.
I have also restarted my PC.
I have tried on a different PC.
I have recently switched from using R to using RStudio. I thought this may have something to do with it, so I opened and resaved the script in RStudio, to no effect.
Furthermore, I have created an R-Project folder and have copied the files, including the script in question, into it. Then I started R-Studio and opened the project.
I would like to post my sessionInfo(), but do not know how to do that without executing the command.
Keep in mind that I only use R for stats purposes. I don't know much about informatics or other types of programming etc., so please try to keep it simple for me. Thanks!
In addition to the solution offered above, in Rstudio, Ctrl + Enter does not work if the chunk is broken in .Rmd files.
For example, if you press CTRL + Enter on the following line (2+2), it won't work:
```{r}
2+2
``
The chunk should end three Backtick characters, not two.
The problem was that the script file (for some reason unkown to me) did not have the correct extension (.R). When I added that to the script file extension, it worked fine again.
Since I have started using R I have noticed inconsistent behaviours when pasting a complete single command line in the R GUI console. Sometimes the command is executed, sometime is not. I tried to use the "paste commands only" but the command is still executed and the option does not seem to have any effect. I want to past a complete command and not execute it.
How do I control what happens when I paste a single line?
EDIT: i think the issue is that I am also copying from notepad the "enter" character, so that's what make the command run right away. Is there a way to avoid this?
If you do not copy a newline character at the end of the command, it will not be executed when you paste it. You will need to enter one manually.
Rather than copy/paste, you might want to use an editor where you can highlight the code you want to run and then send it to the R console via a button or shortcut key. The Windows R GUI has this feature, as does Rstudio.
I am running a set of selected code on R. Like
source("/tmp/r-plugin-honli/Rsource-2704-quantmod.R")
There is no output. Only the prompt '>' flickered there.
I use 'killall' to kill the R session. But I don't know where is wrong on the code. Because R did not give any output. How could I know what's going on about the code.
I'd try two things:
Run the code interactively. As in, open the Rsource-2704 file and run its lines one by one.
If that doesn't replicate the problem or is not possible, you can take Joshua Ulrich's suggestion or use:
R CMD BATCH --vanilla Rsource-2704-quantmod.R out.log
Which will run the code in a batch mode and output the usual console lines to a file called out.log (you can name it whatever you like).
Instead of using print statements, you could also take a look at the browser() command. This drops you into an interactive session at the point where the command is put. This works particularly well when trying to figure out what is happening inside a function, although I don't know if your script contains them.