R code on Google docs

后端 未结 4 1013
既然无缘
既然无缘 2021-02-10 02:47

I\'m aware that you can publish spreadsheets on Google docs and then import them in R. However, let\'s say I have a function or some code I want to read in R (like in the sourc

4条回答
  •  醉酒成梦
    2021-02-10 03:18

    I also think that the best way is to use github or dropbox. But using the RGoogleDocs and XML packages we can parse the code (I'm not very experienced) with XML parsing may be someone will have better code.

    ### require(devtools);dev_mode(TRUE, .libPaths()[1]);install_github("RGoogleDocs", "duncantl")
    require(RGoogleDocs) 
    require(XML)
    
    auth <- getGoogleAuth("dicko.ahmadou@gmail.com", "*********")
    
    con <- getGoogleDocsConnection(auth)
    
    mydoc <- getDocs(con)
    
    ## I put star for confidentiality
    ## Your doc is in 10th position
    names(mydoc)
    
    ##  [1] "*********"                                 
    ##  [2] "*********"                             
    ##  [3] "panel_tp_transferts"                                      
    ##  [4] "txint"                                                    
    ##  [5] "avortementsuivisen"                                       
    ##  [6] "Untitled Document"                                        
    ##  [7] "copie de villages_emprise10km"
    ##  [8] "AéroportBlaiseDiagne_AFDB.pdf"                            
    ##  [9] "strassen_eng.pdf"                                         
    ## [10] "R_script_CO2_emissions_airborne"  
    
    
    rcode <- mydoc[[10]]
    rcode <- getDocContent(rcode, con)
    ## remove Non break space in the document (there are plenty of them...)
    rcode <- gsub(" ", " ", rcode)
    rcode <- htmlParse(rcode, asText = TRUE)
    rcodecontent <- xpathApply(rcode, "/html//body//p//span")
    rcodecontent <- sapply(rcodecontent, function(x) unname(xmlSApply(x, xmlValue))
    

    Now we can save the code in a file

    ### save the script in my dropbox folder (dropbox is very easy to use...)
    cat(sapply(rcodecontent, function(x) paste(x, "\n")), 
           file = "/home/ahmadou/Dropbox/Public/code.R")
    
    ### retrieve the public link
    oldwd <- getwd()
    setwd("/home/ahmadou/Dropbox/Public")
    system('dropbox puburl code.R', intern = TRUE)
    [1] "https://dl.dropbox.com/u/8750577/code.R"
    
    setwd(oldw)
    

    Here is the link for the code

提交回复
热议问题