Unexpected symbol in r script, please help.

So I'm absolutely new to R and entirely legally blind and use a screen reader, so I'm not completely useless. Anyway I'm just starting out and no matter how many times I try, I keep getting the unexpected symbol error when I run the script. I tried the code in the commandline and it works there... I'm learning from a book, anyways the code is below and is written as I wrote it in command line and it worked...
hellostring<-"hello world"
print[hellostring]

So what am I doing wrong in the script?
Thanks

Hello and welcome to the community.
Your issue is the type of bracket used. print like all r functions should be accompanied with round brackets ( )
R does use square brackets [ ] for accessing vector and lists.
In this case if you use round brackets your code will run fine.
Hope it helps

So I tried it with the following corrections and I'm still getting the same error.
Hellostring<-"hello world"
print(hellostring)

That must be frustrating
In this code there is an issue that the first assignment is to Hellostring capitalised and then the hellostring to print is uncapitalised. Can you copy and paste the following ?


hellostring <- "hello world"
print(hellostring)

I wonder whether the problem may be something else, I'll tell you my reasoning.
If I try the code you first shared, the one with square rather than rounded brackets, I actually get a different error than what you described.
The error I see says "Error in print[hellostring] : object of type 'closure' is not subsettable"
So I'm wondering, if your script may have other contents than just the hellostring assignment and print statement, because if you do, they might be the culprit...

There's nothing else in the script besides the hello world code.

ok, a few more questions to try and get to the bottom of this mystery!

how are you executing the script ?
by the mechanism of the run , or source buttons in the script window?
by using the source function in the console/commandline ?
by selecting all the code in the script window with Control+A and then sending to console with Control+Enter?
Those are the different ways I can think, there's probably more...

Just tried it and still got the same error. And it is frustrating. Is there a way to post a screencap of what I'm typing,?

yes, if you can take a screen grab and save it as a file, this forum would let you post it by dragging and dropping the image into the box where you type your posts

Here is the screen cap

Ah, my apologies, I was wrong to assume you were using Rstudio.
You are using RGui to compose your script.
You are then using Rterm to run it.
I see that in Rterm you are calling Rscript, but Rscript is not an R function, nor a command of Rterm, its a utility in its own right.

Assuming that you are in an Rterm window, and that the working directory is set to be the same as where your hello.R script is then you need the source() function like :

source("hello.R")
 

here is an example, and showing some other useful functions.
I use getwd() to see where I am in Rterm
I use dir() to see what files and folders are at my current working directory
I can verify if a file i want is there with file.exists()
and finally I can use source to run it
source with echo=TRUE parameter shows the underlying code of the script as well as running it.

> getwd()
[1] "C:/Users/nirgr/Documents/R/scripts"
> dir()
 [1] "experimentscreenshotscriptjs.html" "hello.R"
 [3] "lubridate test.R"                  "report.html"
 [5] "report.Rmd"                        "rmarkdownexample.R"
 [7] "shiny test example.R"              "tb_local.html"
 [9] "tb_local.Rmd"                      "twitter_premium.jsonl"
[11] "webshotshinytest.R"
> file.exists("hello.R")
[1] TRUE
> source("hello.R")
[1] "hello world"
> source("hello.R", echo=TRUE)

> h<-"hello world"

> print(h)
[1] "hello world"

I have to say, R is not the greatest for error messages. If in a new session, I type a and enter, it correctly complains about no such object, similar for if I did with b. If I do a space b and enter, I get a completely different error message.

> a
Error: object 'a' not found
>  b
Error: object 'b' not found
> a b
Error: unexpected symbol in "a b"

The original error you highlighted is of this form, simply because like my undefined 'a' , 'Rscript' was not defined, and it was followed up with the 'b' of 'hello.R'

So I tried the execute order you gave, and it gave me an error about no such file In directory. I'm assuming I need to change the directory? How do I do that? And what it's the difference between the gui and studio? Sorry about all the questions, and thanks for all your help so far.

changing current working directory in R can be done with setwd() function -- counterpart of the getwd() function. It can be useful to use file.path() function to put together the directory names of a path as different systems use different sepators to distinguish one directory for an other e.g.
C:\myfolder on windows
~/home/myfolder on linux

Different ways to work in R are available to serve different use cases, and different peoples preferences. RGui provides a minimalistic 'no frills' R script editing tool, and a live dynamic console to run interactive commands on. You can use source() on RGui command lines to read in and evaluate scripts that you have saved in an RGUI editor. But if you want to launch scripts from the command line, an executable such as R.exe Rscript.exe Rterm.exe might be useful. There are so many variations just from historic reasons. R.exe is probably the most general ?

RStudio is a more heavyweight coding environment with lots of features packed in with the intention of helping R programmers be extra productive.
I know some old-school R programmers that get by till todat with RGui and dont look at RStudio, but I'm a big fan of RStudio myself.

Thanks for the info, and there was nothing wrong with the code, I just didn't realize that you could run the code in the script and see the results in the command console. I think I'll stick with just the gui, since I'm just learning. Also what would you reccomend to learn to use if I want to work professionally in the field?

1 Like

I hesitate because I'm not sure how different the experience is when one relies on a screen reader...but I would normally recommend it to you.

Fair enough, and thanks for the info though I'll have to try r studio later, considering I'll be using it for my thesis.