I am working with the R programming language.
I have the following dataset:
df = structure(list(var1 = c(4.39524353447787, 7.6982251051672, 25.5870831414912,
10.7050839142458, 11.2928773516095, 27.1506498688328, 14.609162059892,
-2.65061234606534, 3.13147148106474, 5.54338029900042), var2 = c(22.2408179743946,
13.5981382705736, 14.0077145059405, 11.1068271594512, 4.44158865245925,
27.8691313680308, 14.9785047822924, -9.66617156629638, 17.0135590156369,
5.27208592272066), var3 = c(-0.678237059868451, 7.82025085341705,
-0.260044483072397, 2.7110877070886, 3.74960732150743, -6.86693310742413,
18.3778704449452, 11.5337311783652, -1.38136937011948, 22.5381492106993
), var4 = c(14.2646422147681, 7.04928517007729, 18.9512566104502,
18.7813348753304, 18.2158108163749, 16.8864025410009, 15.5391765353759,
9.38088289423278, 6.94037336260083, 6.19528998987617)), row.names = c(NA,
-10L), class = "data.frame")
write.csv(df, "df.csv")
Here is a VBA function I have that I am running from the VBA editor within Excel:
Sub ColorCellsRowWise()
Dim myRange As Range
Set myRange = Range("A2:D11")
Dim row As Range
For Each row In myRange.Rows
Dim cell As Range
For Each cell In row.Cells
If IsNumeric(cell.Value) Then
If cell.Value = Application.WorksheetFunction.Max(row) Then
cell.Interior.Color = RGB(253, 127, 127)
ElseIf cell.Value = Application.WorksheetFunction.Large(row, 2) Then
cell.Interior.Color = RGB(252, 181, 128)
ElseIf cell.Value = Application.WorksheetFunction.Large(row, 3) Then
cell.Interior.Color = RGB(253, 247, 127)
Else
cell.Interior.Color = RGB(199, 254, 126)
End If
End If
Next cell
Next row
End Sub
My Question: I am wondering, is it possible to run this VBA function directly in R?
I tried to follow the instructions here (Run VBA script from R):
library("devtools")
install_github('omegahat/RDCOMClient')
current_dir <- getwd()
file_path <- file.path(current_dir, "df.csv")
df <- read.csv(file_path)
xlApp <- COMCreate("Excel.Application")
xlWbk <- xlApp$Workbooks()$Open(file_path)
But from here I do not know what to do.
I am working on an R script where "df" is created in R, then saved as a CSV - and then I want to run the VBA script all in one shot. The approach in the above link seems like this might not be possible.
Can someone please show me how to do this?
Thanks!