ggplot2 error: Error in default + theme : non-numeric argument to binary operator - r

I have been getting the error Error in default + theme : non-numeric argument to binary operator. I have been using R and teaching R for a long time but I can't find this problem. I have included a reproducible example that fails this way below:
library(tibble)
library(ggplot2)
brains <- as_tibble(brains)
brains <- brains[1:10, ]
brains
ggplot(brains, aes(x = BodyWt, y = BrainWt)) +
geom_point()
The error occurs when executing the ggplot() statement.
My hardware is an HP Laptop 15-ef0xxx. I am running Windows 10 Home version 2004. I am running RStudio community edition "Water Lily" and R version R x64** 4.0.2.
I know this is a simple error and it is driving me crazy.

So I finally solved this problem. On the github issue I had opened Hiroaki commented that "One possibility is that you might set a invalid default theme in your .Rprofile, but I'm not sure..." (see link to issue below).
I'm not sure if your R file is part of a project but mine is.
So I went back and deleted the theme_set() line in my R file, went in and double checked all my project options and selected the option "Disable .Rprofile execution on session start/resume" and "Quit child processes on exit". And then I restarted the R session and now everything works. Including on the default R editor console.
I'm not sure if all those steps are necessary but that seemed to do the trick for me! Hope it helps.
I thought this was an RStudio issue but it seems it's possibly a ggplot2 > problem. I have verified using two different datasets that the same >error comes up when I try using ggplot2 in RStudio or using the default R >console. I get the exact same error with code that's been working fine >but now suddenly won't. I have opened an issue on Github (ggplot2) with a >reprex. Might be worth checking there: >https://github.com/tidyverse/ggplot2/issues/4177
I know this is not an answer per se but I don't have enough reputation >points to add a comment to the previous answer but I thought linking to >the issue on Github might help.

I think you have numbers quoted somewhere and you are trying to perform mathematical operation on character values. Consider
x <- c("5","6")
y <- x/1
> y <- x/1
Error in x/1 : non-numeric argument to binary operator
Now try converting x to numeric and perform the same operation.
y <- as.numeric(x)/1
> y
[1] 5 6
So, you need to use as.numeric on your variable.
The following should resove this issue
ggplot(brains, aes(x = as.numeric(BodyWt), y = as.numeric(BrainWt)))

Related

Error when using ggstatsplot - Error in 'mutate()': Caused by error in vapply()':

