R markdown, hiding the library output - r

I am working on a rmarkdown presentation. I am trying to show the usage of cast function. However, since the reshape package is necessary, to run the cast function, I need to load reshape library as below.
{r package_options, echo=TRUE}
library(reshape)
cast(datam, isim~Ay, value="Sirano")
However, after knitting the codes, I face with the output ;
I just need to see the name of the library on the screen which is library(reshape) , and also I want it to be used to run cast function, but I dont want to see the package outputs as shown in the picture.
Would someone help about that?

If you want to hide all these messages you have to put :
```{r,warning=FALSE,message=FALSE}
library(reshape)
```

Related

Printing an R code and output using knitr

I'm trying to print an R-code and its output. In the past I remember using a function called knit for this. Looking online it seems it may be part of the package knitr (although I seem to remember there being a button in the file dropdown which did the same).
However I can't work out a simple way to use this, and what functions produce a MS Word output.
Just wondering what the simplest way is to print the r-code and output?

RMarkdown error with melt() function

I'm trying to create a file word starting from a program that on console perfectly works.
The problem is that when it reaches a function that uses melt() it generates an error
cannot find melt function
and stops compiling the code.
The package reshape is installed, and I tried to rerun the code without using markdown and it doesn't generate any error.
Is possible that RMarkdown doesn't support melt() function?
Installing a package is not enough — you need to load it. It’s possible that it works in your console because it was still loaded from before, or because you installed it in the current session.
To use the package, you need to add the following into your (R Markdown) code, before using any of its functions:
library(reshape)
Alternatively, you can just prefix all its functions with reshape::. So, instead of calling melt, you’d call reshape::melt. That’s less commonly done but there’s nothing wrong with it.
Load the package inside R markdown. Loading the package outside R markdown is not enough. The loading of package should be inside R chunk in R markdown.
```{r}
library(reshape)
melt(iris)
```
I had the exact same problem and finally solved it. It was caused by setting eval=FALSE inside the code chunk that loaded the libraries in my Markdown document, including reshape2. When I removed that option, it finally worked:
{r setoptions, ***eval=FALSE***}
library(ggplot2)
library(plyr)
library(dplyr)
library(reshape2)
library(knitr)

Save a package specific plot to pdf R

I use the package esd (see reproductible code below) which is climatology package.
The package has several custom map() and plot() functions.
I save my plots to Pdf's. map() function work well doing:
x<-my.field.object
CairoPDF(file="/path/to/file.pdf")
map.field(x)
dev.off()
However plot doesn't work. Doing the same code as above, it will actually plot (with x11()) in Rstudio and nothing will be output to the pdf file.
I tried looking inside the function but can't see anything that make it behave like that.
Any idea?
P.S: for those who would like to try, the package has some field data in it, for example
Reproducible code:
#devtools::install_github("metno/esd")
library("Cairo")
x<-t2m.NCEP(lon=c(-40,40),lat=c(30,70))
CairoPDF(file="/path/to/file.pdf")
plot.field(x)
dev.off()
EDIT: I'm aware of the dev.print solution. It works, but the problem is that i actually need to put several plot.field into one PDF file.

issues of wrapping up a set of workable R commands into a function

I generate a dendrogam using a collection of r commands. It worked just fine and saved the generated dendromgram into a PDF file. To improve efficiency, I wrapped these commands as a function, which does not change anything. However, the pdf is just a blank file without any graphical content. Please let me know what’s wrong with my function defintion. Thanks.
myplot<-function(inputcsv, outputfile){
library(ggdendro)
library(ggplot2)
x<-read.csv(inputcsv,header=TRUE)
d<-as.dist(x,diag=FALSE,upper=FALSE)
hc<-hclust(d,"ave")
dhc<-as.dendrogram(hc)
ddata<-dendro_data(dhc,type="rectangle")
ddata$labels$text <- gsub("\\."," ",ddata$labels$text)
ggplot(segment(ddata))+geom_segment(aes(x=x0,y=y0,xend=x1,yend=y1))
pdf(outputfile, width=30,height=35)
last_plot()
dev.off()
}
R FAQ
Wrap your ggplot call in a print() function.
ggplot and friends return an object, and the plotting only happens when the object is printed. When you do this on the command line the printing happens automatically. When you stick it in a script or function you have to do it yourself.
The debate on whether this is a good idea or a dumb thing that just generates questions like this continues...

sweave and ggplot2: no pdfs generated at all

I am trying create a sweave report that contains some graphics done with ggplot2. Though I am looking for some environment for the long run – I just use a simple .Rnw file here that only contains the code and the plot
\documentclass[a4paper]{article}
\SweaveOpts{echo=FALSE}
\usepackage{a4wide}
\begin{document}
\begin{figure}[htbp]
\begin{center}
<<>>=
library(ggplot2)
x=rnorm(100)
qplot(x)
#
\caption{My Graph}
\end{center}
\end{figure}
\end{document}
Unfortunately the graph is not created, I only get a corrupted .pdf and .eps file. Though I get a nice .tex file that appears to work except for the graphics.
I use the following basic code to create it:
Sweave("myfile.Rnw")
I just found some older post on the web that were discussing problems with transparency and sweave / ggplot2 but nothing that could have helped. I also tried the relaxed package, which did not help either. Btw, is there any news on decumar package?
qplot() produces objects, not a graphic output. It might seem like it does when you run it, but that's because without assignment, R is automatically printing the output of qplot(). To integrate it into Sweave, either wrap print() around qplot(), or assign the output of qplot() to something, then wrap that in print().
...
<<fig = T, echo = F>>=
library(ggplot2)
x=rnorm(100)
p <- qplot(x)
print(p)
#
...
That should work. I use ggplot2 graphics in my sweave docs all the time.
You have to wrap it around print() to make it work in sweave.
Actually, while both previous answers are correct, your problem is something else.
You need to ensure that the entire code block is at the left of the page (apart from iundentation in functions). Again, I have no idea why but this causes problems for Sweave.
After ensuring that all code (and header/footer for code chunk) were at the left of the page (and adding a print statement) then your example works for me.
Incidentally, i learned today that you can create an environment around your code in sweave documents (which i wasn't aware of, and will save me much time). Good old stackoverflow, teaching you something new even when you answer a question!
Hope this helps.

Resources