What role does the capacitor and the resistor play in the following circuit?
It's a cheapo capacitive dropper power supply.

The impedance of the cap limits the current without wasting too much power into heat. R1 discharges C1 to protect you from shock when you unplug the bulb. R2 limits inrush current.