[R] how to add comma to string vector?

Ivan Krylov kry|ov@r00t @end|ng |rom gm@||@com
Fri Jun 10 12:34:00 CEST 2022


On Fri, 10 Jun 2022 12:19:57 +0200
Luigi Marongiu <marongiu.luigi using gmail.com> wrote:

> I need to convert an R vector to a python array.

Have you considered using the reticulate package on the R side or the
rpy2 package on the Python side? It's hard to cover all edge cases when
producing source code to be evaluated by another language. You may
encounter a corner case where you output a character special to Python
without properly escaping it and end up with an injection error (see
also: SQL injection vulnerability, the most widely known type of this
mistake). For producing R code from R values, there's deparse(), and
even that's not perfect:
https://bugs.r-project.org/show_bug.cgi?id=18232

Having said that, paste(sQuote(values, FALSE), collapse = ',') will do
the trick, but only if values are guaranteed not to contain single
quotes or other characters that have special meaning in Python. Note
the FALSE argument to sQuote: otherwise it could return ‘Unicode
quotes’, `TeX quotes', or even «guillemets», depending on the options
set by the user.

-- 
Best regards,
Ivan



More information about the R-help mailing list