St Mirren defeated Dundee United in a tense 4-3 thriller to move within a point of their hosts in the Scottish Premier League.