Skip to main content
v2026.11,986 entries · CC-BY 4.0

How to Import a Dataset in RStudio (CSV, Excel, SPSS)

Import a dataset in RStudio with File > Import Dataset, then keep the Code Preview so the import is reproducible. Covers CSV, Excel and SPSS defaults.

Written and maintained by CASRAI Editorial Board

Published

On this page

To import a dataset in RStudio, choose File > Import Dataset, pick the group that matches your file (text, Excel or statistical software), set the options in the wizard, check the preview, and press Import. The wizard writes and runs R code for you, and that code is what makes the import repeatable. The same menu is how you import a dataset into RStudio from a URL as well as from a local file. This guide covers CSV and other delimited text, Excel workbooks and SPSS files, shows the code behind each route, and explains what the documented defaults do to your data.

What it is used for in research

Most analysis in R starts by turning a file into a data frame, a table with one column per variable and one row per observation. The Import Dataset wizard in RStudio is a point-and-click front end for that step. According to the Posit documentation on local data, the wizard has three groups: text data, Excel data and statistical data. Each opens a dialog with a file or URL field, a preview of the parsed table and a Code Preview box that shows the R call the wizard will run.

For research work the important point is reproducibility. A mouse-driven import leaves no record unless you keep the generated code. The Posit documentation states that the final step is to press Import to execute the code under Code Preview, so the Code Preview text is the exact instruction that produced your data frame. Copy it into your analysis script and your import is documented and can be rerun. For how data frames behave once they are loaded, see data frames in R programming.

Install or access

The Import Dataset command is part of the RStudio IDE; Posit documents it under File > Import Dataset, with the same options available from the dropdown in the Environment pane. RStudio itself is distributed by Posit. The wizard calls functions from R packages, and the readr, readxl and haven packages are documented on the tidyverse project sites listed under Sources. If R reports that a package is missing, install it as you would any other package; the steps are in how to install libraries in R.

Step-by-step: import a dataset in RStudio

Import a CSV or other text file

  1. Choose File > Import Dataset > From Text (readr). This opens the Import Text Data dialog.
  2. Enter the file location in the file field, or paste a URL. The Posit documentation lists both the file system and a URL as sources.
  3. Set the options you need. The documented options include skipping the first N rows, changing the column delimiter, setting the encoding, and setting the quote, escape, comment and NA identifiers.
  4. Check the Data Preview. If a column looks wrong, adjust the options and look at the preview again before continuing.
  5. Read the Code Preview, then press Import to run it.

The “From Text (base)” entry in the same menu imports text through base R instead of readr. The choice between the two matters because their defaults differ; the table below compares them.

Import an Excel workbook

  1. Choose File > Import Dataset > From Excel, which opens the Import Excel Data dialog.
  2. Select the file, then choose the sheet to read. The wizard lets you select a specific sheet, skip the first N rows and set NA identifiers.
  3. Posit’s own example is a workbook called deaths.xlsx, where the table only parses correctly after setting Skip to 4 and pressing Update. If your workbook has title text above the header row, the same approach applies.
  4. Check the preview and the Code Preview, then press Import.

Import an SPSS, SAS or Stata file

  1. Choose File > Import Dataset and pick the matching entry from the statistical data group.
  2. Enter the file or URL in the File/Url field and press Update.
  3. Rename the dataset if you want a different object name, and specify a model file where the dialog asks for one.
  4. Press Import.

Do the same import in code

Once you have seen the Code Preview, you can skip the wizard. The functions below are the ones documented by their package authors; each reads a file and returns a data frame (a tibble for the tidyverse packages).

library(readr)
library(readxl)
library(haven)

survey  <- read_csv("survey.csv")                  # comma-separated text
survey2 <- read_csv2("survey_eu.csv")              # semicolon separator, comma decimal
budget  <- read_excel("budget.xlsx", sheet = 1)    # first sheet by default
budget2 <- read_excel("budget.xlsx", range = "B3:D87")
spss    <- read_sav("cohort.sav")                  # SPSS .sav / .zsav
plain   <- read.csv("survey.csv")                  # base R alternative

The file names are placeholders for your own files. The wizard is not required for any of these calls; it simply generates equivalent code.

Output and how to read it

After you press Import, the new data frame appears in the Environment pane under the name shown in the dialog. Open it from there, or in the console, to confirm the number of rows and columns and that each column has the type you expect.

Column types deserve a deliberate check. The readr documentation says that when col_types is left at NULL, the types are inferred from the first guess_max rows, which means a column whose first values look numeric can be read differently from one whose type only becomes clear later. The same documentation says that if there are parsing problems, a warning will alert you. Treat that warning as a result to investigate rather than noise.

Missing values need the same attention. The readr default for na is c(“”, “NA”), so empty cells and the text NA become missing values, and any other missing-data code in your file, such as a sentinel number or a different string, stays as ordinary data unless you add it to the NA identifiers. That is exactly what the NA identifiers option in the wizard controls.

