Generating vectors that will result in a HTML file with R results

99 Views Asked by At

I have already done this, so I know it's possible, and it might be a very simple issue, so I am sorry if the question is not good enough, but here is the deal:

I have a code in R to generate a few analysis from a stock: the log return, the histogram, a descriptive statistics from its value and the log returns, and so on.

What I want is to make a cool html with this results. I had something similar in my old job long time ago, but I am really struggling to remember exactly how I put the results into the html.

It starts with an empty object, then I add html codes and within the codes I start to insert my results. After that I use write.table and my work is done. Not sure why it is not working this time. I thought it could be something from the number of rows and columns some results have, but I couldn't solve the issue. This is what will generate the html:

HTMLGenerator<- ""
HTMLGenerator[length(HTMLGenerator)+1]<-paste("<!DOCTYPE html>",sep="")
HTMLGenerator[length(HTMLGenerator)+1]<-paste("<html>",sep="")
HTMLGenerator[length(HTMLGenerator)+1]<-paste("<head>",sep="")
HTMLGenerator[length(HTMLGenerator)+1]<-paste("<title>Stock Analysis</title>",sep="")
HTMLGenerator[length(HTMLGenerator)+1]<-paste("</head>",sep="")
HTMLGenerator[length(HTMLGenerator)+1]<-paste("<body>",sep="")
HTMLGenerator[length(HTMLGenerator)+1]<-paste("<h2>Stock Analysis</h2>",sep="")
HTMLGenerator[length(HTMLGenerator)+1]<-paste("<h3>Stock: CSAN3</h3>",sep="")
HTMLGenerator[length(HTMLGenerator)+1]<-paste("<h3>Made by me</h3>",sep="")
HTMLGenerator[length(HTMLGenerator)+1]<-paste("The Log Returns from CSAN3 are:\"",LogReturnCsan,"\" ",sep="" )
HTMLGenerator[length(HTMLGenerator)+1]<-paste(" \"",DescriptiveStat,"\" ",sep="" )
HTMLGenerator[length(HTMLGenerator)+1]<-paste(" \"",Histogram ,"\" ",sep="" )
HTMLGenerator[length(HTMLGenerator)+1]<-paste("</body>",sep="")
write.table(HTMLGenerator,"C:/Users/Desktop/FinalAnalysis.html",sep="\t", quote=FALSE, row.names=FALSE, col.names=FALSE)

And this is what the R code looks like:

#Read the stock information
Csan <- read.table("C:/Users/Desktop/csan.txt",header = TRUE, sep = ",", dec = ".", fill = TRUE)
#get the stock log return based on the close value from each day
LogReturnCsan <- diff(log(Csan$Close))
DescriptiveStat <- summary(LogReturnCsan)
#Makes a histogram with the log returbs
Histogram <- hist(LogReturnCsan, breaks=30, col="burlywood3", main="LN Return Csan3 ")

The HTML is failing to get the results from R such as the LogReturnCsan, DescriptiveStat and Histogram.

This is the content of csan.txt, each column is separated by "," and the decimals by "." (It is the Year, Day, Month, DayMonth, Open Value, Highest Value of the stock in the day, Lowest Value of the stock in the day, Close price in the day, Volume of tradings, Close Value adjusted):

Ano,Dia,Mes,DiaMes,Open,High,Low,Close,Volume,AdjClose
2010,04,01,04 - 01,22.6185,22.7429,21.9964,22.6629,1088200,20.10939
2010,05,01,05 - 01,22.7696,23.0006,22.103,22.6718,2295300,20.11728
2010,06,01,06 - 01,22.503,22.7518,21.8364,22.023,2115500,19.54159
2010,07,01,07 - 01,21.7297,21.8186,20.3078,20.8499,8368700,18.50066
3

There are 3 best solutions below

1
Robert On BEST ANSWER

One inefficient way to do this:

