Rcpp - can't access elements of StrinvgVector












0















http://gallery.rcpp.org/articles/working-with-Rcpp-StringVector/



I used the above link to try because I want to work with a string or character vector in R



however Rcpp is for some reason concatenating the elements of the vector I am using Rcout to try to understand what is happening but I have no idea what it is:



cppFunction('CharacterVector test(NumericMatrix h, NumericMatrix nt, StringVector d, int r){

CharacterVector m(h.ncol());
Function f("paste0");
for(int i = 0; i < d.size(); i++){
Rcout << d[i];
}

return m;
}')

h <- matrix(0,nrow=2, ncol =2)
colnames(h) <- c("A", "B")
nt <- matrix(0,nrow=2, ncol =2)

d <- c("2019-03", "2014-04")
test(h, nt, d, 1)


the output of Rcout is:



2019-032014-04[1] "" ""


in stead of:



"2019-03" "2014-04"


Why is this happening ?










share|improve this question


















  • 3





    Rcout prints bits to the stdout, in this case the R console. Why would you think it'd automagically put double-quotes around it for you. Rcout doens't return values, just prints thingsl

    – hrbrmstr
    Nov 23 '18 at 21:50











  • sry as i am new I didn't understand why they are stuck to each other so I thought they were being concatenated the two pairs of quotation marks after the first output didn't help me at all

    – user9396820
    Nov 29 '18 at 16:40


















0















http://gallery.rcpp.org/articles/working-with-Rcpp-StringVector/



I used the above link to try because I want to work with a string or character vector in R



however Rcpp is for some reason concatenating the elements of the vector I am using Rcout to try to understand what is happening but I have no idea what it is:



cppFunction('CharacterVector test(NumericMatrix h, NumericMatrix nt, StringVector d, int r){

CharacterVector m(h.ncol());
Function f("paste0");
for(int i = 0; i < d.size(); i++){
Rcout << d[i];
}

return m;
}')

h <- matrix(0,nrow=2, ncol =2)
colnames(h) <- c("A", "B")
nt <- matrix(0,nrow=2, ncol =2)

d <- c("2019-03", "2014-04")
test(h, nt, d, 1)


the output of Rcout is:



2019-032014-04[1] "" ""


in stead of:



"2019-03" "2014-04"


Why is this happening ?










share|improve this question


















  • 3





    Rcout prints bits to the stdout, in this case the R console. Why would you think it'd automagically put double-quotes around it for you. Rcout doens't return values, just prints thingsl

    – hrbrmstr
    Nov 23 '18 at 21:50











  • sry as i am new I didn't understand why they are stuck to each other so I thought they were being concatenated the two pairs of quotation marks after the first output didn't help me at all

    – user9396820
    Nov 29 '18 at 16:40
















0












0








0








http://gallery.rcpp.org/articles/working-with-Rcpp-StringVector/



I used the above link to try because I want to work with a string or character vector in R



however Rcpp is for some reason concatenating the elements of the vector I am using Rcout to try to understand what is happening but I have no idea what it is:



cppFunction('CharacterVector test(NumericMatrix h, NumericMatrix nt, StringVector d, int r){

CharacterVector m(h.ncol());
Function f("paste0");
for(int i = 0; i < d.size(); i++){
Rcout << d[i];
}

return m;
}')

h <- matrix(0,nrow=2, ncol =2)
colnames(h) <- c("A", "B")
nt <- matrix(0,nrow=2, ncol =2)

d <- c("2019-03", "2014-04")
test(h, nt, d, 1)


the output of Rcout is:



2019-032014-04[1] "" ""


in stead of:



"2019-03" "2014-04"


Why is this happening ?










share|improve this question














http://gallery.rcpp.org/articles/working-with-Rcpp-StringVector/



I used the above link to try because I want to work with a string or character vector in R



however Rcpp is for some reason concatenating the elements of the vector I am using Rcout to try to understand what is happening but I have no idea what it is:



cppFunction('CharacterVector test(NumericMatrix h, NumericMatrix nt, StringVector d, int r){

CharacterVector m(h.ncol());
Function f("paste0");
for(int i = 0; i < d.size(); i++){
Rcout << d[i];
}

return m;
}')

h <- matrix(0,nrow=2, ncol =2)
colnames(h) <- c("A", "B")
nt <- matrix(0,nrow=2, ncol =2)

d <- c("2019-03", "2014-04")
test(h, nt, d, 1)


the output of Rcout is:



2019-032014-04[1] "" ""


in stead of:



"2019-03" "2014-04"


Why is this happening ?







r string rcpp






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 21:35









user9396820user9396820

