jupyter / help

:sparkles: Need some help or have some questions? Please visit our Discourse page.
https://discourse.jupyter.org
291 stars 97 forks source link

Jupyter cannot make geochart with googleVIS #51

Open stefangeorge opened 8 years ago

stefangeorge commented 8 years ago

Using this works in R console:

plot(G)

but when typed into a cell in jupyter I get:

starting httpd help server ... done and no graph.

So here is what I did. Into Anaconda 2.7.11, I installed R essentials

conda install -c r r-essentials

started up jupyter

notebook jupyter

installed needed reqs, XML and googleVIS, by typing this into a cell

options(repos=structure(c(CRAN="https://cloud.r-project.org/")))
install.packages('googleVis')
install.packages('XML')

typed this code into a cell

suppressPackageStartupMessages(library(googleVis))
library(googleVis)
library(XML)
url <- "http://en.wikipedia.org/wiki/List_of_countries_by_credit_rating"
x <- readHTMLTable(readLines(url), which=3)
levels(x$Rating) <- substring(levels(x$Rating), 4, 
                            nchar(levels(x$Rating)))
x$Ranking <- x$Rating
levels(x$Ranking) <- nlevels(x$Rating):1
x$Ranking <- as.character(x$Ranking)
x$Rating <- paste(x$Country, x$Rating, sep=": ")
G <- gvisGeoChart(x, "Country", "Ranking", hovervar="Rating",
                options=list(gvis.editor="S&P",
                             projection="kavrayskiy-vii",
                             colorAxis="{colors:['#91BFDB', '#FC8D59']}"))

then

plot(G)

This code works fine when typed directly into an R console and makes a nice map. But something is causing jupyter to choke on starting a server. I guess since jupyter itself is web page running in a server there is some sort of problem with a web page starting a server? GoogleVIS needs to start the http do server that I think is built into R. But I don't know how to test whether this functionality exists in jupyter or the R essentials that I installed into Python.

takluyver commented 8 years ago

Pinging @JanSchulz and @flying-sheep as this question relates to the R kernel.

jankatins commented 8 years ago

The problem is that we are not interactive:

In googleVis:::plot.gvis

[...]
        print(x, file = file)
        .url <- sprintf("http://127.0.0.1:%s/custom/googleVis/%s", 
            ifelse(R.version["svn rev"] < 67550 | getRversion() < 
                "3.2.0", get("httpdPort", envir = environment(startDynamicHelp)), 
                tools::startDynamicHelp(NA)), basename(file))
        if (interactive()) {
            browseURL(.url, ...)
        }
        else {
            browseURL(.url, browser = "false", ...) # does basically nothing due to the false :-(
        }
        invisible(file)
jankatins commented 8 years ago

This produces an iframe, but I can't get it to become visible:

repr_text.gvis <- function(obj, ...) 'gvis cannot be represented in plain text (need html)'

repr_html.gvis <- function(obj, ...){
    htmlfile <- googleVis:::plot.gvis(obj)
    readChar(htmlfile, file.info(htmlfile)$size)
}

# no plot, just let it display...
G
jankatins commented 8 years ago

This works...:

repr_text.gvis <- function(obj, ...) 'gvis cannot be represented in plain text (need html)'

repr_html.gvis <- function(obj, ...){
    if (!requireNamespace('googleVis', quietly = TRUE))
        stop('repr_html.gvis called without loadable googleVis')

    htmlfile <- tempfile(fileext = '.html')
    on.exit(unlink(htmlfile))

    print(obj, tag='chart', file=htmlfile)
    readChar(htmlfile, file.info(htmlfile)$size)
}
repr_latex.gvis <- function(obj, ...) NULL
repr_markdown.gvis <- function(obj, ...) NULL
flying-sheep commented 8 years ago

why not simply this?

repr_html.gvis <- function(obj, ...) {
    if (!requireNamespace('googleVis', quietly = TRUE))
        stop('repr_html.gvis called without loadable googleVis')

    capture.output(print(obj, tag = 'chart'))
}
jankatins commented 8 years ago

I added a PR to googleVIS to add these functions: https://github.com/mages/googleVis/pull/39

jankatins commented 8 years ago

The interactive stuff is https://github.com/IRkernel/IRkernel/issues/236 :-(

jankatins commented 8 years ago

why not simply this?

random choice (and having the htmlwidget code available): I don't like neither using a tempfile nor capture.output :-) The real solution in this case would be to extract the string generation from print.gvis and use that directly instead of having print first generate the string and then output it via cat and we then capture.output / read it in from the file.