Converting Pandas DataFrame to R DataFrame using Reticulate in RMarkdown is Inconsistent












0















I’m using RMarkdown with the reticulate package and often have the requirement to print pandas DataFrame objects using R packages such as Kable. Unfortunately, the conversion appears to work intermittently when Knitting the document. Again, sometimes it works, sometimes it doesn’t.



Here's a reproducible example of RMarkdown:



output: html_document

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{python}
import pandas as pd
df = pd.DataFrame({'a':4, 'b':5, 'c':9}, index=[0])
print(df)
```

```{r}
library(reticulate)
df2 <- reticulate::py$df
print(df2)
print(reticulate::py$df)
```


Actual Behavior:



import pandas as pd
df = pd.DataFrame({'a':4, 'b':5, 'c':9}, index=[0])
print(df)
## a b c
## 0 4 5 9
library(reticulate)
df2 <- reticulate::py$df
print(df2)
## a b
## 1 <environment: 0x000000001d8d9fa0> <environment: 0x000000001d9cd630>
## c
## 1 <environment: 0x000000001dac0d30>
print(reticulate::py$df)
## a b
## 1 <environment: 0x000000001dde8a58> <environment: 0x000000001dedac08>
## c
## 1 <environment: 0x000000001e0119e0>


Expected Behavior:
The Pandas DataFrame object should be converted to an R DataFrame object and printed as follows:



##    a  b  c
## 0 4 5 9


Would appreciate any guidance on this issue as the actual behavior I cited above doesn't occur all the time.



Here's the session information:



## R version 3.5.2 (2018-12-20)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 17134)
##
## Matrix products: default
##
## locale:
## [1] LC_COLLATE=English_United States.1252
## [2] LC_CTYPE=English_United States.1252
## [3] LC_MONETARY=English_United States.1252
## [4] LC_NUMERIC=C
## [5] LC_TIME=English_United States.1252
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] reticulate_1.10.0.9004
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.0 lattice_0.20-38 digest_0.6.16 rprojroot_1.3-2
## [5] grid_3.5.2 jsonlite_1.6 backports_1.1.2 magrittr_1.5
## [9] evaluate_0.11 stringi_1.1.7 Matrix_1.2-15 rmarkdown_1.10
## [13] tools_3.5.2 stringr_1.3.1 yaml_2.2.0 compiler_3.5.2
## [17] htmltools_0.3.6 knitr_1.20









share|improve this question





























    0















    I’m using RMarkdown with the reticulate package and often have the requirement to print pandas DataFrame objects using R packages such as Kable. Unfortunately, the conversion appears to work intermittently when Knitting the document. Again, sometimes it works, sometimes it doesn’t.



    Here's a reproducible example of RMarkdown:



    output: html_document

    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = TRUE)
    ```

    ```{python}
    import pandas as pd
    df = pd.DataFrame({'a':4, 'b':5, 'c':9}, index=[0])
    print(df)
    ```

    ```{r}
    library(reticulate)
    df2 <- reticulate::py$df
    print(df2)
    print(reticulate::py$df)
    ```


    Actual Behavior:



    import pandas as pd
    df = pd.DataFrame({'a':4, 'b':5, 'c':9}, index=[0])
    print(df)
    ## a b c
    ## 0 4 5 9
    library(reticulate)
    df2 <- reticulate::py$df
    print(df2)
    ## a b
    ## 1 <environment: 0x000000001d8d9fa0> <environment: 0x000000001d9cd630>
    ## c
    ## 1 <environment: 0x000000001dac0d30>
    print(reticulate::py$df)
    ## a b
    ## 1 <environment: 0x000000001dde8a58> <environment: 0x000000001dedac08>
    ## c
    ## 1 <environment: 0x000000001e0119e0>


    Expected Behavior:
    The Pandas DataFrame object should be converted to an R DataFrame object and printed as follows:



    ##    a  b  c
    ## 0 4 5 9


    Would appreciate any guidance on this issue as the actual behavior I cited above doesn't occur all the time.



    Here's the session information:



    ## R version 3.5.2 (2018-12-20)
    ## Platform: x86_64-w64-mingw32/x64 (64-bit)
    ## Running under: Windows 10 x64 (build 17134)
    ##
    ## Matrix products: default
    ##
    ## locale:
    ## [1] LC_COLLATE=English_United States.1252
    ## [2] LC_CTYPE=English_United States.1252
    ## [3] LC_MONETARY=English_United States.1252
    ## [4] LC_NUMERIC=C
    ## [5] LC_TIME=English_United States.1252
    ##
    ## attached base packages:
    ## [1] stats graphics grDevices utils datasets methods base
    ##
    ## other attached packages:
    ## [1] reticulate_1.10.0.9004
    ##
    ## loaded via a namespace (and not attached):
    ## [1] Rcpp_1.0.0 lattice_0.20-38 digest_0.6.16 rprojroot_1.3-2
    ## [5] grid_3.5.2 jsonlite_1.6 backports_1.1.2 magrittr_1.5
    ## [9] evaluate_0.11 stringi_1.1.7 Matrix_1.2-15 rmarkdown_1.10
    ## [13] tools_3.5.2 stringr_1.3.1 yaml_2.2.0 compiler_3.5.2
    ## [17] htmltools_0.3.6 knitr_1.20









    share|improve this question



























      0












      0








      0








      I’m using RMarkdown with the reticulate package and often have the requirement to print pandas DataFrame objects using R packages such as Kable. Unfortunately, the conversion appears to work intermittently when Knitting the document. Again, sometimes it works, sometimes it doesn’t.



      Here's a reproducible example of RMarkdown:



      output: html_document

      ```{r setup, include=FALSE}
      knitr::opts_chunk$set(echo = TRUE)
      ```

      ```{python}
      import pandas as pd
      df = pd.DataFrame({'a':4, 'b':5, 'c':9}, index=[0])
      print(df)
      ```

      ```{r}
      library(reticulate)
      df2 <- reticulate::py$df
      print(df2)
      print(reticulate::py$df)
      ```


      Actual Behavior:



      import pandas as pd
      df = pd.DataFrame({'a':4, 'b':5, 'c':9}, index=[0])
      print(df)
      ## a b c
      ## 0 4 5 9
      library(reticulate)
      df2 <- reticulate::py$df
      print(df2)
      ## a b
      ## 1 <environment: 0x000000001d8d9fa0> <environment: 0x000000001d9cd630>
      ## c
      ## 1 <environment: 0x000000001dac0d30>
      print(reticulate::py$df)
      ## a b
      ## 1 <environment: 0x000000001dde8a58> <environment: 0x000000001dedac08>
      ## c
      ## 1 <environment: 0x000000001e0119e0>


      Expected Behavior:
      The Pandas DataFrame object should be converted to an R DataFrame object and printed as follows:



      ##    a  b  c
      ## 0 4 5 9


      Would appreciate any guidance on this issue as the actual behavior I cited above doesn't occur all the time.



      Here's the session information:



      ## R version 3.5.2 (2018-12-20)
      ## Platform: x86_64-w64-mingw32/x64 (64-bit)
      ## Running under: Windows 10 x64 (build 17134)
      ##
      ## Matrix products: default
      ##
      ## locale:
      ## [1] LC_COLLATE=English_United States.1252
      ## [2] LC_CTYPE=English_United States.1252
      ## [3] LC_MONETARY=English_United States.1252
      ## [4] LC_NUMERIC=C
      ## [5] LC_TIME=English_United States.1252
      ##
      ## attached base packages:
      ## [1] stats graphics grDevices utils datasets methods base
      ##
      ## other attached packages:
      ## [1] reticulate_1.10.0.9004
      ##
      ## loaded via a namespace (and not attached):
      ## [1] Rcpp_1.0.0 lattice_0.20-38 digest_0.6.16 rprojroot_1.3-2
      ## [5] grid_3.5.2 jsonlite_1.6 backports_1.1.2 magrittr_1.5
      ## [9] evaluate_0.11 stringi_1.1.7 Matrix_1.2-15 rmarkdown_1.10
      ## [13] tools_3.5.2 stringr_1.3.1 yaml_2.2.0 compiler_3.5.2
      ## [17] htmltools_0.3.6 knitr_1.20









      share|improve this question
















      I’m using RMarkdown with the reticulate package and often have the requirement to print pandas DataFrame objects using R packages such as Kable. Unfortunately, the conversion appears to work intermittently when Knitting the document. Again, sometimes it works, sometimes it doesn’t.



      Here's a reproducible example of RMarkdown:



      output: html_document

      ```{r setup, include=FALSE}
      knitr::opts_chunk$set(echo = TRUE)
      ```

      ```{python}
      import pandas as pd
      df = pd.DataFrame({'a':4, 'b':5, 'c':9}, index=[0])
      print(df)
      ```

      ```{r}
      library(reticulate)
      df2 <- reticulate::py$df
      print(df2)
      print(reticulate::py$df)
      ```


      Actual Behavior:



      import pandas as pd
      df = pd.DataFrame({'a':4, 'b':5, 'c':9}, index=[0])
      print(df)
      ## a b c
      ## 0 4 5 9
      library(reticulate)
      df2 <- reticulate::py$df
      print(df2)
      ## a b
      ## 1 <environment: 0x000000001d8d9fa0> <environment: 0x000000001d9cd630>
      ## c
      ## 1 <environment: 0x000000001dac0d30>
      print(reticulate::py$df)
      ## a b
      ## 1 <environment: 0x000000001dde8a58> <environment: 0x000000001dedac08>
      ## c
      ## 1 <environment: 0x000000001e0119e0>


      Expected Behavior:
      The Pandas DataFrame object should be converted to an R DataFrame object and printed as follows:



      ##    a  b  c
      ## 0 4 5 9


      Would appreciate any guidance on this issue as the actual behavior I cited above doesn't occur all the time.



      Here's the session information:



      ## R version 3.5.2 (2018-12-20)
      ## Platform: x86_64-w64-mingw32/x64 (64-bit)
      ## Running under: Windows 10 x64 (build 17134)
      ##
      ## Matrix products: default
      ##
      ## locale:
      ## [1] LC_COLLATE=English_United States.1252
      ## [2] LC_CTYPE=English_United States.1252
      ## [3] LC_MONETARY=English_United States.1252
      ## [4] LC_NUMERIC=C
      ## [5] LC_TIME=English_United States.1252
      ##
      ## attached base packages:
      ## [1] stats graphics grDevices utils datasets methods base
      ##
      ## other attached packages:
      ## [1] reticulate_1.10.0.9004
      ##
      ## loaded via a namespace (and not attached):
      ## [1] Rcpp_1.0.0 lattice_0.20-38 digest_0.6.16 rprojroot_1.3-2
      ## [5] grid_3.5.2 jsonlite_1.6 backports_1.1.2 magrittr_1.5
      ## [9] evaluate_0.11 stringi_1.1.7 Matrix_1.2-15 rmarkdown_1.10
      ## [13] tools_3.5.2 stringr_1.3.1 yaml_2.2.0 compiler_3.5.2
      ## [17] htmltools_0.3.6 knitr_1.20






      python r






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 14 at 1:31







      John

















      asked Feb 14 at 1:07









      JohnJohn

      13




      13






















          0






          active

          oldest

          votes











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "3"
          };
          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%2fsuperuser.com%2fquestions%2f1405502%2fconverting-pandas-dataframe-to-r-dataframe-using-reticulate-in-rmarkdown-is-inco%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Super User!


          • 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%2fsuperuser.com%2fquestions%2f1405502%2fconverting-pandas-dataframe-to-r-dataframe-using-reticulate-in-rmarkdown-is-inco%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

          Probability when a professor distributes a quiz and homework assignment to a class of n students.

          Aardman Animations

          Are they similar matrix