4

I want to make a report with the graphics I obtain with R. Here I show you both images produced with site_rose(site_ref) and site_time_series(site_ref) I designed. enter image description here

enter image description here

But after running sweave('Profile.Rnw') and getting Profile.tex I obtain Profile-002.pdf and Profile-003.pdf that these last two documents have 0KB. And so I get a .pdf report without any figure at all. Here I show you the code:

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[spanish]{babel}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}

\title{Anàlisi in situ emplaçament}
\author{Jonel Palou Visa}

\begin{document}
\maketitle
\begin{center}
<<fig=TRUE,echo=TRUE>>=
 site_ref <- site_time_series(site_ref,peaks=T,stationary=T)
@
\end{center}

\begin{center}
<<fig=TRUE,echo=TRUE>>=
site_rose(site_ref)
@

\end{center}

\end{document}

I would like to know if there is a problem with the new device I call in order to plot the figures or what is my real problem. The code for both functions is too large to show here, there isn't any extrange in these functions because to get the figure I only call plot(...)

BenMorel
  • 34,448
  • 50
  • 182
  • 322
JPV
  • 1,079
  • 1
  • 18
  • 44
  • 3
    Nit-pick: it's wise to use `TRUE` instead of `T`. `TRUE` is an R constant which represents the Boolean value you're aiming for. `T` can be defined to mean anything you want. For instance, `T <- FALSE` is a valid command and would really confuse your code. – Jeff Allen Apr 18 '12 at 16:29

2 Answers2

11

Without the code of the two functions you created it remains unclear what is going wrong. However, if you use either lattice or ggplot2 not calling print on the object that that came out of ggplot2 or lattice. On the command line these kinds of plots work because print is then called implicitely. So:

print(site_rose(site_ref))

Should produce the correct result. Alternatively, you could start using knitr instead of Sweave. knitr does not have this problem. In general I would recommend using knitr instead of Sweave.

Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149
  • @Paul Hiemstra: Thank you very much for the answer. Coincidentally, I was just having problems with compiling a PDF in RStudio, and the fact that I didn't include `print(p)` where `p` was my plot produced by the function `ggplot` was the culprit. – Jubbles Apr 18 '12 at 17:14
  • @Jonel_R, where you using ggplot or lattice? And did printing the object solve the problem? – Paul Hiemstra Apr 18 '12 at 17:48
  • @Jubbles, you could also use knitr, which does not have this problem with printing. In many other respects, including syntax, is Sweave, but then better. – Paul Hiemstra Apr 18 '12 at 17:49
  • I am not using ggplot neither lattice. The comands that are implemented in site_time_series and site_rose are simply : plot.new() and plot(x.....) – JPV Apr 19 '12 at 08:23
2

Is that the whole Sweave file? You won't be able to re-use variables that are defined in your current environment variable when creating a Sweave document. The whole point of Sweave is to reproduce some analysis from end-to-end, so that wouldn't be a good approach to take anyways.

I'd recommend including whatever analysis is necessary to generate the site_ref variable, among others in this Sweave document.

If you insist on taking a shortcut, you could save your current R environment and load it in as the first command in the Sweave document to provide access to those variables.

Use Stangle to extract the R code from your Sweave document, then create a new R session and try running that code. You'll probably get errors about undefined variables, or messages about functions not being defined. You should be able to run the R code contained in your Sweave document in an empty environment and have it work successfully. Then you're ready to render it as a PDF.

Jeff Allen
  • 17,277
  • 8
  • 49
  • 70
  • 1
    Sorry I didn't undersatnd you very much. What exactly do I have to do with my site_ref variable? – JPV Apr 18 '12 at 16:38
  • In your first code chunk, you use the variable `site_ref`. You never defined `site_ref` in this script. So it may be that you're trying to plot a variable which doesn't exist (which is just going to give you an empty plot, or an error). It would be best to include ALL of your code in your Sweave document, including whatever is necessary to create such variables (and custom functions). Sweave scripts should be completely self-contained and not require pre-defined variables or pre-loaded libraries. – Jeff Allen Apr 18 '12 at 16:44
  • NO this is a aprt of the whole code. In the first lines I already delcare site_ref. It is not the problem of the variable. But I still wonders why whith kintr works and with sweave doesn't. – JPV Apr 19 '12 at 08:17
  • You also reference `site_ref` in your declaration of `site_ref`: `site_ref <- site_time_series(**site_ref**,peaks=T,stationary=T)`. I haven't had the chance to play around with KnitR yet, so I'm not sure about the differences there. – Jeff Allen Apr 19 '12 at 13:03