shiny output messing up

you should debug your main function, i.e. observe what values are passed in via the input$ params, and understand how they behave in your function.

(Shiny debugging and reprex guide)