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
Convert to data frame with Rcpp #857
Conversation
src/mrgsolve.cpp
Outdated
@@ -388,6 +388,19 @@ Rcpp::List EXPAND_OBSERVATIONS( | |||
Rcpp::Named("index") = index); | |||
} | |||
|
|||
Rcpp::List mat2df(Rcpp::NumericMatrix& x) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not familiar with Rcpp df API. Could you walk me through this function? Thanks. @kylebaron
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The pattern basically follows base::as.data.frame.matrix()
- Pass in a matrix
- create a list with the same number of slots as the matrix has columns
- Iterate through the matrix columns and copy into the list
Rcpp::_
is just a way to get all the rows (or all columns)- The
rn(2)
bit replicates what happens inbase::.set_row_names
- Set the class to data frame
- Add row names
- Return
as.data.frame.matrix()
does some other stuff but this does the essential steps for what I need.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So mat2df
doesn't take ownership of the matrix x
's data. Perhaps use const
to ensure x
is not modified? Like
Rcpp::List mat2df(Rcpp::NumericMatrix const& x) {
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me!
Thanks @yizhang-yiz |
Summary
This pr implements
as.data.frame.matrix
in Rcpp so that we can pass back a data frame from the simulation code. The changes in this PR are not user-facing.The implementation follows
base::as.data.frame.matrix()
.