生成将生成包含 R 结果的 HTML 文件的向量
Generating vectors that will result in a HTML file with R results
我已经这样做了,所以我知道这是可能的,这可能是一个非常简单的问题,所以如果问题不够好,我很抱歉,但这是交易:
我在 R 中有一个代码可以从股票中生成一些分析:日志 return、直方图、来自其值的描述性统计信息和日志 returns,等等.
我想要的是用这个结果做一个很酷的 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
一种低效的方法:
#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)
这就是我使用 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"
。
我明白了!刚刚找到一个旧代码。它还不完美,但现在是调整 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)
希望这对想要进行类似报告的人有所帮助。
干杯,感谢您的帮助!
我已经这样做了,所以我知道这是可能的,这可能是一个非常简单的问题,所以如果问题不够好,我很抱歉,但这是交易:
我在 R 中有一个代码可以从股票中生成一些分析:日志 return、直方图、来自其值的描述性统计信息和日志 returns,等等.
我想要的是用这个结果做一个很酷的 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
一种低效的方法:
#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)
这就是我使用 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"
。
我明白了!刚刚找到一个旧代码。它还不完美,但现在是调整 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)
希望这对想要进行类似报告的人有所帮助。
干杯,感谢您的帮助!