To check if a package is installed or not, before running install.packages(), you can do the following:
"xtable" %in% rownames(installed.packages())
The output is TRUE if the package is already installed, otherwise FALSE.
In your case:
if("xtable" %in% rownames(installed.packages()) == FALSE)
{install.packages("xtable")
}
The code above will check if the package is already installed or not. It will skip the code in the If statement if the condition evaluates to TRUE, otherwise, it will install the required package.