1

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!

miken32
  • 42,008
  • 16
  • 111
  • 154
SeanM
  • 135
  • 7

2 Answers2

2

Why not just delete it from R?

```{r}
system("rm -r $FILEPATH")
```
data princess
  • 1,130
  • 1
  • 23
  • 42
0

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
miken32
  • 42,008
  • 16
  • 111
  • 154