Read.Table() and Read.CSV Both Error in Rmd

read.table() and read.csv both Error in Rmd

The short answer is:

KNOW THY getwd()

Do not confuse the working directory of your current R console with the working directory of your R Markdown document (which is the directory of this R Markdown document). When in doubt, print out getwd() where you want to know your working directory (e.g. in *.Rmd). Note R Markdown documents are compiled in separate R sessions to enhance reproducibility, so you current R console has nothing to do with the compilation of the R Markdown documents.

read.table() and read.csv both Error in Rmd

The short answer is:

KNOW THY getwd()

Do not confuse the working directory of your current R console with the working directory of your R Markdown document (which is the directory of this R Markdown document). When in doubt, print out getwd() where you want to know your working directory (e.g. in *.Rmd). Note R Markdown documents are compiled in separate R sessions to enhance reproducibility, so you current R console has nothing to do with the compilation of the R Markdown documents.

read.table() and read.csv both Error in Rmd

The short answer is:

KNOW THY getwd()

Do not confuse the working directory of your current R console with the working directory of your R Markdown document (which is the directory of this R Markdown document). When in doubt, print out getwd() where you want to know your working directory (e.g. in *.Rmd). Note R Markdown documents are compiled in separate R sessions to enhance reproducibility, so you current R console has nothing to do with the compilation of the R Markdown documents.

R read.csv from URL error in knitr

Thus adapting @Thomas's answer to your data, the following does the trick.

library(RCurl)
data <- getURL("https://courses.edx.org/c4x/MITx/15.071x_2/asset/WHO.csv",
ssl.verifypeer=0L, followlocation=1L)
read.csv(text=data)

You may also check Error when knitr has to download a zip file, where dropping https for http helped.

Why R Markdown does not import .csv files and says there is no such file at the directory

You might have better luck using the absolute path (or full path) instead of the relative path to the file.

On MacOS, select the file, press Command + I to open up the information pane, and copy the text portion of Where, which should give you the absolute path to the folder the file is in. Copy that to R, and add your file name. There are other ways to do it as well.

The major advantage of using the absolute path is that you don't have to worry about your current working directory when importing data.

Extra commas at end of lines causing error with read.csv and read.table

You are correct in your assessment that the trailing "," is causing the issues. To be precise, it's the fact that you have a trailing "," in the data lines but not in the line where the column names are declared.

If you don't want to manually fix the issue like you do in your code above, you could use readr::read_csv

library(tidyverse);
df <- read_csv("pubmed_result.csv");
df;
## A tibble: 375 x 11
# Title URL Description Details ShortDetails Resource Type Identifiers
# <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
# 1 Myoedi… /pubm… Zhang Y, Lo… Physiol… Physiol Rev… PubMed cita… PMID:29717…
# 2 Cullin… /pubm… Papizan JB,… J Biol … J Biol Chem… PubMed cita… PMID:29653…
# 3 Fusoge… /pubm… Bi P, McAna… Proc Na… Proc Natl A… PubMed cita… PMID:29581…
# 4 Correc… /pubm… Long C, Li … Sci Adv… Sci Adv. 2… PubMed cita… PMID:29404…
# 5 Single… /pubm… Amoasii L, … Sci Tra… Sci Transl … PubMed cita… PMID:29187…
# 6 Requir… /pubm… Shi J, Bi P… Proc Na… Proc Natl A… PubMed cita… PMID:29078…
# 7 Consid… /pubm… Carroll KJ,… Circ Re… Circ Res. … PubMed cita… PMID:29074…
# 8 ZNF281… /pubm… Zhou H, Mor… Genes D… Genes Dev. … PubMed cita… PMID:28982…
# 9 Functi… /pubm… Kyrychenko … JCI Ins… JCI Insight… PubMed cita… PMID:28931…
#10 Defici… /pubm… Papizan JB,… J Clin … J Clin Inve… PubMed cita… PMID:28872…
## ... with 365 more rows, and 3 more variables: Db <chr>, EntrezUID <int>,
## Properties <chr>

This will throw a bunch of warnings which originate from the missing/additional trailing ",", which you can ignore in this case. Note that column names are correctly assigned.

csv file containing 2 rows and 2 columns is not rendered in R markdown

I think the problem is the use of select:

data <- select(read.csv("test1.csv", stringsAsFactors = F))
data
> data
data frame with 0 columns and 1 row

If your remove select:

data <- read.csv("test1.csv", stringsAsFactors = F)
data

> data
column1 coumn2
1 value1 value2


Related Topics



Leave a reply



Submit