2015-06-21 51 views
1

我已經完成了這項工作,所以我知道這是可能的,這可能是一個非常簡單的問題,所以如果問題不夠好,我很抱歉,但是在這裏是這筆交易:生成帶有R結果的HTML文件的向量

我有一個代碼在R從股票生成幾個分析:日誌返回,直方圖,從它的值和日誌返回等描述性統計。

我想要的是用這個結果做一個很酷的html。我很久以前在舊工作上有類似的事情,但我真的很難記住我是如何將結果放入html中的。

它從一個空對象開始,然後添加html代碼並在代碼中開始插入我的結果。之後,我使用write.table並完成了我的工作。不知道爲什麼這次不工作。我認爲這可能是某些結果所具有的行數和列數,但我無法解決問題。這是將生成的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) 

這是R代碼如下所示:

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

的HTML未能獲得來自R的結果,如LogReturnCsan,DescriptiveStat和直方圖。

這是csan.txt的內容,每列由「,」分隔,小數由「。」分隔。 (它是年,日,月,日月,開盤價值,當日股票的最高價值,當日股票的最低價值,當日收盤價格,交易量,調整後的收盤價格):

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 
+1

如果沒有csan.txt的內容,很難回答你的問題。但無論如何,使用RMarkdown和[knitr](http://yihui.name/knitr/),而不是您目前使用的方法幾乎可以更好地解決這類問題。 –

+0

@NickK我剛剛添加了關於csan.txt的信息...我認爲knitr可能會有很多幫助,但是我過去使用的這種方法看起來很簡單,我想試一試 – dekio

+0

您的問題是您將LogReturnCan,DescriptiveStat和Histogram視爲長度爲1的矢量,而LogReturnCan具有長度4,DescriptiveStat長度6和直方圖是一個列表。你想要的輸出是什麼? –

回答

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) 
+0

它接近我正在尋找,不完全,但它有幫助。謝謝! – dekio

0

這是我如何使用RMD和knitr::knit做到這一點。此文件應保存爲(例如)Stocks.Rmd,然後在與此文件和數據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庫,可以產生很好的格式化的降價表。如果您使用它,則需要記住將這些表的塊選項設置爲results="asis"

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) 

希望能幫助想要做類似報告的人。

乾杯和感謝您的幫助!