creating a pdf file in rstudio

I am trying to convert an assignment for school into a pdf file. I'm using the knit function and I get the following error message every time. Can someone please explain why and how to fix it? Thanks.

processing file: Week-1---Lab.spin.Rmd

Error in contrib.url():
! trying to use CRAN without setting a mirror
Backtrace:

  1. └─utils::install.packages("psych")
  2. └─utils::contrib.url(repos, "source")

Quitting from Week-1---Lab.spin.Rmd:2-19 [unnamed-chunk-1]
Execution halted

I don't use RMarkdown but as a guess, do you have {psych} installed—outside of your RMarkdown document.

Related to jrkrideau's response, please make sure that you don't have install.packages() in any of your code chunks.

If you could share what is in your R Markdown document, that would help us troubleshoot!

#Explanation of R Environment

#Set Working Directory
#Data Entry
setwd("C:/Users/polly/Downloads")
TVdat <- read.csv("TvHours2018.csv")

This is what is in the markdown document. I have the install.packages() there because that was part of the instructions for the assignment. Maybe there is a different way? This is my first semester ever using R.

#Describe data
install.packages("psych")
library(psych)
describe(TVdat)

Yup, remove install.packages("psych") from your R Markdown file and keep library(psych). This will fix your issue!

You only need to install a package once on your computer. When you install packages, you should do so directly in the R console, not inside an R Markdown file. You only ever have to do this once for each package (until you upgrade R to a major version, like 4.2 -> 4.3, because that creates a new folder for all your packages). Once the package is installed, you can use the library() command inside any R Markdown file (or any other R script) to tell that specific session to load the package from your computer.

You're getting that error because you're trying to install a package from within the R Markdown document every time you knit it. Every time you knit an R Markdown session, R creates a new session just for that process. Because it's a fresh session, it doesn't know where to go to download new packages. The "mirror" is just the website or server it needs to download packages from, and since you haven't told it which one to use, it gets confused.

Hope that helps.

@ ivelasq3
Thanks for the explanation, I was pretty sure that that was a likely problem but not why.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.