1
這是我第一次同時使用rcharts和閃亮。我試圖創建一個基本的閃亮應用程序,用戶可以從下拉菜單中選擇股票,並在交互圖表中比較它們的表現(我想使用rcharts進行交互)。RCharts + Shiny動態改變圖表數據
我已經設法創建了下拉菜單和rcharts圖表,但無法獲取圖表創建的數據以進行更改。代碼如下:
ui.R
shinyUI(fluidPage(
titlePanel("S&P 500 Retroactive Stock Comparision - 2009 and 2010"),
sidebarLayout(
sidebarPanel(uiOutput("stockA"), uiOutput("stockB"),submitButton("Submit")),
mainPanel(showOutput("StockPlot","NVD3"))
)
))
server.R:
stock.names<-reactive({
## data from : http://pages.swcp.com/stocks/#historical%20data
stocks_data<-read.table("S.P.500_data.txt",sep=",")
colnames(stocks_data)<-c("Date","Ticker","Open","High","Low","Close","Volume")
stocks_data$Date<- as.Date(as.character(stocks_data$Date), "%Y%m%d")
stocks_data$Percent.Change<-((stocks_data$High-stocks_data$Low)/stocks_data$Open)*100
#get company names + match with ticker symbols
library(RCurl)
company_names1 <- "http://data.okfn.org/data/core/s-and-p-500-companies/r/constituents.csv"
company_names2<-getURL(company_names1)
company_names3<-read.csv(textConnection(company_names2))
stocks_data$Company.Names<-company_names3[match(stocks_data$Ticker,company_names3$Symbol),"Name"]
x<-sort(unique(as.character(stocks_data$Company.Names)))
return(x)
})
library(rCharts)
shinyServer(function(input,output){
########
output$stockA <- renderUI({
selectInput("stockA", "Select your first stock to compare:",choices=stock.names())
})
##########
output$stockB <- renderUI({
selectInput("stockB", "Select your second stock to compare:",choices=stock.names())
})
########
stockA.name<- reactive({
input$StockA
})
#########
stockB.name<- reactive({
input$StockB
})
#######
chart.data<-reactive({
data<-subset(stocks_data, Company.Names %in% input$stockA | Company.Names %in% input$stockB)
return(data)
})
#######
output$StockPlot<-renderChart2({
plot1<-nPlot(Close~Date,data=chart.data() ,group='Company.Names',type='lineChart')
return(plot1)
})
}
)
可悲的是沒有工作(除非下面的代碼是錯誤的。我沒有保留stockA/B.name代碼,因爲它沒有實際使用):'observeEvent(輸入$ Submit,{ chart.data <-subset(stocks_data,Company.Names%in%input $ stockA | Company.Names %in input%stockB) ####### output $ StockPlot <-renderChart2({0} {0} {plot1 <-nPlot(Close_ Date,data = chart.data(),group ='Company.Names' ,type ='lineChart') return(plot1) }) })' – user3711502
我沒有看到你在服務器腳本之外有反應......很多事情......你有沒有看過這個例子? http://shiny.rstudio.com/tutorial/lesson6/ –
我做過,但由於它使用了quantmod包/ getSymbols命令,所以對我來說並不是很有用。我得到了反應的總體思路,只是不確定如何製作反應數據框架/從中創建圖表。 – user3711502