From c@@rd|@g@bor @end|ng |rom gm@||@com  Tue Mar 19 11:51:10 2019
From: c@@rd|@g@bor @end|ng |rom gm@||@com (=?UTF-8?B?R8OhYm9yIENzw6FyZGk=?=)
Date: Tue, 19 Mar 2019 10:51:10 +0000
Subject: [Rd] Possibly broken system2 env-option
In-Reply-To: <A5278CAD-D8BD-45B2-A14C-709BDB8FFDB0@gmail.com>
References: <f38d1b55-c8f8-dbe4-94c4-bf15bb32c052@gmx.de>
 <3244E2B3-3E44-4469-919B-4E5768F06F23@gmail.com>
 <CABtg=K=RUYOXkgUgMmMKxYcmDTm9ZUNt2bErKLrs9wiMx1B50w@mail.gmail.com>
 <A5278CAD-D8BD-45B2-A14C-709BDB8FFDB0@gmail.com>
Message-ID: <CABtg=Kn_1-2thZSfb+UFrpYx=ukWFKy3Un7qTkv1k1-mgb5eWg@mail.gmail.com>

Right. I suppose, one thing you can do is setting the env var in the
current process, call system2() and then unset/restore the env var.
This is unlikely to cause problems, unless your R process is
multi-threaded, and the env var causes troubles in the other threads.
The withr package makes setting the temporary env var convenient, but
it is easy to write your own with_envvar() function:

withr::with_envvar(
  c(VAR = "Hello world"),
  system2("sh", "-c 'echo $VAR'"))

This seems to be portable, at least it works on macOS and Windows.

G.


On Tue, Mar 19, 2019 at 10:44 AM peter dalgaard <pdalgd at gmail.com> wrote:
>
>      On Windows, ?env? is only supported for commands such as ?R? and
>      ?make? which accept environment variables on their command line.
>
> So I suppose that would be tricky.
>
> The basic issue is that on Unix-alikes, system2 constructs a command like
>
> FOO=bar cmd args
>
> and passes that to sh via system(). On windoes, system() does not call sh, so system2() does (effectively)
>
> cmd FOO=bar args
>
> and hopes that cmd knows what to do with the env setting.
>
> -pd
>
> > On 19 Mar 2019, at 11:09 , G?bor Cs?rdi <csardi.gabor at gmail.com> wrote:
> >
> > On Tue, Mar 19, 2019 at 9:59 AM peter dalgaard <pdalgd at gmail.com> wrote:
> > [...]
> >> What you need is something like (NB: single quotes!)
> >>> system2("sh", env = c("VAR='Hello World'"), args = c("-c 'echo $VAR'"))
> >> Hello World
> >
> > Just out of curiosity, do you think it is possible to make this
> > portable, assuming sh is available? On Windows it gives
> >
> >> system2("sh", env = c("VAR='Hello World'"), args = c("-c 'echo $VAR'"))
> > /rtools34/bin/sh: VAR=Hello World: No such file or directory
> > Warning message:
> > running command '"sh" VAR='Hello World' -c 'echo $VAR'' had status 127
> >
> > G.
>
> --
> Peter Dalgaard, Professor,
> Center for Statistics, Copenhagen Business School
> Solbjerg Plads 3, 2000 Frederiksberg, Denmark
> Phone: (+45)38153501
> Office: A 4.23
> Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com
>
>
>
>
>
>
>
>
>



[Rd] Possibly broken system2 env-option
=?UTF-8?B?R8OhYm9yIENzw6FyZGk=?=