It should be pretty straightforward. Just determine the predicted class, and the actual class, where each class is identified by an integer. Then just increment the confusion matrix there
confusionMatrix(trueClass, estimatedClass) = confusionMatrix(trueClass, estimatedClass) + 1;
Note : you cannot determine a confusion matrix for test data since you don't know the true class - you only know the estimated class. You only know the true class for the training data and validation data, not the test data.