r_code

Use download.file

参考 Schouwenaars (2016)

web excel

对于在 Web 上的 Excel

  1. gdata 可以直接连接
  2. readxl 必须先下载后才能连接,使用 download.file 函数,必须指定 dest.file 参数

具体参考 Reference

# Load the readxl and gdata package
library(readxl)
# library(gdata)

# Specification of url: url_xls
url_xls <- "http://s3.amazonaws.com/assets.datacamp.com/production/course_1478/datasets/latitude.xls"

# excel_gdata <- read.xls(url_xls)

download.file(url_xls, destfile = "local_latitude.xls")
library(readxl)
excel_readxl <- read_excel("datasets/latitude.xls")
excel_readxl <- read_excel("datasets/latitude.xlsx")

web RData

You can load data from an RData file using the load() function, but this function does not accept a URL string as an argument. In this exercise, you’ll first download the RData file securely, and then import the local data file. DataCamp

也需要下载。

# https URL to the wine RData file.
url_rdata <- "https://s3.amazonaws.com/assets.datacamp.com/production/course_1478/datasets/wine.RData"

# Download the wine file to your working directory
download.file(url_rdata,"wine_local.RData")

# Load the wine data into your workspace using load()
load("wine_local.RData")

# Print out the summary of the wine data
wine
trying URL 'https://s3.amazonaws.com/assets.datacamp.com/production/course_1478/datasets/wine.RData'
InternetOpenUrl failed: '�޷����������������'Error in download.file(url_rdata, "wine_local.RData") : 
  cannot open URL 'https://s3.amazonaws.com/assets.datacamp.com/production/course_1478/datasets/wine.RData'

报错。

Schouwenaars, Filip. 2016. “Importing Data in R (Part 2).” 2016. <https://www.datacamp.com/courses/importing-data-in-r-part-2>.