plot.MCA() not included in Sweave - r

I don't seem to be having a problem with any other method of including a plot via Sweave. However, plot.mca(), a method from the FactoMineR package seems to not have it's plot pulled through. It does create an Rplot.pdf file - but for whatever reason it's not renamed into "RnwFilename-00X.pdf" and not included in the resulting PDF when you compilePdf() it in RStudio.
Here's a trivial example, try it for yourself.
Note you may have to: install.packages("FactoMineR")
\documentclass[a4paper]{article}
% PREAMBLE
\begin{document}
\begin{center}
<<echo=false,fig=true>>=
library(FactoMineR)
x <- data.frame(
A=sample(letters[1:3],100,rep=T),
B=sample(letters[1:4],100,rep=T),
C=sample(letters[1:3],100,rep=T))
fit.mca <- MCA(x, graph=FALSE)
plot(fit.mca, invisible="ind")
#
\end{center}
\end{document}
Update - more details on the error message:
LaTeX errors:
!pdfTeX error: C:\Program Files (x86)\MiKTeX 2.9\miktex\bin\pdflatex.EXE (file
R:/.../RnwFilename-010.pdf): PDF inclusion: required page do
es not exist <0>

It works for me if I tell plot.MCA not to create a new device:
plot(fit.mca, invisible="ind",new.plot = FALSE)
Editorializing a bit, this seems like sub-optimal behavior for a plotting function, which most users (and other code, clearly) will expect to rely on R's default action to open a new device automatically. A plot function should only open a new device if the user has explicitly told it to (either by calling png, pdf etc or by actually setting new.plot = TRUE). Opinions may differ on this, though.

Related

RMarkdown: "Error: path for html_dependency not found:"

I am a Chemist who uses R and R Studio to analyze mass spectrometry data. I have automated scripts that process my data, because there are a lot of files. The scripts call rmarkdown using render() to import data, manipulate the data frame, save the processed data frame as a .csv and generate an html of plots. I have a problem rendering rmarkdown files since I recently updated to R v3.5.1 and R Studio v1.1.463. The error I receive is: Error: path for html_dependency not found: which I get if I use the knit button in R Studio, or if I use render(). The script inside the rmarkdown runs to completion, as the resulting objects are present in the environment window in R Studio, but the html file doesn’t render, and I get the error which stops the loop of render() I use to process all the files in a specific folder. This is a process I have done many times before updating.
I ran traceback() and got the following results:
8: stop("path for html_dependency not found: ", file, call. = FALSE)
7: FUN(X[[i]], ...)
6: lapply(dependencies, validate_html_dependency)
5: dependency_resolver(all_dependencies)
4: html_extras_for_document(knit_meta, runtime, dependency_resolver,
format_deps)
3: base(...)
2: output_format$pre_processor(yaml_front_matter, utf8_input, runtime,
knit_meta, files_dir, output_dir)
1: render("Processing and QA Template_INT_FINAL_MFAssignR.rmd",
output_file = paste0(substr(file_list[i], 1, nchar(file_list[i]) -
4), ".html"), output_dir = folder, params = list(data = file_list[i], path = folder))
I have uninstalled and reinstalled packages several times. I even removed my library folder entirely once, then reinstalled. My packages are up to date, but I had problems installing packages since the update, including rmarkdown. I had to use install.packages(“package”, type=”binary”) to install the dependencies for rmarkdown in order to get it to install. Normally I can just use the Install button in R Studio.
This is a work PC (Windows 10, 64 bit) I do not have administrator access to. I have to uninstall/install through IT at my university, which is a hassle, so I want to come to them with a plan. My package library defaults to a network drive that I can read/write to, and I have limited access to the hard disks; either way I can’t seem to change where packages are installed in R Studio. I don’t know if I can re-install an older version of R and R Studio or if it would help. A lot of the packages I use are developed for the current version of R and I was having some other issues, which was why I updated in the first place. The exact same scripts and data files run properly on another PC (my personal laptop, which is also up to date on R, R Studio and packages); the only changes I made are to the working directory so the data loads properly. I also had no trouble installing packages.
Here is an example of my code; since my scripts are very long, complicated and data specific, I have prepared a more simplified version of what they do as an example. I really don’t think there is anything wrong with the scripts themselves, as I mentioned before I have run them many times before the update. I suspect something is wrong with either the install of R, R studio or rmarkdown.
The main script, which calls render() from rmarkdown:
setwd("D:/Working Directory")
library("rmarkdown")
folder=paste0(getwd(),"/")
file_list=list.files(path=folder, pattern="*_MF.csv")
for (i in 1:length(file_list)){
render("Processing and QA Template.rmd",
output_file = paste0(substr(file_list[i],1, nchar(file_list[i])-4),".html"), output_dir = folder,
params=list(data=file_list[i], path=folder))
}
The rmarkdown, named “Processing and QA Template.rmd” which is in "D:/Working Directory":
---
title: "Example Processing and QA"
author: "Matt Brege "
date: "2018-12-12"
output: html_document
params:
data: x
path: x
---
```{r, echo=FALSE, message=FALSE}
library(dplyr)
library(ggplot2)
library(tidyr)
file_name <- substr(params$data, 1, nchar(params$data)-7)
folder <- params$path
input <- tbl_df(read.csv(paste0(file_name, "_MF.csv"), stringsAsFactors = FALSE))
```
```{r, echo=FALSE}
#...a series of long and complicated data manipulations later…
write.csv(input7, paste0("Output/", file_name, "_QAd.csv"), row.names=FALSE, na="")
```
```{r,r, echo=FALSE, warning=FALSE, message=FALSE}
#...plotting section…
# these are just examples
p1 <-ggplot(diamonds, aes(x=carat, y=price)) + geom_point()
print(p1)
p2<- ggplot(diamonds, aes(x=carat, y=price, color=clarity)) + geom_point()
print(p2)
p3<- ggplot(diamonds, aes(x=carat, y=price, color=cut)) + geom_point()
print(p3)
```
Finally, I have found the following similar posts about what appears to be the same issue, but they are old and from different versions of R, and it doesn’t seem like it was resolved in those instances anyway:
RMarkdown cannot knit: html_dependency not found
RNotebook cannot output due to html_dependency not found
R looks in the wrong place for html dependency
One suggestion from the last link was to clear the cache: “If you cached the chunk containing HTML widgets, you may need to invalidate the cache after you update R packages. – Yihui Xie Dec 6 '17 at 19:00” but I am not exactly sure what that means or how to do it. I generally run cat("\014") and rm(list=ls()) at the beginning of each script, but I don’t know if that’s what the suggestion means, and it has not helped.
I got it to work by:
Clear Knitr Cashe... (see the little arrow next to the Knit-icon in RStudio).
I also had to restart R (.rs.restartR() ).

