Skip to main content
Chemistry LibreTexts

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

  • Page ID
    216017
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    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)

    5.2.5: How to Incorporate Data Files in Your Code Blocks- For R is shared under a CC BY 1.3 license and was authored, remixed, and/or curated by LibreTexts.