Just installed ggstatsplot and tried running the Example in the Documentation to just see what inputs it requires and how to manipulate the function.
library(ggstatsplot)
ggbetweenstats(mtcars, am, mpg)
I was immediately met with the error:
Error in `mutate()`:
! Problem while computing `n_label = paste0(am, "\n(n = ", .prettyNum(n), ")")`.
Caused by error in `vapply()`:
! values must be length 1,
but FUN(X[[1]]) result is length 3
I have tried multiple examples I found online of how to use the package and all result in the same error. I also tried ggwithinstats and received the same error. I updated all of my packages and have restarted R Studio at each step.
Any help would be appreciated, please let me know if I can provide any other information.
Sorry for the troubles.
This is due to update to insight package (https://github.com/IndrajeetPatil/ggstatsplot/issues/749).
EDIT on 21 May 22:
Both statsExpressions and ggstatsplot updates are now on CRAN, so all these issues should go away.
I get the exact same error.
#for reproducibility and data
set.seed(123)
library(WRS2)
library(ggstatsplot)
ggwithinstats(
data = bugs_long,
x = condition,
y = desire
)
May it is a bug?

truthTable in Rstudio Error: incorrect outcome specification

I was trying to do a QCA analysis of sufficeny in RStudio 4.1.3. using truthTable command. By running the code i get Error: wrong outcome specification. The outcome is written correctly. When I do an analysis of necessity with superSubset code everything works fine. Everything is coded numeric but it does not work with bivariate callibration either.
> ttCO21 <- truthTable( data = mydata, outcome = "CO21",
+ conditions = "GENDG1, FDI1, GDP1",
+ sort.by="incl, n", show.cases = TRUE, complete = TRUE)
**Error: Incorrect outcome specification.**
I had the same issue today and the solution was to delete any unnecessary columns (or create a new dataframe). I left only the calibrated conditions and outcome, because when I found this error, the raw data was in the same df, even if was not using it in the truthTable() command.
Because of the 3 hours i wasted on this problem i'll share my situation and solution. I had the exact same problem. creating a new df did not help, nor did removing unnecessary colums (wouldn't make sense anyway). Completely reinstalling en reloading the QCA package did work for me. Hope this helps someone.
you can try "comma" instead of "dot" (in “decimal") when you import file

R function seas() not found in v.4.1.0

I have a script that I have used several times over the past year. The last time I used it was 3 months ago. It was all working with no errors.
In the interim, I have upgraded R to v. 4.1.0. This may be a generic problem, but I've covered what I think are the obvious possible explanations (packages not installed, libs not loaded, typos, etc).
Here's the relevant code (all calls are successful up to the seas() call):
library(fpp2)
library(ggplot2)
library(seasonal)
library(seas) # Added recently in attempt to mitigate this issue
hh=6
ff=12
LT <- read.csv("C:\\...\\lt.csv")
LTts <- ts(LT, start=c(2007,1),frequency=12)
#...
LTx11 <- seas(LTts, x11="")
RESULT (Console output):
> LTx11 <- seas(LTts, x11="")
#Error in seas(LTts, x11 = "") : could not find function "seas"
Any tips or suggestions would be greatly appreciated.
After digging around a little more, I found the original text reference where I had learned to use the seas() function in Hyndman's text: https://otexts.com/fpp2/x11.html
In that text, I also found that seas() should be exposed by the seasonal library and verified this in the docs for that library.
After verifying this, added seasonal:: in front of the function and it worked. I wish I knew WHY this happened, but I don't. So here is the workaround.
Workaround:
LTx11 <- seasonal::seas(LTts, x11="")
If anyone can explain why this was necessary in v.4.1.0 and not earlier versions of R, please do!

Fatal error when runing es() function in R

es function in smooth package has a weird behavior for some short time-series. Here is a case:
require(smooth) # version 2.4.7
#create two time series of length 10
vec1_ts<-ts(c(36,24,51,7,7,77,1,29,19,2),start=c(2016,1),frequency=12)
vec2_ts<-ts(1:10,start=c(2016,1),frequency=12)
#this works:
es(vec1_ts)
#but this doesn't:
es(vec2_ts)
# another non-working example with a shorter length
vec3_ts<-ts(c(23,5,4,12,2,5),start=c(2016,1),frequency = 12)
es(vec3_ts)
I got a "R encountered a fatal error" message, and then forced to terminate the session when running es for the second time-series.
This looks like a bug in the function of that package. Can you, please, submit the bug here: https://github.com/config-i1/smooth/issues
The StackOverflow is not for solving problems of specific packages, it is for the solution of more general problems.

DataTable warning table ajax error for some people but not all with shiny

I recieved such error when, in created shiny app in R with DataTables packages in my mobile and one laptop. However, it didn't throw error with other laptops, Same code was working yesterday, and day before yesterday. I haven't changed code, but it is throwing error.
Why there is problem with some devices but not all devices. I used other devices, those devices are not throwing error.
Any idea how to fix this problem? The solution for this problem should be explained here http://datatables.net/manual/tech-notes/7
What is issue ?
Following is thrown error as follows:
DataTables warning: table id=DataTables_Table_0 - Ajax error. For more
information about this error, please see http://datatables.net/tn/7
I am also suffering. Please take a look at the discussion of this issue #269 on Github.
It seems to be a problem post version 0.1 where you need to "flatten" variables using as.numeric.
ISSUE
Example and solution given by XD-DENG was and to quote them:
"The error is gone when I use
temp <- tapply(iris$Sepal.Length, iris$Species, mean)
result <- data.frame(species = names(temp),
mean = as.numeric(temp))
return(result)
instead of:
temp <- tapply(iris$Sepal.Length, iris$Species, mean)
result <- data.frame(species = names(temp),
mean = temp)
return(result)
The main difference is whether the column mean has additional attribution, dimension. This is what caused the error.
But it's still weird given version 0.1 of DT works perfectly on both."
PATCH
#yihui has pushed a fix for this to the dev version so please try update your DT package. (You can use package devtools to install from github devtools::install_github('rstudio/DT'))
I hope this fixes this intermittent error.
I just explicitly made as.numeric() to all numeric columns within my data.frame (though they are already numeric when I do str()) and the problem is gone.

Resources