Can't code functions


I'm trying to learn a bit of basic R - part of an attempt to get my head round Bayesian statistics. I'm working my way through 'Learn R in a Day by Steve Murray', experimenting with the examples in the book using the Console in RStudio Cloud, running within Chrome on my Chromebook. All is going pretty well, but try as I might I can't get any of the 'create a function' examples to work. Here's my problem:


I've two problems. First, I don't seem able to format the multiplier problem with the indents - except by repeatedly pressing the space bar. Second, I keep getting this error message telling me I'm getting an unexpected symbol. I've tried repeating this example using RStudio on my Windows 10 PC and using R on my Windows 10 PC, but always with the same error message. All this suggests I'd doing something very basic wrong.

Can anyone help

Regards Roger D

Try to always write the indented code as follow in script editor not in a console.

> multiplier <- function(x, y, z){
+     result<- x*y*z
+     print(result)
+ }

Your problem is only a lack of line breaks.
The document on the left indicates the console is outputting a new line by showing you a plus symbol to symbolise that. Put the print statement onto a new line and not as a run on to the result assignment.

(Alternatively you could use a semi colon to have the effect of a new line without the newline, but that may be a poor habit to form)

Many thanks. This worked. I'm switching to the script editor in the top left window from now on

Many thanks for the prompt response. Basic question I know, but how do I get onto a new line in the console? Carriage return doesn't work.

Why? The console is not really intended for this type of thing. That's what the editor is for.

I don't think you can get a true "new" line in the console but you can use cntl return to get text to show up on a second line without executing.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.