/
report_code_unknown.Rmd
141 lines (118 loc) · 4.81 KB
/
report_code_unknown.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
---
title: "Principal Components Analysis"
author: "Statsomat.com"
date: "`r format(Sys.time(), '%d %B %Y')`"
output:
pdf_document:
latex_engine: xelatex
toc: true
toc_depth: 2
bibliography: ["references.bib"]
params:
data: NA
filename: NA
fencoding: NA
decimal: NA
enc_guessed: NA
vars1: NA
header-includes:
- \usepackage{xcolor}
- \setmainfont[BoldFont=FiraSans-Bold, Extension=.otf]{FiraSans-Regular}
- \usepackage{booktabs}
- \usepackage{longtable}
- \usepackage{float}
---
```{r child = 'report_kernel.Rmd'}
```
```{r include=FALSE}
if (exists("eval3")) eval_code <- eval3 else eval_code <- FALSE
```
```{r, results="asis", eval=eval_code, echo=FALSE}
cat("\n# R Packages", fill=TRUE)
cat("To run the code you need to install following R packages:", fill=TRUE)
cat("\\newline ")
cat("R version:", paste0(R.Version()[c("major","minor")], collapse = "."), fill=TRUE)
cat("\\newline ")
cat("Package corrplot, version:", paste0(packageVersion("corrplot")), fill=TRUE)
cat("\\newline ")
cat("Package data.table, version:", paste0(packageVersion("data.table")), fill=TRUE)
cat("\\newline ")
cat("Package knitr, version:", paste0(packageVersion("knitr")), fill=TRUE)
cat("\\newline ")
cat("Package factoextra, version:", paste0(packageVersion("factoextra")), fill=TRUE)
cat("\\newline ")
cat("Package FactoInvestigate, version:", paste0(packageVersion("FactoInvestigate")), fill=TRUE)
cat("\\newline ")
cat("Package FactoMineR, version:", paste0(packageVersion("FactoMineR")), fill=TRUE)
cat("\\newline ")
cat("Package gridExtra, version:", paste0(packageVersion("gridExtra")), fill=TRUE)
cat("\\newline ")
cat("Package imputeMissings, version:", paste0(packageVersion("imputeMissings")), fill=TRUE)
cat("\\pagebreak")
```
\pagestyle{empty}
```{r, results="asis", eval=eval_code, echo=FALSE}
cat("\n# R Code ", fill=TRUE)
```
```{r, results="asis", eval=eval_code, echo=FALSE}
cat("\\textcolor{blue}{Instructions ")
cat("\\newline ")
cat("If not already available, please install R, RStudio and the requiered packages listed on the previous page. Copy the code below, paste it carefully in a new R Script within RStudio. For a seamless copy \\& paste process, open the PDF report in a browser. Change the path to your data in the line} ")
cat("`filepath <- ...` ")
cat("\\textcolor{blue}{For Windows users, avoid using paths containing spaces. Run the code. Explore the results (numerical results in the Console, plots in the Plots tab). }")
```
<style>
pre.whites {
background-color: #ffffff !important;
}
</style>
```{whites, eval=FALSE, echo = eval_code}
# Import required libraries
suppressPackageStartupMessages(library(corrplot))
suppressPackageStartupMessages(library(data.table))
suppressPackageStartupMessages(library(knitr))
suppressPackageStartupMessages(library(factoextra))
suppressPackageStartupMessages(library(FactoInvestigate))
suppressPackageStartupMessages(library(FactoMineR))
suppressPackageStartupMessages(library(gridExtra))
suppressPackageStartupMessages(library(imputeMissings))
```
```{r, results="asis", eval=eval_code, echo=FALSE}
cat("\\# `Define the path to your data (please remark the forward slash)`")
cat("\\newline ")
cat("`filepath <-")
cat(paste0("\"C:/",params$filename[1],"\"`"))
```
```{r, results="asis", eval=eval_code, echo=FALSE}
if (params$decimal == "auto"){
cat("\\# `Upload the data`")
cat("\\newline ")
datainput1 <- fread(params$filename$datapath, header = "auto", sep="auto", dec=".", encoding ="unknown", data.table = FALSE, na.strings = "")
# Probably comma as decimal
colnames <- sapply(datainput1, function(col) is.numeric(col) & Negate(is.integer)(col))
if (sum(colnames) == 0L){
cat(paste0("`df <- fread(filepath, header =\"auto\", sep =\"auto\", dec =\",\", encoding =\"unknown\", data.table = FALSE, na.strings = \"\")`"))
} else {
cat(paste0("`df <- fread(filepath, header =\"auto\", sep =\"auto\", dec =\".\", encoding =\"unknown\", data.table = FALSE, na.strings = \"\")`"))
}
} else {
cat("\\# `Upload the data`")
cat("\\newline ")
cat(paste0("`df <- fread(filepath, header = \"auto\", sep =\"auto\", dec = \"", params$decimal,"\", encoding = \"unknown\", data.table = FALSE, na.strings = \"\")`"))
}
```
```{r, results="asis", eval=eval_code, echo=FALSE}
cat("\\# `Convert characters to UTF-8 encoding`")
cat("\\newline ")
cat("\\## `Depending on your local R settings`")
cat("\\newline ")
cat("\\## `you could try to ignore and skip the next 4 lines`")
cat("\\newline ")
cat(paste0("`colnames(df) <- iconv(colnames(df),\"",params$enc_guessed,"\",\"UTF-8\")`"))
cat("\\newline ")
cat(paste0("`col_names <- sapply(df, is.character)`"))
cat("\\newline ")
cat(paste0("`df[ ,col_names] <- sapply(df[, col_names], function(col) iconv(col, \"",params$enc_guessed,"\", \"UTF-8\"))`"))
```
```{r child = 'report_code_common.Rmd', eval=eval_code}
```