You are looking for the length of the road (kilometers) divided by the buffer area (square kilometers), aren't you? To calculate the length of the road, you can use your method with rasterToContour() , although this does not reproduce with the example you provided.
But to calculate the buffer area in square kilometers, you can do: n <- length(extract(r, line2_buff)) to get n number of pixels in the buffer equal to 625 m ^ 2. The required conversion factor is 1 km ^ 2 = 1 000 000 m 2. All together, the buffer area in km ^ 2 is determined as follows:
length(extract(r, line2_buff)) * 625 / 1000000
source share