I perform a rather complicated and lengthy statistical analysis of a data set, and one of the final outputs is a group of 8 colored squares with a centered label. Both the color and the label depend on the results of the analysis, many of them are created, and they need to be updated periodically, so manual editing is not an option. Squares are 2x2 cm2, and in some cases the marks do not correspond to the square. If you reduce the font size with cex, the text will become too small.
This is a simple example of a problem (I am using RStudio):
plot.new() plot.window(xlim=c(0,5),ylim=c(0,5)) rect(1,1,4,4) text(2,2,"This is a long text that should fit in the rectangle")

The question arises: how can I automatically insert a string of variable length into a rectangle, for example below?
plot.new() plot.window(xlim=c(0,5),ylim=c(0,5))

source share