This is extremely ugly, I have no idea why you'd do this. If you're going to call a tool in interactive mode on the CLI, then use R for proper data handling.
Aside from the fact that OP's example can run in a non-interactive mode, it makes absolutely no difference to the discussion.
If AP had a "better way of doing things" maybe they should have actually given us an example of what it looks like.
Right now, all I think about R is that snobby, gatekeeping assholes use it to do things I can already do easily in SQL.