【问题标题】:R Shiny: Read CSV from webpage, incomplete final line found by readTableHeaderR Shiny:从网页读取 CSV,readTableHeader 找到的最后一行不完整
【发布时间】:2017-10-26 16:24:05
【问题描述】:

我有一个问题,我似乎无法找到解决方案。我正在从网络上的 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 文件并链接到同一个(大)文件,它可以工作)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    您正在读取的文件是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")
    

    【讨论】:

    • 非常感谢!我只是没有意识到这是一个 JSON 文件。现在我在 URL 中看到它。你让我开心!
    猜你喜欢
    • 2014-04-05
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 2011-08-24
    • 2014-09-15
    • 1970-01-01
    • 1970-01-01
    • 2010-10-24
    相关资源
    最近更新 更多