84




84








  • 3





    Rcout prints bits to the stdout, in this case the R console. Why would you think it'd automagically put double-quotes around it for you. Rcout doens't return values, just prints thingsl

    – hrbrmstr
    Nov 23 '18 at 21:50











  • sry as i am new I didn't understand why they are stuck to each other so I thought they were being concatenated the two pairs of quotation marks after the first output didn't help me at all

    – user9396820
    Nov 29 '18 at 16:40
















  • 3





    Rcout prints bits to the stdout, in this case the R console. Why would you think it'd automagically put double-quotes around it for you. Rcout doens't return values, just prints thingsl

    – hrbrmstr
    Nov 23 '18 at 21:50











  • sry as i am new I didn't understand why they are stuck to each other so I thought they were being concatenated the two pairs of quotation marks after the first output didn't help me at all

    – user9396820
    Nov 29 '18 at 16:40










3




3





Rcout prints bits to the stdout, in this case the R console. Why would you think it'd automagically put double-quotes around it for you. Rcout doens't return values, just prints thingsl

– hrbrmstr
Nov 23 '18 at 21:50





Rcout prints bits to the stdout, in this case the R console. Why would you think it'd automagically put double-quotes around it for you. Rcout doens't return values, just prints thingsl

– hrbrmstr
Nov 23 '18 at 21:50













sry as i am new I didn't understand why they are stuck to each other so I thought they were being concatenated the two pairs of quotation marks after the first output didn't help me at all

– user9396820
Nov 29 '18 at 16:40







sry as i am new I didn't understand why they are stuck to each other so I thought they were being concatenated the two pairs of quotation marks after the first output didn't help me at all

– user9396820
Nov 29 '18 at 16:40














1 Answer
1






active

oldest

votes


















2














If you want a space after each element you send to Rcpp::Rcout, you have to tell it so. You need to change



Rcout << d[i];


to



Rcout << d[i] << " ";


Also, as I now notice thanks to hrbrmstr's comment, you also want quotation marks around each element when they're printed. Again, if you want quotation marks, you have to tell that to Rcout, it doesn't happen automatically. Then, you'd further modify the aforementioned line to



Rcout << """ << d[i] << "" ";


I would also add a new line before the function ends. So, let's compare; I have my C++ code in the file so-answer.cpp:



#include <Rcpp.h>

using namespace Rcpp;

// [[Rcpp::export]]
CharacterVector test(NumericMatrix h, NumericMatrix nt, StringVector d, int r){

CharacterVector m(h.ncol());
Function f("paste0");
for(int i = 0; i < d.size(); i++){
Rcout << d[i];
}

return m;
}

// [[Rcpp::export]]
CharacterVector test2(NumericMatrix h, NumericMatrix nt, StringVector d, int r){

CharacterVector m(h.ncol());
Function f("paste0");
for(int i = 0; i < d.size(); i++){
Rcout << """ << d[i] << "" ";
}

Rcout << "n";

return m;
}

/*** R
h <- matrix(0,nrow=2, ncol =2)
colnames(h) <- c("A", "B")
nt <- matrix(0,nrow=2, ncol =2)

d <- c("2019-03", "2014-04")
test(h, nt, d, 1)
test2(h, nt, d, 1)
*/


Then when I use Rcpp::sourceCpp() to compile and expose to R:



Rcpp::sourceCpp("so-answer.cpp")
#>
#> > h <- matrix(0,nrow=2, ncol =2)
#>
#> > colnames(h) <- c("A", "B")
#>
#> > nt <- matrix(0,nrow=2, ncol =2)
#>
#> > d <- c("2019-03", "2014-04")
#>
#> > test(h, nt, d, 1)
#> 2019-032014-04[1] "" ""
#>
#> > test2(h, nt, d, 1)
#> "2019-03" "2014-04"
#> [1] "" ""


Created on 2018-11-23 by the reprex package (v0.2.1)



