Can you share the command you run that raises the error? Your function works, so the error's probably coming from somewhere else.
Also, on this forum, you can format code blocks by putting a line with three tick marks above and below the code:
This will show as regular text
```
# This is a code block
x <- 1 + 1
```
Back to regular text