For SPSS files, the haven documentation says read_sav() returns a tibble, stores variable labels in the “label” attribute, and with the default user_na = FALSE converts user-defined missing values to NA. Setting user_na = TRUE instead keeps them as labelled_spss() values. Which setting is correct depends on whether your analysis needs to distinguish kinds of missingness. Once the data are loaded, summary statistics in R is a sensible first check, and analysing data in SPSS covers the other side of that file format.

Troubleshooting

Everything lands in one column

The delimiter does not match the file. read_csv() and read_tsv() are special cases of the more general read_delim(), according to the readr documentation, so a file separated by something other than a comma needs the delimiter set in the wizard or a call to read_delim(). A file saved with semicolons and decimal commas, a common export format in some locales, is what read_csv2() is documented to read: it uses ; as the separator and , as the decimal mark.

The header row is wrong or there is junk above it

Use the skip option in the wizard. For Excel, readxl offers an alternative: the range argument, such as “B3:D87” or “Budget!B2:G14”, takes precedence over skip, n_max and sheet, so do not combine them and expect all to apply.

Wrong sheet in an Excel file

readxl reads the first sheet by default. The sheet argument accepts a sheet name as a string or a position as an integer. The package reads both .xls and .xlsx files, detecting the format from the extension and the file, and read_xls() or read_xlsx() skip that guessing if you already know the format. The documentation notes that password-protected workbooks can only be read through read_xlsx().

Numbers imported as text, or a parsing warning

Because types are guessed from a limited number of rows, set col_types explicitly in the call, or raise guess_max, then re-run. Keep the corrected call in your script rather than only in the wizard.

You expected a wizard in Positron

The workflow above is specific to the RStudio IDE. Readers who work in Posit’s newer IDE can use the code routes in the previous section; see the Positron IDE guide for what that environment provides. If you save cleaned data for later sessions, the RDS file format is the native R way to store an object.

Free alternatives to the wizard

Every route in this guide is free software; the choice is between clicking and writing code. The data table below lists the main functions with their documented defaults, so you can pick the right one for your file. In short:

Approach Best matched to Documented detail
Import Dataset wizard, From Text (readr) CSV or delimited text, first-time import Options for skip, delimiter, encoding, quote, escape, comment and NA identifiers; shows a Code Preview
read_csv() / read_delim() (readr) Scripted import of text files read_csv() and read_tsv() are special cases of read_delim()
read_excel() (readxl) Excel .xls and .xlsx sheet by name or position; range overrides skip, n_max and sheet
read_sav() (haven) SPSS .sav and .zsav Returns a tibble; variable labels kept in the “label” attribute
read.csv() (base R, utils) Text files without extra packages Same as read.table() with different defaults, including header = TRUE and sep = “,”

Frequently asked questions

How do I import a dataset into RStudio from a CSV file?

Use File > Import Dataset > From Text (readr), choose the file or paste a URL, review the preview, and press Import. The equivalent code is read_csv(“yourfile.csv”).

How do I import an Excel file into RStudio?

Use File > Import Dataset > From Excel, select the sheet, set Skip if there are rows above the header, and press Import. In code, use read_excel() from readxl.

Can I import a dataset in RStudio without the wizard?

Yes. The wizard generates ordinary R code, shown in the Code Preview, so you can run that code directly in the console or a script.

Why does my imported data have the wrong column types?

readr infers types from the first guess_max rows when col_types is NULL. Specify col_types or increase guess_max.

How do I keep my import reproducible?

Paste the Code Preview text into your script before or right after pressing Import, so the call is saved with your analysis.

Sources

Data: Documented defaults of R functions behind the RStudio Import Dataset routes

Compares the R import functions that the RStudio Import Dataset wizard corresponds to (readr, readxl, haven, base R), listing the file types each reads and defaults stated in each package's reference documentation. Compiled from the packages' official reference pages.

Period covered: 2026.

Documented defaults of R functions behind the RStudio Import Dataset routes
FunctionPackageReadsDocumented default or behaviour
read_csv()readrComma-separated textSpecial case of read_delim(); na = c("", "NA"); column types inferred from guess_max rows when col_types is NULL
read_csv2()readrSemicolon-separated textUses ; as separator and , as decimal mark
read_excel()readxl.xls and .xlsxReads the first sheet by default; sheet by name or integer; range takes precedence over skip, n_max and sheet; returns a tibble
read_sav()havenSPSS .sav and .zsavReturns a tibble; user_na = FALSE converts user-defined missing values to NA; variable labels stored in the label attribute
read.csv()utils (base R)Comma-separated textSame as read.table() with different defaults: header = TRUE, sep = ",", dec = ".", stringsAsFactors = FALSE

Sources

Compiled by CASRAI from the sources above. Free to reuse under CC BY 4.0 with attribution.

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about How to Import a Dataset in RStudio (CSV, Excel, SPSS)

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →