R function seas() not found in v.4.1.0 - r

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!

Related

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

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)))

Removing/de-registering a specific function from an R package

I may not be using the terminology correctly here so please forgive me...
I have a case of one package 'overwriting' a function with the same name loaded by another package and thus changing the behavior (breaking) of a function.
The specific case:
X <- data.frame ( y = rnorm(100), x1 = rnorm(100), x2 = rnorm(100) )
library(CausalImpact)
a <- CausalImpact::CausalImpact( X, c(1,75), c(76, 100) ) # works
library(bfast) # imports quantmod which loads crappy version of as.zoo.data.frame
b <- CausalImpact::CausalImpact( X, c(1,75), c(76, 100) ) # Error
I know the error comes from two versions of the function as.zoo.data.frame.
The problematic version is imported by bfast from the package 'quantmod' (see https://github.com/joshuaulrich/quantmod/issues/168). Unfortunately their hotfix did not prevent this error. Super annoying.
I can hack around this specific problem, but I was wondering if there is a general way to like 'de-register' this function variant from the search path. Neither detach nor unloadNamespace remove the offending function (same behavior after). An explanation and similar problem is discussed here and here, but I wasn't able to find a general solution. For instance I'd rather just remove this function than clone and re-write CausalImpact to deal with this behavior.
From R 3.6.0 onwards, there is a new option called "conflicts.policy" to handle this within an established framework. For small issues like this, you can use the new arguments to library(). If you aren't yet to 3.6, the easiest solution might be to explicitly namespace CausalImpact when you need it, i.e. CausalImpact::CausalImpact. That's a mouthful, so you could do causal_impact <- CausalImpact::CausalImpact and use that alias.
# only attach select
library(dplyr, include.only = "select")
# exclude slice/arrange from being attached.
library(dplyr, exclude = c("slice", "arrange"))
library(bfast, exclude = "CausalImpact") should solve your problem.
Attach means that they are available for use without explicit prefixing with their package. In either of these cases, something like dplyr::slice would work just fine.
For more information, you can see ?library. Also, the R-Core member Luke Tierney wrote a blog explaining how the conflicts.policy works. You can find that here
Here's an answer that works, but is less preferable than de-registering a S3 method because it involves replacing the registered version in the S3 Methods table with the desired method:
library(CausalImpact)
library(bfast)
assignInNamespace("as.zoo.data.frame", zoo:::as.zoo.data.frame, ns = asNamespace("zoo"))
based partially on #smingerson's suggestion in the comments

R:V3.1.1, Platform:x86_64-w64-mingw32/x64 (64-bit), Package: choroplethrMaps

This is my first question to the community. I've read through the guidelines and am doing my best to ask an appropriate question and including a minimal, complete, and verifiable example. That being said, please feel free to suggest ways in which I can ask better questions going forward.
I am having trouble with the choroplethrMaps package, which I have never used in the past. I have had issues installing packages on my work computer before, but have gotten around this issue by pasting the package and its dependencies in my library directory. Part of my issues may stem from that, but I'm not sure.
Here is the code that replicates the issue on my machine.
library(choroplethrMaps)
library(choroplethrAdmin1)
library(choroplethr)
data(state.map)
df<-data.frame(region=unique(x = state.map$region),value=rnorm(n = 51,mean = 500,sd = 45))
debug(state_choropleth)
state_choropleth(df = df,title = "", legend = "", num_colors = 1)
After debugging the state_choropleth function, it looks like there is an error with the "render" portion of the code. When I execute the above code, I get the following error message.
Error in withCallingHandlers(tryCatch(evalq((function (..., call. = TRUE, :
object '.rcpp_warning_recorder' not found
Note that I am only using state_choropleth because when running the choroplethr function, I was advised to use state_choropleth instead. It seems as though choroplethr is out of date.

Error in code from split-apply-combine paper - how to resolve?

To try and get to grips with data manipulations in R, I've started reading Hadley's paper on split-apply-combine.
I'm on page 3 and trying to go through the code to understand it. Unfortunately the code is erroring and my reproduction is faithful (I've done c&p and handtyped). As I'm trying to learn this stuff and I'm right at the beginning I can't actually tell what's wrong with it. I tried it on both R2.5 and R3.0
library("MASS")
library("plyr")
data(ozone)
one<-ozone[1,1,]
month<-ordered(rep(1:12,length=72))
model<-rlm(one ~ month - 1)
deseas<-resid(model)
deseasf<-function(value) {rlm(value ~ month - 1)}
models<-aaply(ozone,1:2,deseasf)
deseas<-aaply(models,1:2,resid)
Where the models line errors with Error: Results must have one or more dimensions.
Can somebody tell me whether it works for them, or what needs to be fixed/amended if it doesn't and why?
PS - Can't check on http://plyr.had.co.nz/ for errata because my work proxy currently blocks the site!
It should be
models <- alply(ozone, 1:2, deseasf)
deseas <- ldply(models, resid)
It turns out this is a bug in aaply and Hadley has said he will look into it soon:
https://groups.google.com/forum/#!topic/manipulatr/kg2wDU96mGM

GoogleVis, Geomap Plot error

When I want to create the map using the gvisGeoMap() from googleVis, I get error:
## Using the google visualization API with R
library(googleVis)
input<- read.csv("data.csv")
select<- input[which(input$Subgroup=="Total 5-14"),]
select<- input[which(input$Subgroup=="Total 5-14 yr"),]
Map<- data.frame(select$Country.or.Area, select$Value)
names(Map)<- c("Country", "Percentage")
Geo=gvisGeoMap(Map, locationvar="Country", numvar="Percentage",
options=list(height=350, dataMode='regions'))
plot(Geo)
#starting httpd help server ... done
#Error in ifelse(interactive(), getOption("browser"), "false") :
#replacement has length zero
The above is the error in the "RGui". The error message in "RStudio" is differet:
#object of type 'closure' is not subsettable
and the browser does not fire at all. The HTTP server works fine since I can simply call help pages.( for example ?googleVis will fire up the browser and give the help page). The "Geo" object in the code above is fine and contains the html code only that the plot() does not do what it is supposed to do (I can manually run the html file in the temp folder and see the results). The example above is available here.
I would appreciate your clues.
Thank you
This is the result of correspondance with the the Authors of the packages. It seems that there was a bug that prevented the plot to work properly. The released a new version. You can find the link below.
TQ
.....
Yesterday evening I realised that with version 0.3.0 of googleVis I unfortunately introduced a bug in RStudio and R on Windows.
The bug has been fixed already and a new version (0.3.1) is available from our project site (here is the link), but not on CRAN yet.
I have put a note on my blog (here is the link) to inform others as well.
I hope this helps.
Best regards
Markus

Resources