Converting whole R Markdown chunk to function with one variable

Lets say I have chunk with length around 150 lines with dozen of different functions and and for loops inside of it and I would like to rerun the chunk again with only one variable changed. Does R have a solution for it or I just have to rewrite the whole chunk to a function?