SeanM
SeanM

Reputation: 135

Cannot delete folder path when passing Rmarkdown variable into Bash

I'm trying to delete a folder path defined as a R variable filepath by using a bash code chunk in my Rmarkdown.

Following up from a previous question as well as from Yihui's example , I passed my variable filepath into the bash chunk with the following R code chunk:

```{r}
Sys.setenv(FILEPATH = filepath)
```

While I'm able to echo my variable in the bash chunk, there was an error when I tried rm -r my $FILEPATH variable.

```{bash}
echo $FILEPATH
rm -r $FILEPATH
```

~/Projects/test_threshold

rm: ~/Projects/test_threshold: No such file or directory

I've tried assigning my variable directly in the bash terminal for deletion and it worked.

deletedir=~/Projects/test_threshold
rm -r $deletedir

Any suggestions on what I've done wrong? Thanks for your help!

Upvotes: 1

Views: 99

Answers (2)

miken32
miken32

Reputation: 42715

Use an absolute path such as /home/users/seanm/Projects/test_threshold instead of using the tilde (which is best left as a convenience for humans.) And always always quote your variables.

Use this script to see an example of what's going wrong for you:

cd ~
mkdir foobar
ls ~/foobar
a="~/foobar"
echo "$a"
ls "$a"

You'll see this output:

 
~/foobar
-bash: cd: ~/foobar: No such file or directory

Upvotes: 0

data princess
data princess

Reputation: 1158

Why not just delete it from R?

```{r}
system("rm -r $FILEPATH")
```

Upvotes: 2

Related Questions