Studying Professor Dalgaard's code a bit, here is a little hack that
returns the desired result on Unix-alikes:
From
>* system2("echo", env = c("VAR='Hello World'"), args = c("$VAR"))*
to
>* system2("echo", env = c("VAR='Hello World';"), args = c("$VAR"))*
Hello World
That is, adding the semi-
Okay, thanks for clarification.
On 3/19/19 10:51 AM, peter dalgaard wrote:
> You are using it wrong. It wants strings of the form "name=value", not a
> character vector with names as labels. So this is closer to the mark:
>
>> system2("echo", env = c("VAR='Hello World'"), args = c("$VAR"))
>
>>
>
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
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 sy
On Tue, Mar 19, 2019 at 9:59 AM peter dalgaard 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 ava
You are using it wrong. It wants strings of the form "name=value", not a
character vector with names as labels. So this is closer to the mark:
> system2("echo", env = c("VAR='Hello World'"), args = c("$VAR"))
>
However, as you see it doesn't work as intended. The problem is that the
$-substitu
Probably not broken, just hard to get the quoting right? Or it does
not work with echo?
It is surely hard to get it right, I have been trying for 15 minutes.I
am pretty sure that the correct form is env = "VAR='Hello-World'" but
spaces and other special characters might cause trouble. E.g. this
wo