2

I'm having trouble getting plots to display in R Markdown.
Running the below code in an R script works fine and produces charts. Running the same code in an R Markdown document does not.

Thanks to @aosmith over in this post for the grouping of charts.

---
title: "TestingIssue"
author: "John"
date: "August 18, 2015"
output: html_document
---

```{r, results='asis'}
# Load visualization & manipulation libraries
library(ggvis)
library(dplyr)
library(knitr) # added this per aosmith suggestion

# Build DF
dailyCaloriesDF <- structure(list(Name = c("Amy", "Amy", "Amy", "Amy", "Amy", "Amy", 
                        "Amy", "Amy", "Amy", "Sue", "Sue", "Sue", "Sue", "Sue", "Sue", 
                        "Sue", "Sue", "Sue", "Jim", "Jim", "Jim", "Jim", "Jim", "Jim", 
                        "Jim", "Jim", "Jim"), 
               Date = c("1/1/2015", "1/2/2015", "1/3/2015", 
                        "1/4/2015", "1/5/2015", "1/6/2015", "1/7/2015", "1/8/2015", "1/9/2015", 
                        "1/1/2015", "1/2/2015", "1/3/2015", "1/4/2015", "1/5/2015", "1/6/2015", 
                        "1/7/2015", "1/8/2015", "1/9/2015", "1/1/2015", "1/2/2015", "1/3/2015", 
                        "1/4/2015", "1/5/2015", "1/6/2015", "1/7/2015", "1/8/2015", "1/9/2015"), 
               Calories = c(1700L, 1650L, 1600L, 1550L, 1500L, 1450L, 1400L, 
                            1350L, 1300L, 2000L, 1950L, 1900L, 1850L, 1800L, 1750L, 1700L, 
                            1650L, 1600L, 1800L, 1750L, 1700L, 1650L, 1600L, 1550L, 1500L, 
                            1450L, 1400L)), 
          .Names = c("Name", "Date", "Calories"), 
          class = c("tbl_df", "tbl", "data.frame"), 
          row.names = c(NA, -27L))

# Build function
makeCharts = function(dat) {
  dat %>% ggvis(~Date, ~Calories)
}

# Execute for each group by name
allplots2 = dailyCaloriesDF %>%
  group_by(Name) %>%
  do(plots = makeCharts(.))

# Print plots - This won't work in markdown, but will work in script
allplots2$plots

# New code, per aosmith suggestion
data(mtcars)
plots <- lapply(1:5, function(x){
  ggvis(mtcars, ~cyl, ~mpg) %>% layer_points() %>% knit_print
})
html <- paste0(lapply(plots, unclass), collapse = "\n")
asis_output(html, ggvis_dependencies(absolute = TRUE, in_shiny = FALSE))

```

When I run that code in a markdown document, I just get the console output in the generated document.

[[1]]

[[2]]

[[3]]

...

The new code gives this error message:

Quitting from lines 9-53 (testOutput.Rmd) 
Error in structure(x, class = "knit_asis", knit_meta = meta, knit_cacheable = cacheable) : 
  could not find function "ggvis_dependencies"
Calls: <Anonymous> ... withVisible -> eval -> eval -> asis_output -> structure
Community
  • 1
  • 1
John Tarr
  • 717
  • 1
  • 9
  • 21
  • As always, it's great to have a reproducible example, including a sample dataset (use e.g. `dput()`). – maj Aug 18 '15 at 16:56
  • You could try something like `lapply(llplots2, plot)`. I noticed that your function `makeCharts2` is defined with only 1 parameter but you pass 2 to it. – talat Aug 18 '15 at 16:59
  • @docendodiscimus `makeCharts2` from the answer to the linked question has a second ("group") parameter. – maj Aug 18 '15 at 17:05
  • @maj, ah right - overlooked that. – talat Aug 18 '15 at 17:09
  • Sorry, fixed the 2nd parameter. Was going with the first version of the function to keep it simpler. – John Tarr Aug 18 '15 at 17:37
  • 2
    Looks like there might be [an issue using ggvis in a list with knitr](https://groups.google.com/forum/#!topic/ggvis/HPhAaSttQzU). The link gives a hacky work around you could try. – aosmith Aug 18 '15 at 18:42
  • @aosmith - I just tried the code in the link and edited my original post accordingly. I'm getting an error on that code. – John Tarr Aug 18 '15 at 19:11
  • I had the same problem with the linked code and I don't now how to fix it. It still seems like knitr/ggvis interaction problem, as you can do the same process using ggplot2 to plot and plots from the list show up fine in a markdown document. – aosmith Aug 19 '15 at 13:35
  • @aosmith Thanks for trying the code out. I would like to avoid learning ggplot2, since ggvis is supposed to be the next gen of ggplot2, but the facets and knitr friendliness would help. I'm concerned, because I've sold my boss on R, but I'm finding it doesn't like to do things that Excel can, such as dual axis charts. I'm going to see if I can find a place to submit a bug report on ggvis, since it works fine in ggplot2, I'm guessing it isn't knitr's problem. Since you did all that work, if you want to submit an answer that it's a bug, I'll accept it. Thanks again! – John Tarr Aug 19 '15 at 14:38
  • 1
    I don't know if you've seen [this](http://stats.stackexchange.com/questions/117078/for-plotting-with-r-should-i-learn-ggplot2-or-ggvis), but I thought it was a nice overview of ggvis vs ggplot2 and why in the end you might learn both. And if you are in one of those situations where a dual axis plot is reasonable, there are pretty detailed examples of how to do this in ggplot2 on SO. – aosmith Aug 21 '15 at 17:15
  • 1
    @aosmith I have, and thanks for the note. As an update, I've been playing with ggplot2 and will be switching over to use that. At this point, I really don't need the interactivity and the facets and export to word/PDF are making it worthwhile. Thanks for all the help you've given me as I'm starting out with R. – John Tarr Aug 21 '15 at 18:54

0 Answers0