Have you considered saving it to a csv file in R and then reading the file in
python?
Tim
-----Original Message-----
From: R-help <r-help-bounces at r-project.org> On Behalf Of Ivan Krylov
Sent: Friday, June 10, 2022 6:34 AM
To: Luigi Marongiu <marongiu.luigi at gmail.com>
Cc: r-help <r-help at r-project.org>
Subject: Re: [R] how to add comma to string vector?
[External Email]
On Fri, 10 Jun 2022 12:19:57 +0200
Luigi Marongiu <marongiu.luigi at 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://urldefense.proofpoint.com/v2/url?u=https-3A__bugs.r-2Dproject.org_show-5Fbug.cgi-3Fid-3D18232&d=DwIFaQ&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=_jG6hqNCBjiWjIVFOx5Zm4cTTHAd-Rv2IBVjFZkWtwdR9QDc9qZI59xb7fZs6qtT&s=unYB3ajJ6LhkNKab1X8TGSNYaiYwhgEC7GZfknFfA9Q&e
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
______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Dhelp&d=DwIFaQ&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=_jG6hqNCBjiWjIVFOx5Zm4cTTHAd-Rv2IBVjFZkWtwdR9QDc9qZI59xb7fZs6qtT&s=10-fNd6yyMRCfduzKbeG6MHUz5Tkf4rf6lbKc7OFuSM&ePLEASE
do read the posting guide
https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org_posting-2Dguide.html&d=DwIFaQ&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=_jG6hqNCBjiWjIVFOx5Zm4cTTHAd-Rv2IBVjFZkWtwdR9QDc9qZI59xb7fZs6qtT&s=j_RwC0Njzj5WVBbGGmslVCm2EoDQxs7iNbMXItIRobM&eand
provide commented, minimal, self-contained, reproducible code.