Hello,

There is a series of functions to manipulate, search, edit R Wiki pages from within R that will be provided by the future 'rwiki' r package in development. Here is the function that does what you want in attachment.
Best,

Philippe Grosjean

Gabor Grothendieck wrote:
This was just posted to r-help:

   https://www.stat.math.ethz.ch/pipermail/r-help/2006-July/109931.html

Now suppose we wanted to run the code on the wiki page referenced
in that post.  Currently I copy and paste the code from that wiki page into an R
session (or else into a file, save it and source it).  It would be
convenient if it were possible to source the code for each of the code snippets
on the referenced wiki page directly:

   source("http://...whatever...";)

Is this already possible?
If so, how does one discover the url?

If not,it would be nice to have some feature to facilitate grabbing code
from the wiki.

_______________________________________________
R-sig-wiki mailing list
[email protected]
https://stat.ethz.ch/mailman/listinfo/r-sig-wiki


getWikiRcode <- function(wikipage, url = "http://wiki.r-project.org/rwiki";,
        strip.empty.lines = TRUE, strip.output = FALSE) {
        # Read the raw wiki page
        Url <- paste(url, "/doku.php?id=", wikipage, "&do=export_raw", sep ="")
        Raw <- readLines(Url)
        
        # Get only <code r> .... </code> chunks from this page
        Codestart <- grep("^\\s*<code", Raw)
        Codeend <- grep("^\\s*</code>", Raw)
        # A little bit of checking first
        if (length(Codestart) != length(Codeend) || any(Codeend <= Codestart))
                stop("Malformed wiki page (wrong <code>... </code> sections)")  
        # Get only r code sections (those starting with <code r> from the list
        Rstart <- grep("^\\s*<code r>", Raw)
        if (length(Rstart) == 0) return(character(0))   # no R code in this page
        isRsection <- Codestart %in% Rstart
        Rend <- Codeend[isRsection]
        
        # Construct the list of text lines related to r code
        R <- data.frame(Start = Rstart, End = Rend)
        Seq <- function(x) seq(from = x[1], to = x[2])
        Rrows <- c(apply(R, 1, Seq), recursive = TRUE)
        Rcode <- Raw[Rrows]
        
        # Eliminate <code r> and </code> tags
        Rcode <- gsub("^\\s*</?code( r)?>.*$", "", Rcode)
        
        # Eliminate prompt from R code '> ', or '+ ' at the begining of a line
        Rcode <- sub("^[>+] ", "", Rcode)
        
        # Possibly eliminate empty lines
        if (strip.empty.lines) Rcode <- Rcode[Rcode != ""]
        
        # Possibly eliminate output (lines starting with '#!')
        if (strip.output) {
                Routput <- grep("^\\#\\!", Rcode)
                if (length(Routput) > 0) Rcode <- Rcode[-Routput]
        }
        
        # Return the R code
        return(Rcode)
}

rcode <- getWikiRcode("tips:data-frames:merge")
rcode

sourceWikiRcode <- function(wikipage, echo = TRUE, url = 
"http://wiki.r-project.org/rwiki";,
        strip.empty.lines = TRUE, strip.output = FALSE, ...) {
        # Call getWikiRcode() to extract r code from wiki pages
        Rcode <- getWikiRcode(wikipage = wikipage, url = url,
                strip.empty.lines = strip.empty.lines, strip.output = 
strip.output)
        if (length(Rcode) == 0) {
                warning("No r ocde in this page!")
        } else {
                Con <- textConnection(Rcode)
                source(Con, echo = echo, ...)
                close(Con)
        }
}

sourceWikiRcode("tips:data-frames:merge")
# Here, the last part of this page is not directly executable (data1 is not 
defined)
# but the rest is fine!
_______________________________________________
R-sig-wiki mailing list
[email protected]
https://stat.ethz.ch/mailman/listinfo/r-sig-wiki

Reply via email to