How to use R with Google Colaboratory?

后端 未结 6 1477
误落风尘
误落风尘 2020-11-29 16:59

Google Colaboratory supports Python version 2.7 and 3.6

I see an example how to use Swift in Colab a while ago.

Today, I happened to run

!jup         


        
相关标签:
6条回答
  • 2020-11-29 17:14

    Update: this doesn't work anymore (july 2020).

    The above link on answers above takes directly to R notebook, there you have an option change between R or python. It is strange that Google is changing services just like this. Hence stackoverflow not a great platform to promote tools created by profit mongering/data-selling companies.

    Old answer:

    As of now if you click at Runtime on menu bar then choose Change Runtime Type, you can choose between R or Python.

    0 讨论(0)
  • 2020-11-29 17:15

    Open this link in your browser to create a new notebook with R Kernel

    https://colab.research.google.com/notebook#create=true&language=r

    0 讨论(0)
  • 2020-11-29 17:21

    In case you want to use Python and R together, you can use R magic for some cells.

    # activate R magic
    %load_ext rpy2.ipython
    

    Then, whenever you want to use R, you begin the cell with %%R

    %%R
    x <- 42
    print(x)
    

    More details in rpy2 documentation

    0 讨论(0)
  • 2020-11-29 17:23

    Yes.

    For a new R-notebook, use this link. (shorthand is colab.to/r )

    You can learn from IRkernel demos, e.g. demo.ipynb

    Save a copy in your Google Drive, and make any changes you need.

    2 more demos:

    • Display.ipynb shows how to display HTML, images.
    • Comm_Demo.ipynb shows how to communicate between R and JavaScript.

    See more details in IRkernel Github.

    0 讨论(0)
  • 2020-11-29 17:27

    To expand on a previous answer, here's how you can move dataframes between the R and Python kernels so you can work with both in the same notebook (for example, if you want to load data in with Pandas, process it with an R package, and then plot it with Bokeh).

    # Pandas dataframe to R data frame
    !pip3 install rpy2
    %load_ext rpy2.ipython
    %R -i df
    
    # R data frame to Pandas dataframe
    %R seq.data <- read.delim('sequence.index', header=TRUE, stringsAsFactors=FALSE)
    seq_data = %R seq.data
    
    0 讨论(0)
  • 2020-11-29 17:28

    *****Working as of Friday 13th November 2020

    Go this URL https://colab.to/r whilst signed into colab and that should do it.

    You can check if R in Runtime -> Change runtime type, but it should already be setup.

    To mount google drive:

    install.packages("googledrive")
    library("googledrive")
    
    if (file.exists("/usr/local/lib/python3.6/dist-packages/google/colab/_ipython.py")){ 
      install.packages("R.utils")
      library("R.utils")
      library("httr")
      my_check <- function() {return(TRUE)}
      reassignInPackage("is_interactive", pkgName = "httr", my_check)
      options(rlang_interactive=TRUE)
    }                                                                                    
    

    And authenticate google drive

    drive_auth(use_oob = TRUE, cache = TRUE)
    
    0 讨论(0)
提交回复
热议问题