- Run (once) the R/plumber server:
in R console:
plumber::plumb('plumber.R')$run(port=9000)
or in cmd.exe:
"C:\Program Files\R\R-4.3.1\bin\x64\R.exe" -e "plumber::plumb('plumber.R')$run(port=9000)"
or in powershell:
&"C:\Program Files\R\R-4.3.1\bin\x64\R.exe" -e "plumber::plumb('plumber.R')`$run(port=9000)"
Feel free to change the port if needed, but then modify it in the VBA macro code too. If the "plumber.R" script is not in your current working directory/folder, add the appropriate path to that file.
- Run (as many times as you want) the VBA macro on the selected 1-column 3-cell range in Excel:
- 1st cell should contain the string with the address of the input range in the Excel style e.g. A1:C4
- 2nd cell should contain the string with the valid R code. The input data should be referred in that code as
inputData - 3rd cell should contain the string with the address of the top-left cell of the range where the output from R (assumed to be a data.frame/data.table) will be pasted
With this data:
| A | B | C | |
|---|---|---|---|
| 1 | mycol1 | mycol2 | mycol3 |
| 2 | 101 | abc | 0.5 |
| 3 | 102 | defgh | 0.78 |
| 4 | 103 | ijklmnopq | 0.13 |
running the VBA macro with cells selected (they can be anywhere in the worksheet) with the following contents (text/strings):
A1:C4 |
inputData %>% .[, MyCol4 := mycol1 + 3] %>% within(MyCol5 <- mycol3*10) |
E1 |
(The middle cell contains R code with magrittr pipe and the first operation in the data.table syntax and the second operation in the base-R syntax as examples).
you get the following result:
| A | B | C | D | E | F | G | H | I | |
|---|---|---|---|---|---|---|---|---|---|
| 1 | mycol1 | mycol2 | mycol3 | mycol1 | mycol2 | mycol3 | MyCol4 | MyCol5 | |
| 2 | 101 | abc | 0.5 | 101 | abc | 0.5 | 104 | 5 | |
| 3 | 102 | defgh | 0.78 | 102 | defgh | 0.78 | 105 | 7.8 | |
| 4 | 103 | ijklmnopq | 0.13 | 103 | ijklmnopq | 0.13 | 106 | 1.3 |