Skip to main content
Chemistry LibreTexts

5.2.5: How to Incorporate Data Files in Your Code Blocks- For R

  • Page ID
    216017
  • There are a number of ways data files and can be incorporated into code blocks. However, ensure that you have already followed the steps in the introduction to the CKEditor Binder Plugin Tutorial here.

    Method 1: Using read.csv

    the url for the file can be saved to a variable or be added in all at once as shown below.

    x <- read.csv(url("https://chem.libretexts.org/@api/deki/files/265970/auto-mpg.data"), sep = '\t', head = FALSE)
    print(x)
    
    

    Keep in mind that larger data files will take longer to load.

    Method 2: Using wget or curl through download.file

    Another option to load in a data file is using wget or curl through the download.file function. An example code block is shown below, replace your url with the one in the example. For more information about the download.file function visit this link.

    url <- "https://chem.libretexts.org/@api/deki/files/265970/auto-mpg.data"
    z <- tempfile()
    download.file(url,z,mode="wget")
    file <- read.csv(z, sep = '\t')
    print(file)