Welcome to the community!
The error in your code is the absence of ;'s at the end of statements. If you write a code in a script (or in console) where each statementis in it's own line, ; is not required. But if you write them in same line, you'll have to add them.
Try this: panel.cor <- function(x, y){usr <- par("usr"); on.exit(par(usr));par(usr = c(0, 1, 0, 1)); r <- round(cor(x, y), digits=2);txt <- paste0("R = ", r);cex.cor <- 0.8/strwidth(txt);text(0.5, 0.5, txt, cex = cex.cor * r)}.
Hope this helps.