Issue trying to Knit Rmd File to Word when RWeka is Used

I'm trying to Knit an Rmd file to Word, and keep encountering the following error message:
Error Message from RStudio
My line 28 is as follows:
NN <- make_Weka_filter("weka/filters/unsupervised/attribute/NumericToNominal")
I use NN later to apply the filter function to my testing and training data (I'm attempting a classification model for the Federalist papers)
trainfed <- NN(data=trainfed, control= Weka_control(R="1-3"), na.action = NULL)
testfed <- NN(data=testfed, control= Weka_control(R="1,3"), na.action = NULL)
When I run all of this either in blocks in my instance of RStudio, or just line by line - I have no issues. I can view the output in window, in line, and even in the viewer pane.
However, as soon as I start to try to Knit to Word I get the error mentioned above.
Is it possible to Knit to Word with RWeka package in use? I've seen many of these filed, but never really one with an answer to the error in finding the function on whatever instance Rmarkdown Knits from...
Can anybody advise? Thank you!

Externalise config file and functions in R markdown

I am having problems understanding the (practical) difference between the different ways to externalise code in R notebooks. Having referred to previous questions or to the documentation, it is still unclear the difference in sourcing external .R files or read_chunk() them. For practical purposes let us consider the below:
I want to load libraries with an external config.R file: the most intuitive way, according to me, seems to create config.R as
library(first_package)
library(second_package)
...
and, in the general R notebook (say, main.Rmd) call it like
```{r}
source('config.R')
```
```{r}
# use the libraries included above
```
However, this does not recognise the packages included, so it seems that sourcing an external config file is useless. Likewise using read_chunk() instead. Therefore the question is: How to include libraries at the top, so that they are recognised in the main markdown script?
Say I want to define global functions externally, and then include them in the main notebook: along the same lines as above one would include them in an external foo.R file and include them in the main one.
Again, it seems that read_chunk() does not do the job, whereas source('foo.R') does, in this case; the documentation states that the former "only evaluates code, but does not execute it": when is it ever the case that one wants to only evaluate the code but not execute it? Differently posed: why would one ever use read_chunk() rather than source, for practical purposes?
This does not recognise the packages included
In your example, first_package and second_package are both available in the working environment for the second code chunk.
Try putting library(nycflights13) in the R file and head(airlines) in the second chunk of the Rmd file. Calling knit("main.Rmd") would fail if the nycflights13 package wasn't successfully loaded with source.
read_chunk does in fact accomplish this (along with source) however they go about it differently. With source you will have the global functions available directly after the source (as you have found). With read_chunk however, as you pointed out since it only evaluates code, but does not execute it you need to explicitly execute the chunk and then the function will be available. (See my example with third_config_chunk below. Including the empty chunk of third_config_chunk in the report allows the global some_function to be called in subsequent chunks.)
Regarding "only evaluates code, but does not execute it", this is an entire property of R programming known as lazy evaluation. The idea being that you may want to create a number of functions or template code which is read into your R environment but is not executed on-the-spot, allowing you to modify the environment/parameters prior to evaluation. This also allows you to execute the same code chunks multiple times whereas source will only run once with what is already provided.
Consider an example where you have an external R script which contains a large amount of setup code that isn't needed in your report. It is possible to format this file into many "chunks" which will be loaded into the working environment with read_chunk but won't be evaluated until explicitly told.
In order to externalise your config.R using read_chunk() you would write the R script as:
config.R
# ---- config_preamble
## setup code that is required for config.R
## to run but not for main.Rmd
# ---- first_config_chunk
library(nycflights13)
library(MASS)
# ---- second_config_chunk
y <- 1
# ---- third_config_chunk
some_function <- function(x) {
x + y
}
# ---- fourth_config_chunk
some_function(10)
# ---- config_output
## code that is output during `source`
## and not wanted in main.Rmd
print(some_function(10))
To use this script with the externalisation methodology, you would setup main.Rmd as follows:
main.Rmd
```{r, include=FALSE}
knitr::read_chunk('config.R')
```
```{r first_config_chunk}
```
The packages are now loaded.
```{r third_config_chunk}
```
`some_function` is now available.
```{r new_chunk}
y <- 20
```
```{r fourth_config_chunk}
```
## [1] 30
```{r new_chunk_two}
y <- 100
lapply(seq(3), some_function)
```
## [[1]]
## [1] 101
##
## [[2]]
## [1] 102
##
## [[3]]
## [1] 103
```{r source_file_instead}
source("config.R")
```
## [1] 11
As you can see, if you were to source this file, there would be no way to modify the call to some_function prior to execution and the call would print an output of "11". Now that the chunks are available in the environment, they can be re-called any number of times (after for example, changing the value of y) or used any other way in the current environment (eg. new_chunk_two) which would not be possible with source if you didn't want the rest of the R script to execute.

tikzDevice not working for basic example

Consider the following example:
library(tikzDevice)
tikz("test.tex")
plot(1:10)
dev.off()
An error will be produced once the plot is created, with a lot of code but near the end the error appears:
("C:\ProgramError in getMetricsFromLatex(TeXMetrics, verbose = verbose) :
TeX was unable to calculate metrics for the following string
or character:
m
Common reasons for failure include:
* The string contains a character which is special to LaTeX unless
escaped properly, such as % or $.
* The string makes use of LaTeX commands provided by a package and
the tikzDevice was not told to load the package.
The contents of the LaTeX log of the aborted run have been printed above,
it may contain additional details as to why the metric calculation failed.
In addition: Warning messages:
1: In readLines(texLog) :
incomplete final line found on 'C:\Users\cgmil\AppData\Local\Temp\Rtmp6FouwP/tikzStringWidthCalc.log'
2: In readLines(texLog) :
incomplete final line found on 'C:\Users\cgmil\AppData\Local\Temp\Rtmp6FouwP/tikzStringWidthCalc.log'
What's going on?
A bit late...
You should have a .tex file with the tikz code of your plot in C:\Users\cgmil\AppData\Local\Temp\Rtmp6FouwP/. Just replace the last two lines
\makeatletter
\##end
by
\end{document}
\end{tikzpicture}
Then compile the document. New packages will be installed and it should works.

Correctly set R default graphic device to quartz?

I tried to add the following line in my .Rprofile file:
options(device = quartz)
It produced an error:
Error in options(device = quartz) : object 'quartz' not found
Then I tried:
options(device = "quartz")
And it works.
However, both work in the regular R session. Can anyone tell me what is the reason for the difference in behavior?
The erro message says it all. There is no data-object named 'quartz' and the options function is not expecting (nor can it find) a function name as an argument value for the 'device'-node.
You are seeing the effect of the environment where .Rprofile is being evaluated because some of the usual packages (such as stats or graphics) are not yet loaded. Read more about this at ?Startup. You could avoid this by starting .Rprofile with require(grDevices)

Resources