Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating LB_lengths objects from data inside R #7

Closed
iagomosqueira opened this issue Jan 8, 2020 · 5 comments
Closed

Creating LB_lengths objects from data inside R #7

iagomosqueira opened this issue Jan 8, 2020 · 5 comments

Comments

@iagomosqueira
Copy link

iagomosqueira commented Jan 8, 2020

If I am not mistaken, the initialize method for LB_lengths is designed to be used with data in files, but it becomes difficult to use it inside R when the length samples are in an R object in the workspace.

I am trying to run LBSPR inside some MSE simulations, and I seem to be forced to create an empty object and then populate the stocks directly.

My view is that the kind of input juggling that it is being done should be carried out by a creator method/function, LB_lengths(), while a call to new() should be able to simply assign named inputs to the corresponding slot.

@AdrianHordyk
Copy link
Owner

I'm not clear on the problem. Can you expand on it with a reproducible example of what you mean?

The recommended approach to creating LB_lengths objects in R is:

LenData <- new('LB_lengths', verbose=FALSE)
LenData@LMids <- LenMids
LenData@LData <- LengthComps
etc

@iagomosqueira
Copy link
Author

I would expect new() to take any named object passed to it and place it in the right slot. So

LenData <- new('LB_lengths', verbose=FALSE, LMids=seq(1,10))

should place that vector in LMids

> LenData@LMids
logical(0)

Our MSE code, for example, parses various objects and then constructs a list to then call new, or the creator function if available, using do.call. So being able to construct an object on a single command is quite useful. I am writing a creator function to use there, but I thought it could be something worth considering.

@AdrianHordyk
Copy link
Owner

This can be resolved by creating a wrapper function, eg:

LB_lengths <- function(LMids, etc) {
  Obj <- new('LB_lengths', verbose=FALSE)
  Obj@LMids <- LMids
  etc
  Obj
}

I'll consider adding to the package sometime, but as it isn't under active development it's probably easiest if you write the wrapper function in your own code

@iagomosqueira
Copy link
Author

Sure, no need to worry about it if the package is not being developed further. What I have is

LB_lengths <- function(...) {
  args <- list(...)
  res <- new('LB_lengths', verbose=FALSE)
  for(i in names(args))
    slot(res, i) <- args[[i]]
  return(res)
}

@AdrianHordyk
Copy link
Owner

Sounds good. I'll incorporate in the package at some stage when I do an update.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants