When writing every code, be sure to use the library() function. Each .r file is working in the base form. For example, if you start a new r script it should be run this way:
library(tidyr)
newtable4a <- pivot_longer(table4a, 2:3, names_to = "year", values_to = "cases")
Without doing this, R is working in the base form. It's also a good idea to re-install packages from time to time because they do update.