Getting unexpected input error in R expression - r

I'm trying to run this code to stretch the axes after creating a matrix based on calculated Euclidean distances:
ds_newA <- sqrt((new[1] -A[1]ˆ2 + (3*(new[2]-A[2]))ˆ2)
However, I'm getting an input error in the R console:
Error: unexpected input in "ds_newA <- sqrt((new[1] -A[1])ˆ"
Error: unexpected input in "ds_newB <- sqrt((new[1] -B[1])ˆ"
How should the code for formatted?

As noticed in the SO thread Error: unexpected symbol/input/string constant/numeric constant/SPECIAL in my code :
These errors mean that the R code you are trying to run or source is not syntactically correct. That is, you have a typo.
To fix the problem, read the error message carefully. The code provided in the error message shows where R thinks that the problem is. Find that line in your original code, and look for the typo.
Most probably, here this is due to the symbol ˆ you are using, which is not the appropriate one for an exponent ^:
# correct symbol ^:
> 2^2
[1] 4
# wrong symbol you seem to use ˆ :
> 2ˆ2
Error: unexpected input in "2ˆ"
Change it to the correct one ^ and you will be fine.

Related

Warning message within R function seems to make the function not work?

Using the syuzhet package in R, the following works but returns a warning message:
object <- get_text_as_string("path/name.txt")
When I put this in a function, it returns the same warning error but does NOT change the value of object:
gen <- function(file){
object <- get_text_as_string(file)
}
gen("path/name.txt")
This is the warning message, if it matters:
Warning message:
In readLines(path_to_file) :
incomplete final line found on 'path/name.txt'
...but again, I get that from get_text_as_string() when used outside of the function, but it DOES change the value of object.
Anyone have any advice? There must be something I don't understand about functions?
(I've looked for similar questions/answers, if I've missed the right one I'd be happy to just be directed there.)

Error: object 'skim_without_charts' not found [duplicate]

I got the error message:
Error: object 'x' not found
Or a more complex version like
Error in mean(x) :
error in evaluating the argument 'x' in selecting a method for function 'mean': Error: object 'x' not found
What does this mean?
The error means that R could not find the variable mentioned in the error message.
The easiest way to reproduce the error is to type the name of a variable that doesn't exist. (If you've defined x already, use a different variable name.)
x
## Error: object 'x' not found
The more complex version of the error has the same cause: calling a function when x does not exist.
mean(x)
## Error in mean(x) :
## error in evaluating the argument 'x' in selecting a method for function 'mean': Error: object 'x' not found
Once the variable has been defined, the error will not occur.
x <- 1:5
x
## [1] 1 2 3 4 5
mean(x)
## [1] 3
You can check to see if a variable exists using ls or exists.
ls() # lists all the variables that have been defined
exists("x") # returns TRUE or FALSE, depending upon whether x has been defined.
Errors like this can occur when you are using non-standard evaluation. For example, when using subset, the error will occur if a column name is not present in the data frame to subset.
d <- data.frame(a = rnorm(5))
subset(d, b > 0)
## Error in eval(expr, envir, enclos) : object 'b' not found
The error can also occur if you use custom evaluation.
get("var", "package:stats") #returns the var function
get("var", "package:utils")
## Error in get("var", "package:utils") : object 'var' not found
In the second case, the var function cannot be found when R looks in the utils package's environment because utils is further down the search list than stats.
In more advanced use cases, you may wish to read:
The Scope section of the CRAN manual Intro to R and demo(scoping)
The Non-standard evaluation chapter of Advanced R
While executing multiple lines of code in R, you need to first select all the lines of code and then click on "Run".
This error usually comes up when we don't select our statements and click on "Run".
Let's discuss why an "object not found" error can be thrown in R in addition to explaining what it means. What it means (to many) is obvious: the variable in question, at least according to the R interpreter, has not yet been defined, but if you see your object in your code there can be multiple reasons for why this is happening:
check syntax of your declarations. If you mis-typed even one letter or used upper case instead of lower case in a later calling statement, then it won't match your original declaration and this error will occur.
Are you getting this error in a notebook or markdown document? You may simply need to re-run an earlier cell that has your declarations before running the current cell where you are calling the variable.
Are you trying to knit your R document and the variable works find when you run the cells but not when you knit the cells? If so - then you want to examine the snippet I am providing below for a possible side effect that triggers this error:
{r sourceDataProb1, echo=F, eval=F}
# some code here
The above snippet is from the beginning of an R markdown cell. If eval and echo are both set to False this can trigger an error when you try to knit the document. To clarify. I had a use case where I had left these flags as False because I thought i did not want my code echoed or its results to show in the markdown HTML I was generating. But since the variable was then used in later cells, this caused an error during knitting. Simple trial and error with T/F TRUE/FALSE flags can establish if this is the source of your error when it occurs in knitting an R markdown document from RStudio.
Lastly: did you remove the variable or clear it from memory after declaring it?
rm() removes the variable
hitting the broom icon in the evironment window of RStudio clearls everything in the current working environment
ls() can help you see what is active right now to look for a missing declaration.
exists("x") - as mentioned by another poster, can help you test a specific value in an environment with a very lengthy list of active variables
I had a similar problem with R-studio. When I tried to do my plots, this message was showing up.
Eventually I realised that the reason behind this was that my "window" for the plots was too small, and I had to make it bigger to "fit" all the plots inside!
Hope to help
I'm going to add this on here even though it's not a new question as it comes quite highly in the search results for the error:
As mentioned above, re checking syntax, if you're using dplyr, make sure you have all the %>% pipes at the end of the lines above the error, otherwise the contents of anything like a select statement won't pass down into the next part of the code block.

Unexpected "=" in rstudio

In my code, I get the error message saying that there is an unexpected "=". I have tried running the line without certain "=", but that only causes other error messages, like "unused arguments". This is supposed to help, along with the rest of the code, with making a plot easier to read.
Here is the line of code:
Tricep.peaks <- data.frame (findpeaks(Tricep_falt, npeaks=8, minpeaksheight=.01, minpeaksdistance=freq=5))
Here is the error message:
Error: unexpected '=' in "Tricep.peaks <- data.frame (findpeaks(Tricep_falt, npeaks=8, minpeaksheight=.01, minpeaksdistance=freq="
Packages running are, signal, zoo, matlab, ggplot2, pracma, purrr, dplyr
As the error message shows, the error is caused by minpeaksdistance=freq=5 which looks like a typo to me.
According to https://www.rdocumentation.org/packages/pracma/versions/1.9.9/topics/findpeaks, the parameter is called minpeakdistance but not minpeaksdistance.
The OP probably meant minpeakdistance=5 or minpeakdistance=freq.

How can I store an R variable with the Rextension in Netlogo?

I am using the Rextension in Netlogo.
Using the function r:put, r:get and r:eval. I can use all of these, however, when I use r:put and later on run an entire code in R via r:eval, the value of r:put is not stored and thus the code of r:eval cannot run.
My Netlogo code:
to go
r:clear
r:put "x" 3
print r:get "x"
r:eval "source('C:/Users/Amber van Oel/Documents/SEPAM/Thesis - ABM/Global
model/Netlogo oefenen/test2.r')"
end
My R code:
p <- 5 * x
The error I get when running this code is:
Extension exception: Error in R-Extension: Error in Eval:
org.nlogo.api.ExtensionException: Error in eval(ei, envir) : object 'x' not
found
The r:get "x" line is working, which means that the variable x is successfully transfered to R, however, I cannot use it in the second line.
Does anybody has experience with this error??

methylSigCalc function error

Trying to use the latest version of methylSig package (0.3.2), and getting an error when using the methylSigCalc function.
I get a cryptic error message after it figures out the number of loci:
Total number of bases: 2.59m
Error in result[, 3] : subscript out of bounds”
I tried running the code of the methylSigCalc function line by line, and the first error I encounter is when it gets to the methylSig_dataProcess function (below is the error after specifying just one core to try to get a useful error message):
Error in do.call(rbind, lapply(which(validLoci),
methylSig_dataProcess, : error in evaluating the argument 'args' in
selecting a method for function 'do.call': Error in match.fun(FUN) :
object 'methylSig_dataProcess' not found
It does seem that methylSig_dataProcess is now missing from the package.
Any suggestions on where I might be going wrong?
Many thanks,
gogatea

Resources