生成将生成带有R结果的HTML文件的向量

生成将生成带有R结果的HTML文件的向量,html,r,write.table,Html,R,Write.table,我已经这样做了,所以我知道这是可能的,这可能是一个非常简单的问题,所以如果这个问题不够好,我很抱歉,但这里是交易: 我在R中有一个代码,可以从股票中生成一些分析:对数收益率、柱状图、基于其值和对数收益率的描述性统计数据,等等 我想要的是用这个结果制作一个很酷的html。很久以前,我的旧工作中也有类似的东西,但我真的很难准确地记住我是如何将结果放入html的 它从一个空对象开始,然后我添加html代码,并在代码中插入结果。之后,我使用write.table,我的工作就完成了。不知道为什么这次不起作

我已经这样做了,所以我知道这是可能的,这可能是一个非常简单的问题,所以如果这个问题不够好,我很抱歉,但这里是交易:

我在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)

一种低效的方法是:

#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)
#根据每天的收盘价获取股票日志回报

LogReturnCsan一种低效的方法:

#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)
#根据每天的收盘价获取股票日志回报

LogReturnCsan这就是我使用Rmd和
knitr::knit
的方法。此文件应保存为(例如)Stocks.Rmd,然后在R中输入以下内容,同时将其保存在与此文件和数据相同的工作目录中
knitr::knit(“Stocks.Rmd”)
。或者,它有一个很棒的接口,可以连接到
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")
```
---
标题:“股票分析”
作者:“由我”
输出:html\u文档
---
股票:CSAN3
------------
```{r设置,echo=FALSE}
#阅读股票信息

Csan这就是我使用Rmd和
knitr::knit
的方法。此文件应保存为(例如)Stocks.Rmd,然后在R中输入以下内容,同时将其保存在与此文件和数据相同的工作目录中
knitr::knit(“Stocks.Rmd”)
。或者,它有一个很棒的接口,可以连接到
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")
```
---
标题:“股票分析”
作者:“由我”
输出:html\u文档
---
股票:CSAN3
------------
```{r设置,echo=FALSE}
#阅读股票信息

我明白了!刚找到一个旧代码。它还不完美,但现在需要调整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)
#阅读股票信息

我明白了!刚找到一个旧代码。它还不完美,但现在需要调整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)
#阅读股票信息

如果没有Csan.txt的内容,很难回答您的问题。但在任何情况下,使用RMarkdown几乎肯定比使用您当前使用的方法更好地解决这类问题。@NickK我刚刚在csan.txt上添加了信息。。。我认为knitr可能会有很大帮助,但我在过去使用的这种方法看起来非常简单,我想给出一个例子。您的问题是,您将LogReturnCsan、DescriptiveStat和Histogram视为长度为1的向量,而LogReturnCsan的长度为4,DescriptiveStat的长度为6,Histogram是一个列表。你想要的输出是什么?@NickK你说得对!我认为这是个问题。我试过LogReturnCsan[1],效果很好。然而,我仍然在努力以一种我希望看到的方式(例如,以表格的形式)来构建这个HTML。此外,我还必须进行LogReturnCsan[2]等等,直到最后。我还是不知道该怎么做。也许是htmls代码的某种循环和技巧。。。不确定…如果没有csan.txt的内容,很难回答您的问题。但在任何情况下,使用RMarkdown几乎肯定比使用您当前使用的方法更好地解决这类问题。@NickK我刚刚在csan.txt上添加了信息。。。我认为knitr可能会有很大帮助,但我在过去使用的这种方法看起来非常简单,我想给出一个例子。您的问题是,您将LogReturnCsan、DescriptiveStat和Histogram视为长度为1的向量,而LogReturnCsan的长度为4,DescriptiveStat的长度为6,Histogram是一个列表。你想要的输出是什么?@NickK你说得对!我认为这是个问题。我试过LogReturnCsan[1],效果很好。然而,我仍然在努力以一种我希望看到的方式(例如,以表格的形式)来构建这个HTML。此外,我还必须进行LogReturnCsan[2]等等,直到最后。我还是不知道该怎么做。也许是htmls代码的某种循环和技巧。。。不确定…它和我想要的很接近,不完全一样,但它很有帮助。谢谢它和我想要的很接近,不完全一样,但它很有帮助。谢谢