全部版块 我的主页
论坛 计量经济学与统计论坛 五区 计量经济学与统计软件 LATEX论坛
2097 2
2016-06-18
By Riddhiman








(This article was first published on Modern Data » R, and kindly contributed to R-bloggers)

R-Markdown is a great way to create dynamic documents with embedded chunks of R code. The document is self contained and fully reproducible which makes it very easy to share. This post will be the first in a multi part series on how to embed Plotly graphs in R-Markdown documents as well as presentations.
R-Markdown is a flavor of markdown which allows R-users to embed R code into a markdown document. The document is then ‘knit’ using knitr to create a HTML file.
Getting started# Not run# install.packages('rmarkdown')# install.packages('plotly')
  • Note: you can download the function definition forGetYahooData() here
  • Now we should be good to go…

Creating a R-Markdown document
  • Click on File -> New File -> R Markdown as shown below.
  • You should now see a dialog as shown below. Select ‘Document’ in the left panel and fill in title and author field and hit ‘OK’.


  • You should now have a document which looks like this –


Code chunksTo embed R code into the document, code needs to be inserted as shown below.


Inserting a Plotly graphInserting an awesome interactive Plotly chart is as simple as printing out the plotly object in a code chunk. Use the code snippet below.
source("Yahoo Stock Data Pull.R")AAPL <-  GetYahooData("AAPL")IBM <- GetYahooData("IBM")# Plotly chart library(plotly)mat <-  data.frame(Date = AAPL$Date,                    AAPL = round(AAPL$Adj.Close,2),                   IBM = round(IBM$Adj.Close,2))p <- mat %>%   plot_ly(x = Date, y = AAPL, fill = "tozeroy", name = "Microsoft") %>%   add_trace(y = IBM, fill = "tonexty", name = "IBM") %>%   layout(title = "Stock Prices",          xaxis = list(title = "Time"),         yaxis = list(title = "Stock Prices"))p  # Thats it !
Knitting the R-Markdown documentNow that our R-Markdown document is complete with text, code and graphs, we can go ahead and click the little ‘Knit HTML’ button to generate a HTML file.

We now have a nicely formatted HTML file !



二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2019-7-26 00:04:58
thanks for sharing
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2020-4-16 00:24:05
thx for sharing~
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群