I am using R notebook. This is my chunk:
```{r}
test = matrix(rnorm(200), 20, 10)
pheatmap::pheatmap(test)
```
I guess it's due to the way pheatmap generates the plot, but it actually generates a blank plot first. Thus, this is the output I see:
How do I get rid of that first image? I see it in the RStudio output (screenshot above) and in the .nb.html file. If I knit to HTML, the blank plot is not there.
I tried different fig.keep options. They work when I knit to HTML, but they don't seem to have an effect in the .nb.html file. How can I get rid of it?
Update: This issue was fixed in pheatmap. It may still be applicable to other scenarios.
This is weird. Try this:
```{r}
library(pheatmap)
p <- pheatmap(test, silent = TRUE)
plot(p$gtable)
```
It produces exactly what you describe. Now, split it into two chunks.
```{r}
library(pheatmap)
p <- pheatmap(test, silent = TRUE)
```
```{r}
plot(p$gtable)
```
It works! I have no idea why, though.
Related
I am using R Markdown to generate practice problems for a statistics class, and I like to include some randomness so that there can be multiple versions of the same problem. The students are just starting to use R themselves and I would like to be able to show the correct values in the answer R code. Basically I want to achieve something like this...
Question:
```{r, include=FALSE}
conf <- sample(c(0.9,0.95,0.99), 1)
```
What is the `r conf * 100`% confidence interval for the slope coefficient of your regression "reg"?
Answer:
To get the correct answer run the following code
```{r}
confint(reg, level = magic_function(conf))
````
where the magic_function is some function that will make the code block in the generated document look something like this...
confint(reg, level = 0.95)
Thanks to user2554330 for putting me on the right path. The following gets me the desired result, although there may be a better way to do this
Answer:
To get the correct answer run the following code
```{r, include=FALSE}
code <- c("```{r}", knit::knit_expand(text = "confint(reg_result, level = {{conf}})", conf = conf), "```")
```
`r paste(knitr::knit(text = code), collapse = '\n')`
UPDATE
It seems that if you want to have more than one such code chunk, you will need to use knit_child instead of knit, like so
Answer:
To get the correct answer run the following code
```{r, include=FALSE}
code <- c("```{r}", knit::knit_expand(text = "confint(reg_result, level = {{conf}})", conf = conf), "```")
```
`r paste(knitr::knit_child(text = code), collapse = '\n')`
Is it possible to place a table generated with the xtable (or alternatively the pander) package and a generated plot side-by-side in R markdown knitting to pdf while the rest of the document is not in columns? The following simple example hopefully illustrates the idea:
\begin{multicols}{2}
```{r}
plot(cars)
```
```{r, results='asis'}
library('xtable')
print(xtable(head(cars,5)), type = "latex")
```
\end{multicols}
However, this does not produce the plot. I know that solutions exist using knitr (e.g. here) and for R markdown knitting to HTML (e.g. here) but I don't get them to work for R markdown to pdf.
the gridExtra package works for this without having to go into LaTeX hell. Use the grid.arrange function for side by side charts and what-not.
Works on html and PDF outputs.
Thank you #nycrefugee this already opens some more opportunities. However, there seem to be some problems with xtable outputs. If I use the following code:
library('gridExtra')
library('grid')
library('xtable')
library('lattice')
p = xyplot(1~1)
t = textGrob( print(xtable(head(cars,5)),
type = "latex", label = "test")
)
grid.arrange(p, t, ncol=2)
it produces the following output when compiled to pdf, i.e. the table is shown above the two grobs:
PDF output
I'm working on my first R notebook which works pretty well, except for one issue.
I'd like to be the numbers that I output inline with
`r realbignumber`
to have commas as separator and max 2 decimal points: 123,456,789.12
In order to achieve this, I added a chunk at the beginning of my document, which contains...
```{r setup}
knitr::opts_chunk$set(echo = FALSE, warning=FALSE, cache = TRUE, message = FALSE)
knitr::opts_chunk$set(inline = function(x){if(!is.numeric(x)){x}else{prettyNum(round(x,1), big.mark = ",")}})
options(scipen=999)
```
The suppression of scientific numbers works like a charm, so the chunk is definitely executed. However, formatting of the inline output of numbers does not work.
Any ideas why that could be?
Do these kinds of settings generally not work with R notebooks?
Edit:
The solution suggested here also has no effect on the output format of numbers.
Here is an example illustrating two ways to print a large number in an R Markdown document. First, code to use the prettyNum() function in an inline R chunk.
Sample document where we test printing a large number. First set the number in an R chunk.
```{r initializeData}
theNum <- 1234567891011.03
options(scipen=999,digits=16)
```
The R code we'll use to format the number is: `prettyNum(theNum,width=23,big.mark=",")`.
Next, print the large number. `r prettyNum(theNum,width=23,big.mark=",")`.
The alternative of using chunk options works as follows.
Now, try an alternative using knitr chunks.
```{r prettyNumHook }
knitr::knit_hooks$set(inline = function(x) { if(!is.numeric(x)){ x }else{ prettyNum(x, big.mark=",",width=23) } })
```
Next, print the large number by simply referencing the number in an inline chunk as `theNum`: `r theNum`.
When both chunks of code are embedded in an Rmd file and knit, the output is as follows, showing that both techniques produce the same result.
regards,
Len
Hi I have the following markdown chunk:
```{r, echo=FALSE,warning=FALSE,message=FALSE,error=FALSE}
lapply(obj,function(x) plot(x,main="some plot") box() axis(1,at=seq(0,25,by=1))
```
The output is multiple plots. However I also get the console message in the pdf document underneath the plots.
<Plot 1> nice plot 1!
<Plot 2> nice plot 2!
-- nasty horrible console output
## [[1]]
01.2882829
## [[2]]
120.29393933
I have tried echo/warning/error/message = FALSE, but neither of these suppress the console output
please help!
try this:
{r, echo=FALSE,results='hide',fig.keep='all'}
lapply(obj,function(x) plot(x,main="some plot") box() axis(1,at=seq(0,25,by=1))
Wrapping any object in invisible will prevent automatically printing it.
You should be able to use
invisible(lapply(obj,function(x) plot(x,main="some plot")))
However the fact that echo=FALSE doesn't work suggests that there might be something else going on.
These are the options that worked for me:
echo=FALSE, message=FALSE, results='hide'
I was having this problem as well in my R notebook and echo=FALSE didn't do anything. However message=FALSE does.
```{r, message=FALSE}
Try this,
It will hide the errors, warning, code, and console output. It will show only the graphs.
{r, echo=FALSE,warning=FALSE,message=FALSE,error=FALSE, results='hide',fig.keep='all'}
lapply(obj,function(x) plot(x,main="some plot") box() axis(1,at=seq(0,25,by=1))
Later you can export it to HTML which will be neat and readable
Simply having ```{r, results = 'hide'} or ```{r, results = FALSE} for your chunk options suppresses R output but not warnings, messages or errors. No extra functions are needed.
More details can be found here.
https://yihui.org/knitr/options/#text-output
Hello everyone reading this.
I am trying out R with knitr and rCharts to generate Rmd -> md -> html files with flashy plots. At the moment I am trying out nPlot wrapper and I cannot get addControls feature working for some reason. Here is my simple example:
```{r testing, include=T, results='asis', comment=NA, message=F, echo=F}
cData <- read.csv("~/USCrime2012.csv")
crimebarPlots = nPlot(ViolentCrime ~ State, data = cData, type = "discreteBarChart")
crimebarPlots$xAxis(rotateLabels = -90)
#crimebarPlots$addControls("y", value = "ViolentCrime", values = names(cData[,-1]))
crimebarPlots$print("test", include_assets=T, cdn=T)
```
Everything works while the commented line is left out, but once I include it the plot stops working - all I get is an empty drop-down list button with no labels.
I adopted this example from here: http://bl.ocks.org/patilv/raw/7968210/ where it's presented to be working fine. I got the data from HERE
Before doing this I tried to run it with my own dataset with the same kind of result.
Any help or suggestions greatly appreciated.
-KK.