2 Knitr/R Markdown/Rstudio Issues: Highcharts and Morris.Js

2 Knitr/R Markdown/Rstudio issues: Highcharts and Morris.js

NOTE: This is the same solution I posted in the knitr google group.

To get rCharts to work with knit2html, you will need to use the print method with the argument include_assets = TRUE. This is because knitr will not add the js and css assets required by an rCharts plot automatically. Here is a minimal working example.

## MorrisJS with Knit2HTML

```{r results = 'asis', comment = NA}
require(rCharts)
data(economics, package = 'ggplot2')
econ <- transform(economics, date = as.character(date))
m1 <- mPlot(x = 'date', y = c('psavert', 'uempmed'), type = 'Line',
data = econ)
m1$set(pointSize = 0, lineWidth = 1)
m1$print('chart2', include_assets = TRUE)
```

Note that you need to use m1$print('chart2', include_assets = TRUE, cdn = TRUE) if you intend to publish your chart on RPubs, for otherwise the JS and CSS assets will be served from your local library.

Hope this helps.

rChart in R Markdown doesn't render

GOT IT.

see this answer: Ramnath layin' it down

(chest-swelling feeling of satisfaction quickly deflated upon realization we were just looking at outdated tutorials / walkthroughs...)

last line should be

n1$print('iframesrc', cdn =TRUE, include_assets=TRUE)

I think most of the tutorials out there are using an old version or something. But the above works for me, so give it a shot.

then knit, then you're good to go. Also make sure you're rCharts library is up to date

install_github("ramnathv/rCharts")

In RStudio, where is knitr markdown output located?

Under certain conditions, RStudio moves the knitted (HTML or PDF) file to a temporary directory (located under tempdir()). These conditions are:

  • You are in a RStudio Project.
  • The "Project build tools" are set to "Package".
  • The RMD file is located in a subdirectory vignettes.
  • Knitting is initiated with the "Knit" button in RStudio.

Typically, this happens when knitting a package vignette and has the purpose of "keep[ing] your vignettes folder from becoming cluttered with HTML files".

Changing the Project build tools, changing the folder name or closing the project prevents this behavior. Another way to keep the generated (HTML or PDF) file is to call rmarkdown::render interactively (although this is not exactly equivalent to the "Knit" button).

Note that this only affects where the generated file will be saved. The Working Directory while knitting is unaffected.

knitr rchart datatable dtable does not to render

Remove cdn from the print function to use local JS and CSS.

tab2$print('chart2', include_assets= TRUE)

Is there a way to knitr markdown straight out of your workspace using RStudio?

RStudio opens a new R session to knit() your R Markdown file, so the objects in your current working space will not be available to that session (they are two separate sessions). Two solutions:

  1. file a feature request to RStudio, asking them to support knitting in the current R session instead of forcibly starting a new session;
  2. knit manually by yourself: library(knitr); knit('your_file.Rmd') (or knit2html() if you want HTML output in one step, or rmarkdown::render() if you are using R Markdown v2)

Line breaks in directory using knitr/R Markdown

You can use file.path to construct paths from multiple subpaths, for example:

setwd(file.path(
"C:/Users/Testuser/Documents",
"Testfolder/Testsubfolder/Testsubfolder2",
"Testsubfolder3/Testsubfolder4/Testsubfolder5"
))

Alignment of images in tables with markdown, rstudio and knitr

Or you make use of \raisebox. It puts the content inside of a new box and with its parameters you can modify the offset of the box (play around with the parameter currently set to -0.4):

---
title: "Untitled"
output: pdf_document
---

This example highlights the issue I am having with formatting a nice table with the graphics and the vertical alignment of text.

```{r echo=FALSE, results='hide', warning=FALSE, message=FALSE}
## Load modules
library(dplyr)
library(tidyr)
library(ggplot2)

## Create a local function to plot the z score
varianceChart <- function(df, personNumber) {
plot <- df %>%
filter(n == personNumber) %>%
ggplot() +
aes(x=zscore, y=0) +
geom_rect(aes(xmin=-3.32, xmax=-1.96, ymin=-1, ymax=1), fill="orange2", alpha=0.8) +
geom_rect(aes(xmin=1.96, xmax=3.32, ymin=-1, ymax=1), fill="olivedrab3", alpha=0.8) +
geom_rect(aes(xmin=min(-4, zscore), xmax=-3.32, ymin=-1, ymax=1), fill="orangered3") +
geom_rect(aes(xmin=3.32, xmax=max(4, zscore), ymin=-1, ymax=1), fill="chartreuse4") +
theme(axis.title = element_blank(),
axis.ticks = element_blank(),
axis.text = element_blank(),
panel.grid.minor = element_blank(),
panel.grid.major = element_blank()) +
geom_vline(xintercept=0, colour="black", alpha=0.3) +
geom_point(size=15, shape=4, fill="lightblue") ##Cross looks better than diamond
return(plot)
}

## Create dummy data
Person1 <- rnorm(1, mean=10, sd=2)
Person2 <- rnorm(1, mean=10, sd=2)
Person3 <- rnorm(1, mean=10, sd=2)
Person4 <- rnorm(1, mean=10, sd=2)
Person5 <- rnorm(1, mean=10, sd=2)
Person6 <- rnorm(1, mean=6, sd=1)

## Add to data frame
df <- data.frame(Person1, Person2, Person3, Person4, Person5, Person6)

## Bring all samples into one column and then calculate stats
df2 <- df %>% gather(key=Person, value=time)
mean <- mean(df2$time)
sd <- sqrt(var(df2$time))

stats <- df2 %>%
mutate(n = row_number()) %>%
group_by(Person) %>%
mutate(zscore = (time - mean) / sd)

graph_directory <- getwd() #'./Graphs'

## Now to cycle through each Person and create a graph
for(i in seq(1, nrow(stats))) {
print(i)
varianceChart(stats, i)

ggsave(sprintf("%s/%s.png", graph_directory, i), plot=last_plot(), units="mm", width=100, height=20, dpi=1200)
}

## add a markup reference to this dataframe
stats$varianceChart <- sprintf('\\raisebox{-.4\\totalheight}{\\includegraphics[width=0.2\\textwidth, height=20mm]{%s/%s.png}}', graph_directory, stats$n)

df.table <- stats[, c(1,2,5)]
colnames(df.table) <- c("Person Name", "Time taken", "Variance Chart")
```

```{r}
library(knitr)
kable(df.table[, c(1,2)], caption="Rows look neat and a sensible distance apart")
kable(df.table, caption="Rows are separated a long way apart and images and text are misaligned")
```

Sample Image

Control digit printing for inline knitr

The reason why print() does not work has been explained in #501. Besides, please note that print(x) often returns invisible(x), so the actual value you passed to knitr is still x, and digits = 2 is meaningless. print() is typically used only for its side-effects (printing in the console/terminal), and it does not modify the value passed to it. To sum it up:

  1. print(x) returned invisible(x), so knitr ignored it;
  2. print(x, digits = 2) does not round x as its returned value, so you cannot get the desired number of digits via print().

round(), formatC(), and sprintf() are all correct ways of formatting numbers.



Related Topics



Leave a reply



Submit