Giter VIP home page Giter VIP logo

Comments (5)

gagolews avatar gagolews commented on September 10, 2024

for []. you may overload the [.classname operator in R.

> x <- structure("test", class="testclass")
> "[.testclass" <- function(x, i)
+ stringi::stri_sub(x, i, length=1)
> x[1]
[1] "t"
> x[2]
[1] "e"

replacement version:

> "[<-.testclass" <- function(x, i, value) {
+ stringi::stri_sub(x, i, length=1) <- value
+ x
+ }
> x[1] <- ":P"
> x
[1] ":Pest"

vector of any R objects == list (in Rcpp, this will be std::vector<RObject>.

from datastructures.

bartoszukm avatar bartoszukm commented on September 10, 2024

Why do we use SEXP in stack and queue? Maybe we can also use RObject in stack and queue? Is it any difference?

from datastructures.

gagolews avatar gagolews commented on September 10, 2024

On 20.03.2015 09:38, Maciej Bartoszuk wrote:

Why do we use SEXP in stack and queue? Maybe we can also use RObject in
stack and queue? Is it any difference?

RObject is a thin wrapper around an underlying SEXP. When storing data
is concerned only , SEXP is better (more lightweight)

from datastructures.

bartoszukm avatar bartoszukm commented on September 10, 2024

Ok, I committed vector class in 1196208 .

Unfortunately, there are some issues in getting/setting one element, like at() or operator[].

Firstly, I tried as you said

"[.Vector" <- function(vec, i)
 {
    vector_at(vec,i) 
 }

 "[.Vector" <- function(vec, i, value)
 {
    vector_at(vec,i) = value #it's not working alone either, because R do not know about C++ references
 }

Unfortunately, R is detecting that it is an external pointer and forbid subsetting:

mv = vector_create(4)
mv[1]=5
mv[1]

Error in mv[1] = 5 : object of type 'externalptr' is not subsettable
Error in mv[1] : object of type 'externalptr' is not subsettable

After that I tried to make something like that:

"vector_at<-" <- function(vec, i, value) {
    print(vec)
    print(i)
    print(value)
    vector_set_at(vec,i,value)
    print(vec)
    print(i)
    print(value)
 }

What is interesting, in this function everything is working fine, but... after function ends and returning to main code, which called "vector_at<-"... vector changes to random number, like NULL or 5.

Do you have any ideas?

For now, one can use vector_set_at(vec,i,value) and vector_at(vec,i) for setting and getting item at index i.

from datastructures.

gagolews avatar gagolews commented on September 10, 2024

Try setting  the class attribute by callling the class() function

Marek Gagolewski, PhD

Sent from Type Mail

On Mar 20, 2015, 11:30, at 11:30, Maciej Bartoszuk [email protected] wrote:

Ok, I committed vector class in
1196208 .

Unfortunately, there are some issues in getting/setting one element,
like at() or operator[].

Firstly, I tried as you said

"[.Vector" <- function(vec, i)
{
   vector_at(vec,i) 
}

"[.Vector" <- function(vec, i, value)
{
vector_at(vec,i) = value #it's not working alone either, because R do
not know about C++ references
}

Unfortunately, R is detecting that it is an external pointer and forbid
subsetting:

mv = vector_create(4)
mv[1]=5
mv[1]

Error in mv[1] = 5 : object of type 'externalptr' is not subsettable
Error in mv[1] : object of type 'externalptr' is not subsettable

After that I tried to make something like that:

"vector_at<-" <- function(vec, i, value) {
   print(vec)
   print(i)
   print(value)
   vector_set_at(vec,i,value)
   print(vec)
   print(i)
   print(value)
}

What is interesting, in this function everything is working fine,
but... after function end and returning to main code, which called
"vector_at<-"... vector changes to random number, like NULL or 5.

Do you have any ideas?

For now, one can use vector_set_at(vec,i,value) and vector_at(vec,i)
for setting and getting item at index i.


Reply to this email directly or view it on GitHub:
#2 (comment)

from datastructures.

Related Issues (3)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.