#get the stock log return based on the close value from each day
LogReturnCsan <- data.frame(LogReturnCsan=diff(log(Csan$Close)))
DescriptiveStat <- summary(LogReturnCsan)
#Makes a histogram with the log returbs
Histogram <- hist(LogReturnCsan[,1], breaks=30, col="burlywood3", main="LN Return Csan3 ")

#If you want the Hist as table
Histogram$counts=c(NA,Histogram$counts)
Histogram$density=c(NA,Histogram$density)
Histogram$mids=c(NA,Histogram$mids)
Histtab=do.call(rbind,Histogram[1:4])

HTMLGenerator<- ""
HTMLGenerator[length(HTMLGenerator)+1]<-paste("<!DOCTYPE html>",sep="")
HTMLGenerator[length(HTMLGenerator)+1]<-paste("<html>",sep="")
HTMLGenerator[length(HTMLGenerator)+1]<-paste("<head>",sep="")
HTMLGenerator[length(HTMLGenerator)+1]<-paste("<title>Stock Analysis</title>",sep="")
HTMLGenerator[length(HTMLGenerator)+1]<-paste("</head>",sep="")
HTMLGenerator[length(HTMLGenerator)+1]<-paste("<body>",sep="")
HTMLGenerator[length(HTMLGenerator)+1]<-paste("<h2>Stock Analysis</h2>",sep="")
HTMLGenerator[length(HTMLGenerator)+1]<-paste("<h3>Stock: CSAN3</h3>",sep="")
HTMLGenerator[length(HTMLGenerator)+1]<-paste("<h3>Made by me</h3>",sep="")
write.table(HTMLGenerator,"FinalAnalysis.html",sep="\t", quote=FALSE, row.names=FALSE, col.names=FALSE)

library("xtable")
print(xtable(LogReturnCsan,caption = "Log Returns"), type="html",file="FinalAnalysis.html", append=TRUE)
print(xtable(DescriptiveStat,caption = "Descriptive Stats"), type="html",file="FinalAnalysis.html", append=TRUE)
print(xtable(Histtab,caption="Histogram Stats"), type="html",file="FinalAnalysis.html", append=TRUE)

endfile<-paste("</body>",sep="")
write.table(endfile,"FinalAnalysis.html",sep="\t", quote=FALSE, row.names=FALSE, col.names=FALSE,append = TRUE)
0
Nick Kennedy On

This is how I'd do it using Rmd and knitr::knit. This file should be saved as (e.g.) Stocks.Rmd and then the following entered in R while in the same working directory as both this file and the data knitr::knit("Stocks.Rmd"). Alternatively, Rstudio has a great interface to knitr.

---
title: "Stocks analysis"
author: "by Me"
output: html_document
---

Stock: CSAN3
------------

```{r setup, echo=FALSE}
#Read the stock information
Csan <- read.csv("csan.txt", fill = TRUE)
#get the stock log return based on the close value from each day
LogReturnCsan <- diff(log(Csan$Close))
DescriptiveStat <- summary(LogReturnCsan)
#Makes a histogram with the log returbs
```

The Log Returns from CSAN3 are:

```{r}
LogReturnCsan
DescriptiveStat
```
```{r echo=FALSE}
hist(LogReturnCsan, breaks=30, col="burlywood3")
```

If you want prettier formatting, you could use the pander library which can produce nicely formatted Markdown tables. You'll need to remember to set the chunk options for those tables to be results="asis" if you do use it.

0
dekio On

I got it! Just found an old code. It is not perfect yet, but now is a matter of adjusting the HTML part. The logic is ok:

