0

I am new to writing RExcel macros, and I have a question. I am taking a user input from a macro through an InputBox(). The input is an integer. Then I want to pass this integer value to R.

This is my attempt:

Option Explicit
Sub KMeansClustering()
Dim k As Integer
RInterface.PutDataframe "mydata", Selection
RInterface.RRun "testdata <- na.omit(mydata)"
RInterface.RRun "testdata <- scale(testdata)"
k = InputBox("Enter k")    
' Supply data from VBA k variable to k R variable
RInterface.RRun "fit <- kmeans(testdata, k)"
RInterface.RRun "aggregate(testdata,by=list(fit$cluster),FUN=mean)"
RInterface.RRun "result <- data.frame(testdata, fit$cluster)"
End Sub
Vogel612
  • 5,620
  • 5
  • 48
  • 73
DonaldS
  • 13
  • 4

1 Answers1

0

Before using "k" in R you must first transfer the value to an R variable (array):

Rinterface.PutArrayFromVBA("k_value", k) Rinterface.RRun "fit <- kmeans(testdata, k_value)"