What is going on here is not exactly what it appears, because of the
conventions that S uses in printing character strings. In parsing a
string, the backslash ("\") is an escape character The escape is used,
in particular, to indicate an octal representation of a character or to
indicate some other special characters such as newline="\n", etc. The
backslash appearing before an ordinary character other letters
"escapes" the letter but when the letter has no special meaning, the
backslash has no effect. Therefore to include the backslash in a
string, it must itself be escaped. See following examples:
> "\a" ## backslash escape has no effect
[1] "a"
> "\153" ## backslash for octal representation (happens to be a letter)
[1] "k"
> "\." ## no effect
[1] "."
> "\\" ## backslash escape for a backslash
[1] "\\"
> "\n" ## backslash combination for newline
[1] "\n"
The last two seem odd, but only because the S print() function follows
the same rules as the input parser, and puts the escape backslash back
in front of the backslash! We can easily verify that this is really a
single character string:
> nchar("\\")
[1] 1
> nchar("\n")
[1] 1
and outputting such strings other than with something using "print" (in
particular, with cat()) gives the correct internal string:
> print("abcd\\efg\nhi\tjk\n")
[1] "abcd\\efg\nhi\tjk\n"
> cat("abcd\\efg\nhi\tjk\n")
abcd\efg
hi jk
In your case you want a backslash before the . in the pattern to force
grep to match the . character rather than . as a wildcard for any character.
To represent that \ in the command, you need to escape it, hence
"SP\\."