R Error: unexpected ‘}’ in Code (2 Examples)
In this R article you’ll learn how to fix the error message “unexpected ‘}’ in X”.
Table of contents:
Let’s jump right to the examples…
Example 1: Reproduce the Error – unexpected ‘}’ in X
This Example illustrates how to reproduce the R programming error “unexpected ‘}’ in X”. Consider the following if statement:
if(TRUE) { "x" }} # Reproducing the error # Error: unexpected '}' in "if(TRUE) { "x" }}"
The RStudio console returned the error message “unexpected ‘}’ in X”.
The reason for this is that we used one curly bracket too much at the end of our code. This can easily happen when if else statements or in user-defined functions are getting more complex.
Example 2: Fix the Error – unexpected ‘}’ in X
Example 2 shows how to properly apply curly brackets in R. Compare the following syntax with the R code of Example 1:
if(TRUE) { "x" } # Fixing the error # [1] "x"
This time, the if statement worked fine, because we used only one curly bracket at the end of our code.
Video & Further Resources
Have a look at the following video of my YouTube channel. I’m explaining the R code of this article in the video.
The YouTube video will be added soon.
Furthermore, you might have a look at the other R articles of my website. I have published several tutorials already:
On this page you learned how to deal with the error “unexpected ‘}’ in X” in the R programming language. Don’t hesitate to let me know in the comments section below, in case you have additional comments or questions.
Subscribe to the Statistics Globe Newsletter
Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.
Thank you!
Welcome to the Statistics Globe newsletter. From now on, I’ll send you regular emails about statistics, data science, AI, and programming with R and Python.
I’m Joachim Schork. On this website, I provide statistics tutorials as well as code in Python and R programming.
Statistics Globe Newsletter
Get regular updates on the latest tutorials, offers & news at Statistics Globe. I hate spam & you may opt out anytime: Privacy Policy.
Thank you!
Please check your email inbox and click the confirmation link to complete your subscription. If you don’t see the email within a few minutes, please also check your spam/junk folder.






