rplotr-markdown

R lordif Rmarkdown


I tried to add a plot in a rmarkdown file from the plot-method in lordif.
Here is the simple example :

```{r}

library(lordif) 
data(Anxiety)

age.dif <- lordif(Anxiety[paste("R",1:29,sep="")],Anxiety$age)
plot.lordif(age.dif,labels=c("Younger","Older"),width=8,height=7,cex=0.8,lwd=1)

```

If you run the code on the console it produces multiple plots, however not in rmarkdown.


Solution

  • The problem is that there exists dev.new() in the function plot.lordif() ! You can check it up with :

    > as.list(body(plot.lordif))
    

    and discover dev.new() right in the ninth part :

    > body(plot.lordif)[[9]]
    
    # if (sysname == "Windows") {
    #     dev.new(width = width, height = height, record = TRUE)
    # } else if (sysname == "Linux") {
    #     dev.new(width = width, height = height)
    #     par(ask = TRUE)
    # } else {
    #     dev.new(width = width, height = height)
    # }
    

    dev.new() prevents markdown from displaying your plots. So just get rid of this part and override the function.

    You can try to revise the function by adjusting the function body :

    > body(plot.lordif)[[9]] <- NULL
    

    To fulfill this in markdown, you can add a additional chunk and not show it in your document with echo = FALSE.

    ```{r echo = FALSE}
    library(lordif)
    body(plot.lordif)[[9]] <- NULL
    ```
    
    ```{r}
    data(Anxiety)
    age.dif <- lordif(Anxiety[paste("R", 1:29, sep = "")], Anxiety$age)
    plot.lordif(age.dif, labels = c("Younger", "Older"), width = 8, height = 7, cex = 0.8, lwd = 1)
    ```