I'll also note that I'm note sure what all the superfluous code is there for, but I just left it in.






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53453182%2frcpp-cant-access-elements-of-strinvgvector%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    If you want a space after each element you send to Rcpp::Rcout, you have to tell it so. You need to change



    Rcout << d[i];


    to



    Rcout << d[i] << " ";


    Also, as I now notice thanks to hrbrmstr's comment, you also want quotation marks around each element when they're printed. Again, if you want quotation marks, you have to tell that to Rcout, it doesn't happen automatically. Then, you'd further modify the aforementioned line to



    Rcout << """ << d[i] << "" ";


    I would also add a new line before the function ends. So, let's compare; I have my C++ code in the file so-answer.cpp:



    #include <Rcpp.h>

    using namespace Rcpp;

    // [[Rcpp::export]]
    CharacterVector test(NumericMatrix h, NumericMatrix nt, StringVector d, int r){

    CharacterVector m(h.ncol());
    Function f("paste0");
    for(int i = 0; i < d.size(); i++){
    Rcout << d[i];
    }

    return m;
    }

    // [[Rcpp::export]]
    CharacterVector test2(NumericMatrix h, NumericMatrix nt, StringVector d, int r){

    CharacterVector m(h.ncol());
    Function f("paste0");
    for(int i = 0; i < d.size(); i++){
    Rcout << """ << d[i] << "" ";
    }

    Rcout << "n";

    return m;
    }

    /*** R
    h <- matrix(0,nrow=2, ncol =2)
    colnames(h) <- c("A", "B")
    nt <- matrix(0,nrow=2, ncol =2)

    d <- c("2019-03", "2014-04")
    test(h, nt, d, 1)
    test2(h, nt, d, 1)
    */


    Then when I use Rcpp::sourceCpp() to compile and expose to R:



    Rcpp::sourceCpp("so-answer.cpp")
    #>
    #> > h <- matrix(0,nrow=2, ncol =2)
    #>
    #> > colnames(h) <- c("A", "B")
    #>
    #> > nt <- matrix(0,nrow=2, ncol =2)
    #>
    #> > d <- c("2019-03", "2014-04")
    #>
    #> > test(h, nt, d, 1)
    #> 2019-032014-04[1] "" ""
    #>
    #> > test2(h, nt, d, 1)
    #> "2019-03" "2014-04"
    #> [1] "" ""


    Created on 2018-11-23 by the reprex package (v0.2.1)



    I'll also note that I'm note sure what all the superfluous code is there for, but I just left it in.






    share|improve this answer






























      2














      If you want a space after each element you send to Rcpp::Rcout, you have to tell it so. You need to change



      Rcout << d[i];


      to



      Rcout << d[i] << " ";


      Also, as I now notice thanks to hrbrmstr's comment, you also want quotation marks around each element when they're printed. Again, if you want quotation marks, you have to tell that to Rcout, it doesn't happen automatically. Then, you'd further modify the aforementioned line to



      Rcout << """ << d[i] << "" ";


      I would also add a new line before the function ends. So, let's compare; I have my C++ code in the file so-answer.cpp:



      #include <Rcpp.h>

      using namespace Rcpp;

      // [[Rcpp::export]]
      CharacterVector test(NumericMatrix h, NumericMatrix nt, StringVector d, int r){

      CharacterVector m(h.ncol());
      Function f("paste0");
      for(int i = 0; i < d.size(); i++){
      Rcout << d[i];
      }

      return m;
      }

      // [[Rcpp::export]]
      CharacterVector test2(NumericMatrix h, NumericMatrix nt, StringVector d, int r){

      CharacterVector m(h.ncol());
      Function f("paste0");
      for(int i = 0; i < d.size(); i++){
      Rcout << """ << d[i] << "" ";
      }

      Rcout << "n";

      return m;
      }

      /*** R
      h <- matrix(0,nrow=2, ncol =2)
      colnames(h) <- c("A", "B")
      nt <- matrix(0,nrow=2, ncol =2)

      d <- c("2019-03", "2014-04")
      test(h, nt, d, 1)
      test2(h, nt, d, 1)
      */


      Then when I use Rcpp::sourceCpp() to compile and expose to R:



      Rcpp::sourceCpp("so-answer.cpp")
      #>
      #> > h <- matrix(0,nrow=2, ncol =2)
      #>
      #> > colnames(h) <- c("A", "B")
      #>
      #> > nt <- matrix(0,nrow=2, ncol =2)
      #>
      #> > d <- c("2019-03", "2014-04")
      #>
      #> > test(h, nt, d, 1)
      #> 2019-032014-04[1] "" ""
      #>
      #> > test2(h, nt, d, 1)
      #> "2019-03" "2014-04"
      #> [1] "" ""


      Created on 2018-11-23 by the reprex package (v0.2.1)



      I'll also note that I'm note sure what all the superfluous code is there for, but I just left it in.






      share|improve this answer




























        2












        2








        2







        If you want a space after each element you send to Rcpp::Rcout, you have to tell it so. You need to change



        Rcout << d[i];


        to



        Rcout << d[i] << " ";


        Also, as I now notice thanks to hrbrmstr's comment, you also want quotation marks around each element when they're printed. Again, if you want quotation marks, you have to tell that to Rcout, it doesn't happen automatically. Then, you'd further modify the aforementioned line to



        Rcout << """ << d[i] << "" ";


        I would also add a new line before the function ends. So, let's compare; I have my C++ code in the file so-answer.cpp:



        #include <Rcpp.h>

        using namespace Rcpp;

        // [[Rcpp::export]]
        CharacterVector test(NumericMatrix h, NumericMatrix nt, StringVector d, int r){

        CharacterVector m(h.ncol());
        Function f("paste0");
        for(int i = 0; i < d.size(); i++){
        Rcout << d[i];
        }

        return m;
        }

        // [[Rcpp::export]]
        CharacterVector test2(NumericMatrix h, NumericMatrix nt, StringVector d, int r){

        CharacterVector m(h.ncol());
        Function f("paste0");
        for(int i = 0; i < d.size(); i++){
        Rcout << """ << d[i] << "" ";
        }

        Rcout << "n";

        return m;
        }

        /*** R
        h <- matrix(0,nrow=2, ncol =2)
        colnames(h) <- c("A", "B")
        nt <- matrix(0,nrow=2, ncol =2)

        d <- c("2019-03", "2014-04")
        test(h, nt, d, 1)
        test2(h, nt, d, 1)
        */


        Then when I use Rcpp::sourceCpp() to compile and expose to R:



        Rcpp::sourceCpp("so-answer.cpp")
        #>
        #> > h <- matrix(0,nrow=2, ncol =2)
        #>
        #> > colnames(h) <- c("A", "B")
        #>
        #> > nt <- matrix(0,nrow=2, ncol =2)
        #>
        #> > d <- c("2019-03", "2014-04")
        #>
        #> > test(h, nt, d, 1)
        #> 2019-032014-04[1] "" ""
        #>
        #> > test2(h, nt, d, 1)
        #> "2019-03" "2014-04"
        #> [1] "" ""


        Created on 2018-11-23 by the reprex package (v0.2.1)



        I'll also note that I'm note sure what all the superfluous code is there for, but I just left it in.






        share|improve this answer















        If you want a space after each element you send to Rcpp::Rcout, you have to tell it so. You need to change



        Rcout << d[i];


        to



        Rcout << d[i] << " ";


        Also, as I now notice thanks to hrbrmstr's comment, you also want quotation marks around each element when they're printed. Again, if you want quotation marks, you have to tell that to Rcout, it doesn't happen automatically. Then, you'd further modify the aforementioned line to



        Rcout << """ << d[i] << "" ";


        I would also add a new line before the function ends. So, let's compare; I have my C++ code in the file so-answer.cpp:



        #include <Rcpp.h>

        using namespace Rcpp;

        // [[Rcpp::export]]
        CharacterVector test(NumericMatrix h, NumericMatrix nt, StringVector d, int r){

        CharacterVector m(h.ncol());
        Function f("paste0");
        for(int i = 0; i < d.size(); i++){
        Rcout << d[i];
        }

        return m;
        }

        // [[Rcpp::export]]
        CharacterVector test2(NumericMatrix h, NumericMatrix nt, StringVector d, int r){

        CharacterVector m(h.ncol());
        Function f("paste0");
        for(int i = 0; i < d.size(); i++){
        Rcout << """ << d[i] << "" ";
        }

        Rcout << "n";

        return m;
        }

        /*** R
        h <- matrix(0,nrow=2, ncol =2)
        colnames(h) <- c("A", "B")
        nt <- matrix(0,nrow=2, ncol =2)

        d <- c("2019-03", "2014-04")
        test(h, nt, d, 1)
        test2(h, nt, d, 1)
        */


        Then when I use Rcpp::sourceCpp() to compile and expose to R:



        Rcpp::sourceCpp("so-answer.cpp")
        #>
        #> > h <- matrix(0,nrow=2, ncol =2)
        #>
        #> > colnames(h) <- c("A", "B")
        #>
        #> > nt <- matrix(0,nrow=2, ncol =2)
        #>
        #> > d <- c("2019-03", "2014-04")
        #>
        #> > test(h, nt, d, 1)
        #> 2019-032014-04[1] "" ""
        #>
        #> > test2(h, nt, d, 1)
        #> "2019-03" "2014-04"
        #> [1] "" ""


        Created on 2018-11-23 by the reprex package (v0.2.1)



        I'll also note that I'm note sure what all the superfluous code is there for, but I just left it in.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 23 '18 at 23:10

























        answered Nov 23 '18 at 21:48









        duckmayrduckmayr

        7,57311326




        7,57311326
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53453182%2frcpp-cant-access-elements-of-strinvgvector%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Wiesbaden

            Marschland

            Dieringhausen