Home > OS >  "Error: unexpected ',' in..." when running R code in .Rmd code chunks on Visual
"Error: unexpected ',' in..." when running R code in .Rmd code chunks on Visual

Time:01-06

This is an example code snippet:

Problematic Code:

```{r}
ratings <- df %>%
  dplyr::group_by(rating) %>%
  dplyr::summarise(
    compound = mean(compound),
    neg = mean(neg),
    neu = mean(neu),
    pos = mean(pos),
  )

View(ratings)
```

This is the error I get:

Error message

r$> ratings <- df %>%
      dplyr::group_by(rating) %>%
      dplyr::summarise(
        compound = mean(compound),
        neg = mean(neg),
        neu = mean(neu),
Error: unexpected '}' in:
"    neu = mean(neu),
}"

r$>     pos = mean(pos)
      )
Error: unexpected ')' in:
"    pos = mean(pos)
  )"
Error: unexpected '}' in "}"

I'm not exactly sure what's causing it. I'm on a Windows 11 system, using VSCode as the editor and Radian to run the code. For VSCode extensions I am using: R (REditorSuppor), Markdown All in One (Yu Zhang).

When I run the code on RStudio I do not get an error and the code runs as expected. I'd rather use VSCode and figure out the issue. When searching for an answer online, I've seen issues with unicode characters and actual extra punctuations causing errors. However, as far as I'm aware those don't seem to be the issue here, and how to fix it. Any help would be greatly appreciated. Thank you.

Edit: There is one more detail I forgot to mention above, the summarized function works if it is written as one line:

ratings <- df %>%
  dplyr::group_by(rating) %>% 
  dplyr::summarise(compound = mean(compound), neg = mean(neg), neu = mean(neu), pos = mean(pos))

CodePudding user response:

If you are using radian as your R terminal in VS Code you need to add the following to your user settings.json:

"r.bracketedPaste": true

There are some other useful options in this medium post.

Instructions about how to access your settings.json are set out in the VS Code docs, as well as further information about user and workspace settings.

  • Related