Tuning number of layers in a Keras neural network in R

I’m trying to tune hyperparameters in a neural network I’ve made in R, but not sure how to tune the number of hidden layers. I know that in the Python version of Keras, it’s possible to specify a parameter that controls the number of hidden layers in the NN. It’s done by creating a function to build the NN that loops through layers.add(...) n times. But I’m not sure how to do something similar in the R version of Keras. The tuning_run function takes in a compiled model with a set number of layers. I know there’s tis package KerasTuneR, which seems to be able to do it, but I prefer not to rely on the core Tensorflow/Keras package.

How many layers are there in Keras? and How to Create any Lyers?