#Read the stock information
Csan <- read.table("C:/Users/Desktop/csan.txt",header = TRUE, sep = ",", dec = ".", fill = TRUE)
#get the stock log return based on the close value from each day
LogReturnCsan <- c(1,diff(log(Csan$Close)))
DescriptiveStat <- summary(LogReturnCsan[-1])
#Makes a histogram with the log returbs
Histogram <- hist(LogReturnCsan[-1], breaks=30, col="burlywood3", main="LN Return Csan3 ")
HTMLGenerator<-""
HTMLGenerator[length(HTMLGenerator)+1]<-paste("<!DOCTYPE html>",sep="")
HTMLGenerator[length(HTMLGenerator)+1]<-paste("<html>",sep="")
HTMLGenerator[length(HTMLGenerator)+1]<-paste("<head>",sep="")
HTMLGenerator[length(HTMLGenerator)+1]<-paste("<title>Stock Analysis</title>",sep="")
HTMLGenerator[length(HTMLGenerator)+1]<-paste("</head>",sep="")
HTMLGenerator[length(HTMLGenerator)+1]<-paste("<body>",sep="")
HTMLGenerator[length(HTMLGenerator)+1]<-paste("<h2>Stock Analysis</h2>",sep="")
HTMLGenerator[length(HTMLGenerator)+1]<-paste("<h3>Stock: CSAN3</h3>",sep="")
HTMLGenerator[length(HTMLGenerator)+1]<-paste("<h3>Made by me</h3>",sep="")
HTMLGenerator[length(HTMLGenerator)+1]<-paste("The Log Returns from CSAN3 are:",sep="" )
HTMLGenerator[length(HTMLGenerator)+1]<-paste("                                         <left>  ",sep="")
HTMLGenerator[length(HTMLGenerator)+1]<-paste("                                             <table id='hor-minimalist-b-big'>       ",sep="")
HTMLGenerator[length(HTMLGenerator)+1]<-paste("                                                 <thead>     ",sep="")
HTMLGenerator[length(HTMLGenerator)+1]<-paste("                                                 </thead>        ",sep="")
HTMLGenerator[length(HTMLGenerator)+1]<-paste("                                                 <tbody>     ",sep="")
for (i in 1:length(LogReturnCsan)) {HTMLGenerator[length(HTMLGenerator)+1]<-paste(" ",LogReturnCsan[i]," ",sep="")} 
HTMLGenerator[length(HTMLGenerator)+1]<-paste("                                                 </tr>   ",sep="")
HTMLGenerator[length(HTMLGenerator)+1]<-paste("                                                 </tbody>        ",sep="")
HTMLGenerator[length(HTMLGenerator)+1]<-paste("                                             </table>        ",sep="")
HTMLGenerator[length(HTMLGenerator)+1]<-paste("                                         </left>     ",sep="")
HTMLGenerator[length(HTMLGenerator)+1]<-paste("The Descriptive Statistic for CSAN3 is:",sep="" )
HTMLGenerator[length(HTMLGenerator)+1]<-paste("                                         <left>  ",sep="")
HTMLGenerator[length(HTMLGenerator)+1]<-paste("                                             <table id='hor-minimalist-b-big'>       ",sep="")
HTMLGenerator[length(HTMLGenerator)+1]<-paste("                                                 <thead>     ",sep="")
HTMLGenerator[length(HTMLGenerator)+1]<-paste("                                                 </thead>        ",sep="")
HTMLGenerator[length(HTMLGenerator)+1]<-paste("                                                 <tbody>     ",sep="")
for (i in 1:length(DescriptiveStat)) {HTMLGenerator[length(HTMLGenerator)+1]<-paste(" ",DescriptiveStat[i]," ",sep="")} 
HTMLGenerator[length(HTMLGenerator)+1]<-paste("                                                 </tr>   ",sep="")
HTMLGenerator[length(HTMLGenerator)+1]<-paste("                                                 </tbody>        ",sep="")
HTMLGenerator[length(HTMLGenerator)+1]<-paste("                                             </table>        ",sep="")
HTMLGenerator[length(HTMLGenerator)+1]<-paste("                                         </left>     ",sep="")

write.table(HTMLGenerator,"C:/Users/Desktop/FinalAnalysis.html",sep="\t", quote=FALSE, row.names=FALSE, col.names=FALSE)

Hope that will help someone who wants to make similar reports.

Cheers and thanks for the help!