我正在用shiny app.
做第一个plotly
网站的实时库存数据
https://finance.yahoo.com/lookup。我已经按照以下代码清理了数据。
url<-("http://finance.yahoo.com/quote/")
stock_pt<-read_html(url)%>%html_nodes(xpath = '//*[@id = "market-summary"]/li')%>%html_nodes(xpath = ".//text()[normalize-space()]")%>%html_text()%>%
as.matrix()%>%as.data.frame()%>%split(1:3)%>%as.data.frame()%>%t()%>%as.data.frame()
names(stock_pt)<-lapply(stock_pt[1,], as.character)
stock_pt<-(stock_pt[-1,])
#change row name
rownames(stock_pt)<-c(1:2)
#two data frame formed
stock_point<-stock_pt[1,]
stock_point<-as.matrix(stock_point)
stock_point[1:15]<-as.numeric(gsub(",", "", stock_point[1:15]))
stock_point<-as.data.frame(lapply(stock_point[1,], function(x) as.numeric(as.character(x))))
stock_point<- stock_point%>%mutate(timestamp=as.POSIXct(format(Sys.time(), "%Y-%m-%d %H:%M:%S")))%>%as.data.frame()%>%
select(timestamp, everything())%>%rename_all(funs(make.names(.)))
这是数据清理后的最终结果。
stock_point
timestamp S.P.500 Dow.30 Nasdaq Russell.2000 Crude.Oil Gold Silver EUR.USD X10.Yr.Bond Vix GBP.USD USD.JPY
1 2018-08-19 08:59:19 2850.13 25669.32 7816.33 1692.95 65.92 1191.8 14.77 1.15 2.87 12.64 1.27 110.49
Bitcoin.USD FTSE.100 Nikkei.225
1 6361.88 7558.59 22270.38
str(stock_point)
'data.frame': 1 obs. of 16 variables:
$ timestamp : POSIXct, format: "2018-08-19 08:59:19"
$ S.P.500 : num 2850
$ Dow.30 : num 25669
$ Nasdaq : num 7816
$ Russell.2000: num 1693
$ Crude.Oil : num 65.9
$ Gold : num 1192
$ Silver : num 14.8
$ EUR.USD : num 1.15
$ X10.Yr.Bond : num 2.87
$ Vix : num 12.6
$ GBP.USD : num 1.27
$ USD.JPY : num 110
$ Bitcoin.USD : num 6362
$ FTSE.100 : num 7559
$ Nikkei.225 : num 22270
class(stock_point)
[1] "data.frame"
现在,通过集成
来制作闪亮的应用程序UI
library(shiny)
library(magrittr)
ui<-shinyServer(fluidPage(
titlePanel("Real Time market stock from Yahoo"),
##side bar and sidepanel
sidebarLayout(
column(8, selectInput("select", label = h3("Select Box"),
choices = list("S.P.500", "Dow.30", "Nasdaq", "Russell.2000", "Crude.Oil","Gold",
"Silver", "EUR.USD", "X10.Yr.Bond", "Vix", "GBP.USD", "USD.JPY", "Bitcoin.USD",
"FTSE.100","Nikkei.225")
))
),
mainPanel(
column(8, plotlyOutput(outputId = "timeseries", width = "800px")),
column(8, plotlyOutput(outputId = "percentage", width = "800px")))
)
))
服务器
server<-shinyServer(function(input, output, session){
# Function to get new observations
stock_point_new<-function(stock_point){
stock_pt<-read_html(url)%>%html_nodes(xpath = '//*[@id = "market-summary"]/li')%>%html_nodes(xpath = ".//text()[normalize-space()]")%>%html_text()%>%
as.matrix()%>%as.data.frame()%>%split(1:3)%>%as.data.frame()%>%t()%>%as.data.frame()
names(stock_pt)<-lapply(stock_pt[1,], as.character)
stock_pt<-(stock_pt[-1,])
#change row name
rownames(stock_pt)<-c(1:2)
#two data frame formed
stock_point<-stock_pt[1,]
stock_point<-as.matrix(stock_point)
stock_point[1:15]<-as.numeric(gsub(",", "", stock_point[1:15]))
stock_point<-as.data.frame(lapply(stock_point[1,], function(x) as.numeric(as.character(x))))
stock_point<- stock_point%>%mutate(timestamp=as.POSIXct(format(Sys.time(), "%Y-%m-%d %H:%M:%S")))%>%as.data.frame()%>%
select(timestamp, everything())%>%rename_all(funs(make.names(.)))
return(stock_point)
}
# Initialize my_data
my_point_data <<- stock_point_new()
# Function to update my_data
update_point_data <- function(){
my_point_data <<- rbind(stock_point_new(), my_point_data)
}
##set some color
plotcolor <- "#F5F1DA"
papercolor <- "#E3DFC8"
# # Plot time series chart
output$timeseries <- renderPlotly({
print("Render")
invalidateLater(15000, session)
update_point_data()
print(my_point_data)
if(input$select == "S.P.500"){
plot_ly(source = "source")%>%add_lines(data = my_point_data, x = my_point_data$timestamp,
y = my_point_data$S.P.500, color ="red", line = list(width = 3))%>%
layout(title = "stock price of different market",
xaxis =(list(title = "time",
gridcolor = "#bfbfbf",
rangeslider = list(type = "date"),
rangeselector = list(
buttons = list(
list(
count = 3,
label = "3 mo",
step = "month",
stepmode = "backward"),
list(
count = 1,
label = "1 yr",
step = "year",
stepmode = "backward"),
list(
count = 1,
label = "YTD",
step = "year",
stepmode = "todate"),
list(step = "all")))
)),
yaxis = list(title = "S.P.500", side = "left", overlaying = "y"),
plot_bgcolor = plotcolor,
paper_bgcolor = papercolor
)
}
else if(input$select == "Bitcoin.USD"){
plot_ly(source = "source")%>%add_lines(data = my_point_data, x = my_point_data$timestamp,
y = my_point_data$Bitcoin.USD, color ="blue", line = list(width = 3),
mode = "markers", marker = list(sizemode = "area", size = my_point_data$timestamp ))%>%
layout(title = "stock price of different market",
xaxis =(list(title = "time",
gridcolor = "#bfbfbf",
rangeslider = list(type = "date"),
rangeselector = list(
buttons = list(
list(
count = 3,
label = "3 mo",
step = "month",
stepmode = "backward"),
list(
count = 1,
label = "1 yr",
step = "year",
stepmode = "backward"),
list(
count = 1,
label = "YTD",
step = "year",
stepmode = "todate"),
list(step = "all")))
)),
yaxis = list(title = "Bitcoin.USD", side = "left", overlaying = "y"),
plot_bgcolor = plotcolor,
paper_bgcolor = papercolor
)
}
})
})
shinyApp(ui = ui, server = server)
我正在尝试制作类似以下yahoo链接的图形:Bitcon.USD from Yahoo
现在我的问题是
每隔15秒,只要新数据在图中更新一次,就会刷新整个图,从而使数据更新速度变慢。并且,如果我选择“ 3个月”或“范围选择器”作为周期,那么在每次更新新数据之后,选择也将到达初始开始位置。
我无法固定“ 5分钟”内的x轴刻度长度,类似于折线图this scale,而是x轴刻度长度具有累加性质。
任何建议总是值得赞赏的。