-1

I have a very difficult time to get my R markdown document compiled and execute. I have one-way, two-way ANOVA and ANCOVA analyses in the analysis along with many plots.

Here is the first R code chunk yet to be successfully executed.

{r, echo=FALSE}

install.packages("rmarkdown")
library(rmarkdown)

install.packages("knitr")
library(knitr)

install.packages("ggplot2")

install.packages("car")
library(car)

install.packages("pastecs")
library(pastecs)

install.packages("compute.es")
library(compute.es)

install.packes("multcomp")
library(multcopm)

install.packes("WRS2")
library(WRS2)

install.packages("gmodels")
library(gmodels)

install.packes("MASS")
library(MASS)

Quitting from lines 30-60 (PPC.Rmd) Error in contrib.url(repos, "source") : trying to use CRAN without setting a mirror Calls: ... withVisible -> eval -> eval -> install.packages -> contrib.url

Execution halted

Community
  • 1
  • 1
Albert L
  • 57
  • 5
  • 1
    Installing a package is going to some server (a CRAN mirror), downloading the package, and putting the files on your computer. It shouldn't be necessary to re-download (that is, re-install) any package every time you compile your document. So, **don't use install.packages()` inside your document**, just load the packages with `library()`. Installing the proper packages should be a precursor step to knitting the document, just like installing R and knitr. – Gregor Thomas Feb 03 '16 at 20:36
  • 3
    Also, check for syntax errors. If code would throw an error in a script or in the console, it will also throw and error in a knitr document. For example, you have several `install.packes()`, which is not a valid command. – Gregor Thomas Feb 03 '16 at 20:38
  • I caught the error in syntax. And thanks this is very helpful. – Albert L Feb 03 '16 at 20:47
  • I have successfully executed my R markdown document and enjoying seeing all of it. THANKS very much. R is so powerful, yet frustrating at times, such as a simple trick/ (not to use install.packages() in the R code chunk) save my day. I also have an inline mathematical expression. Do you know how to get it print/render the right way? it is like this $$ r_{contrast} = \sqrt{t^2^ \over t^2^+7441} $$ – Albert L Feb 03 '16 at 21:17
  • 1
    `fortunes::fortune(168)` R is powerful, but you do need to work to understand what you're doing. A `knitr` document runs the code in it every time you compile. `install.packages()` downloads a version of a package and puts it on your computer - it should be run once to get the package and then you don't need to run it again (though you should, occasionally, `update.packages()` to get new versions. Knowing this, it's fairly clear that you shouldn't be installing packages in your knitr document; it doesn't seem like a "trick" to me.... – Gregor Thomas Feb 03 '16 at 21:24
  • The problem comes from running a lot of code that you don't really understand :) But as you climb the learning curve, things will make more and more sense, and you'll be able to do more powerful things. – Gregor Thomas Feb 03 '16 at 21:25
  • `$$` is for "display equations", use a single `$` for in-line equations. If you still have trouble, ask a new question. You might also need to delete the spaces after the first `$` and before the last `$`. – Gregor Thomas Feb 03 '16 at 21:26
  • Please never do this. Adding an `install.packages` is just plain rude to the CRAN mirrors. At the very least change the example code to what Severin showed. – hrbrmstr Feb 04 '16 at 02:18
  • Possible duplicate of [install.packages fails in knitr document: "trying to use CRAN without setting a mirror"](http://stackoverflow.com/questions/33969024/install-packages-fails-in-knitr-document-trying-to-use-cran-without-setting-a) – CL. Feb 04 '16 at 07:43

1 Answers1

3

I usually carry small function

check_and_install <- function( packname ) { # given package name, check installation, install if not found
    if ( packname %in% rownames(installed.packages()) == FALSE ) {
        install.packages( packname )
    }
}

so at the beginning of the Rmd i do

check_and_install("ggplot2")
library(ggplot2)

....
Severin Pappadeux
  • 18,636
  • 3
  • 38
  • 64
  • From `?installed.packages`: "do not use this to find out if a named package is installed [...] nor to find out if a package is usable (call `require` and check the return value)". But anyways, I don't think that this answers the question - see the suggested duplicate above. – CL. Feb 04 '16 at 08:37
  • @CL. that is a bit of over-dated advise. It used to be slow operation on old computers. Nowadays, it is pretty much in the noise to check if package is installed or not. Just measure it... – Severin Pappadeux Feb 04 '16 at 13:49