IDEMSInternational / R-Instat

A statistics software package powered by R
http://r-instat.org/
GNU General Public License v3.0
38 stars 102 forks source link

IDF package has R code that seems to cause problems #8663

Closed rdstern closed 10 months ago

rdstern commented 10 months ago

@lloyddewit We plan to add the IDF package for our WMO work. I keep looking for examples of code that are tricky to run - and the examples with this package just may be. Perhaps for a new reason, but probably not.

I provide 1 here. It seems to be the last line that causes the problem. When I comment it out the other lines run fine.

When it runs (using the reserve method, it gives an error and no graph. I used the current master. I havem't tried in RStudio.

### Name: IDF.plot
### Title: Plotting of IDF curves at a chosen station
### Aliases: IDF.plot

### ** Examples
# Load library "IDF"
library(package="IDF")

data('example',package = 'IDF')

# Save data frame(s) "example"
data_book$import_data(data_tables=list(example=example))

# fit d-gev
fit <- gev.d.fit(example$dat,example$d,ydat = as.matrix(example[,c("cov1","cov2")])
                 ,mutl = c(1,2),sigma0l = 1)
# get parameters for cov1 = 1, cov2 = 1
par <- gev.d.params(fit = fit, ydat = matrix(1,1,2))
# plot quantiles
IDF.plot(durations = seq(0.5,35,0.2),fitparams = par)
# add data points
points(example[example$cov1==1,]$d,example[example$cov1==1,]$dat)
lloyddewit commented 10 months ago

@rdstern Thank you! You found an excellent bug.

In theory, the code you provided above is not legal R. This is because the $ operator has a higher precedence than the [ operator (see extract from R documentation below). Therefore RScript evaluates the $ operator first, finds that there is no valid operand on the left-hand side and raises an error. According to the R documentation I think this is reasonable behaviour.

However, I ran the same code in RStudio and it ran successfully!

The short-term workaround is to add brackets to the left-hand [ operands of the $ operators to clarify the precedence (see code below). The code then runs successfully in R-Instat.

The question is: should we change RScript to be consistent with RStudio or should we be purist and follow the official R documentation?

I will close this issue because I moved it to lloyddewit/rscript#37.

### Name: IDF.plot
### Title: Plotting of IDF curves at a chosen station
### Aliases: IDF.plot

### ** Examples
# Load library "IDF"
library(package="IDF")

data('example',package = 'IDF')

# Save data frame(s) "example"
data_book$import_data(data_tables=list(example=example))

# fit d-gev
fit <- gev.d.fit(example$dat,example$d,ydat = as.matrix(example[,c("cov1","cov2")])
                 ,mutl = c(1,2),sigma0l = 1)
# get parameters for cov1 = 1, cov2 = 1
par <- gev.d.params(fit = fit, ydat = matrix(1,1,2))
# plot quantiles
IDF.plot(durations = seq(0.5,35,0.2),fitparams = par)
# add data points
#points(example[example$cov1==1,]$d,example[example$cov1==1,]$dat)
points((example[example$cov1==1,])$d,(example[example$cov1==1,])$dat)

image