1

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
4

3 回答 3

0

一种低效的方法:

#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)
于 2015-06-21T18:53:28.990 回答
0

这就是我使用 Rmd 和knitr::knit. 该文件应保存为(例如)Stocks.Rmd,然后在 R 中输入以下内容,同时与该文件和数据在同一工作目录中knitr::knit("Stocks.Rmd")。或者,Rstudio有一个很棒的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")
```

如果您想要更漂亮的格式,您可以使用pander可以生成格式良好的 Markdown 表的库。results="asis"如果您确实使用它,您需要记住为这些表设置块选项。

于 2015-06-21T18:58:16.600 回答
0

我知道了!刚刚找到一个旧代码。它还不完美,但现在是调整 HTML 部分的问题。逻辑没问题:

#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)

希望这会帮助想要制作类似报告的人。

干杯并感谢您的帮助!

于 2015-06-22T13:11:20.187 回答