我遇到了一个无法找到解决方案的问题。我正在从网络上的URL读取一个CSV,有点像这样(真正的有4个列):
来自的样本数据 CSV
日期--------------------------关闭
2017-10-23 ----------------- 156.17
2017-10-20 ----------------- 156.16
2017-10-19 ----------------- 155.98
2017-10-18 ----------------- 159.76
2017-10-17 ----------------- 160.47
2017-10-16 ----------------- 159.88
2017-10-13 ----------------- 156.99
2017-10-12 ----------------- 156
2017-10-11 ----------------- 156.55
CSV文件继续这样(每天都有记录)回到1980年左右。在R Shiny中,我使用这样的plot命令...
stockData <- read.csv(url("https://www.quandl.com/api/v3/datasets/WIKI/FB/data.json?api_key=xTLatSPBnz751sCMECza"), header=T, sep=",")
...然后继续使用此代码:
plot(stockData$Date, stockData$Close, main="", type="l", las="1",
xlab="Date", ylab="Share Price", panel.first = grid())
points(x=stockData$Date, y=stockData$Close, col='#f44242', type='l', lwd=2)
grid (10,10, lty = 6, col = "lightgray")
......我收到了这些错误:
Warning in read.table(file = file, header = header, sep = sep, quote = quote, :
incomplete final line found by readTableHeader on 'https://www.quandl.com/api/v3/datasets/WIKI/FB/data.json?api_key=xTLatSPBnz751sCMECza'
Warning in min(x) : no non-missing arguments to min; returning Inf
Warning in max(x) : no non-missing arguments to max; returning -Inf
Warning in min(x) : no non-missing arguments to min; returning Inf
Warning in max(x) : no non-missing arguments to max; returning -Inf
Warning: Error in plot.window: need finite 'xlim' values
我不知道哪些错误是相关的,所以如果有人能够解释我做错了什么,那就太棒了。它是关于文件是否大的问题,我该如何测试?或者它与此完全无关? (注意:我下载了CSV并链接到相同的(大)文件并且有效)
答案 0 :(得分:0)
您正在阅读的文件是json
,需要对其进行解析,然后转换为dataframe
。请使用以下代码:
library(jsonlite)
df <- fromJSON("https://www.quandl.com/api/v3/datasets/WIKI/FB/data.json?api_key=xTLatSPBnz751sCMECza", flatten = T)
stockData <- data.frame(df$dataset_data$data)
names(stockData) <- df$dataset_data$column_names
stockData$Close <- as.numeric(stockData$Close)
plot(stockData$Date, stockData$Close, main="", type="l", las="1",
xlab="Date", ylab="Share Price")
points(x=stockData$Date, y=stockData$Close, col='#f44242', type='l', lwd=2)
grid (10,10, lty = 6, col = "lightgray")