我已經完成了這項工作,所以我知道這是可能的,這可能是一個非常簡單的問題,所以如果問題不夠好,我很抱歉,但是在這裏是這筆交易:生成帶有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
如果沒有csan.txt的內容,很難回答你的問題。但無論如何,使用RMarkdown和[knitr](http://yihui.name/knitr/),而不是您目前使用的方法幾乎可以更好地解決這類問題。 –
@NickK我剛剛添加了關於csan.txt的信息...我認爲knitr可能會有很多幫助,但是我過去使用的這種方法看起來很簡單,我想試一試 – dekio
您的問題是您將LogReturnCan,DescriptiveStat和Histogram視爲長度爲1的矢量,而LogReturnCan具有長度4,DescriptiveStat長度6和直方圖是一個列表。你想要的輸出是什麼? –