簡體   English   中英

嘗試發布 R 筆記本並不斷收到相同的錯誤(錯誤在 contrib.url(repos, “source”) 嘗試使用 CRAN 而不設置鏡像

[英]Trying to publish an R notebook and keep getting the same error (Error in contrib.url(repos, “source”) trying to use CRAN without setting a mirror

我按照其他問題的建議將 OSX Yosemite 與 XQuartz 一起使用,並且我一直在嘗試發布筆記本,但每次都遇到相同的錯誤。 這是 .R 文件的樣子:

#' ---
#' title: "MLB Payroll Analysis"
#' author: "Steven Quartz Universe"
#' date: "21 March 2015"
#' output: pdf_document
#' ---

#loading the payroll data from the Python document
payroll <- read.table("~/Documents/payroll.txt", header=TRUE, quote="\"")

View(payroll)

summary(payroll)

bank <- payroll$PayrollMillions
wins <- payroll$X2014Wins

#loading the payroll data from the Python document
payroll <- read.table("~/Documents/payroll.txt", header=TRUE, quote="\"")

summary(payroll)

bank <- payroll$PayrollMillions
wins <- payroll$X2014Wins

#displaying the mean and sd of payroll and wins (out of 162, of course)
mean(bank)
sd(bank)
mean(wins)
sd(wins)

#setting a linear regression
reg <- lm(wins ~ bank)
summary(reg)
#the regression is valid to significance < .10 (p-value .05072),
#but the R-squared is only .1296, a weak correlation

#a means of comparing the histogram to a normal distribution
histNorm <- function(x, densCol = "darkblue"){
  m <- mean(x)
  std <- sqrt(var(x))
  h <- max(hist(x,plot=FALSE)$density)
  d <- dnorm(x, mean=m, sd=std)
  maxY <- max(h,d)
  hist(x, prob=TRUE,
       xlab="x", ylim=c(0, maxY),
       main="(Probability) Histogram with Normal Density")
  curve(dnorm(x, mean=m, sd=std),
        col=densCol, lwd=2, add=TRUE)
}

#showing the histogram with normal distribution line
histNorm(reg$residuals, "purple")

#QQplots and Shapiro-Wilk test
qqnorm(reg$residuals)
qqline(reg$residuals)
shapiro.test(reg$residuals)
#p-value is .383; this can be considered a normal distribution

plot(reg$fitted.values,reg$residuals)
abline(h = 0)
#variances are wide, but in a channel

install.packages("lmtest")
library(lmtest)
bptest(reg)
#p-value of .849 given; we can assume variances are constant throughout the     distribution

hats <- hatvalues(reg)

hatmu <- mean(hats)
hats[hats > 2 * hatmu]
#we get teams 14 and 19 with high leverage; the Dodgers and Yankees with their     astronomical payrolls

treg <- rstudent(reg)
n <- length(treg)
p <- reg$coefficients
df <- n - p - 1
alpha <- 0.05

#no bonferroni correction for outliers
crit <- qt(1 - alpha/2,df)
treg[abs(treg) > crit]
#no outliers are found

#with bonferroni correction
crit <- qt(1 - (alpha/2)/n,df)
treg[abs(treg) > crit]
#no outliers are found

#comparison of outlier tests
pvals <- pt(-abs(treg),df)*2
padjb <- p.adjust(pvals, method = "bonferroni")
padjf <- p.adjust(pvals, method = "fdr")
cbind(pvals,padjb,padjf)

當我點擊編譯筆記本時,這是輸出:

  |......................                                           |  33%
  ordinary text without R code

  |...........................................                      |  67%
label: unnamed-chunk-1


processing file: payroll.spin.Rmd

Quitting from lines 9-90 (payroll.spin.Rmd) 
Error in contrib.url(repos, "source") : 
  trying to use CRAN without setting a mirror
Calls: <Anonymous> ... withVisible -> eval -> eval -> install.packages ->     contrib.url

我已經查看了有關如何糾正此問題的其他問題,但無濟於事。 我已經完成了命令行修復,再次無濟於事。 有人可以指出我做錯了什么嗎? 謝謝。

install.packages("lmtest")

是這里的問題。 正如錯誤消息所暗示的那樣

Error in contrib.url(repos, "source") : 
  trying to use CRAN without setting a mirror

預計您提供指向該包的存儲庫的鏈接。 所以將其更改為(例如):

install.packages("lmtest", repos = "http://cran.us.r-project.org")

應該做的伎倆。 但是,正如 MrFlick 和 Ben Bolkers 在他們的評論中指出的那樣,可能應該在尚未安裝軟件包時完成。

我在 Knit HTML 發布時遇到了同樣的問題,我修改了文件的開頭,如下所示:

---
title: "dialectic"
author: "micah smith"
date: "3/4/2017"
output: html_document
---

```{r setup, include=FALSE}
chooseCRANmirror(graphics=FALSE, ind=1)
knitr::opts_chunk$set(echo = TRUE)

chooseCRANmirror(graphics=FALSE, ind=1)是修復它的行

chooseCRANmirror(graphics=FALSE, ind=1)
knitr::opts_chunk$set(echo = TRUE)

如果您已經安裝了該軟件包,請在塊的開頭寫下它。

如果您已經運行了 install.packages("___") 腳本,那么您可以在嘗試編寫 Markdown 文件時嘗試將該代碼塊設置為 eval = FALSE?

如果您通過pkgs變量安裝多個軟件包,請執行此操作。 它對我有用。 在我修復它之前,我無法 knitr 到 pdf。

pkgs <- c("moments", "ggplot2", "dplyr", "tidyr", "tidyverse")
install.packages(pkgs, repos = "http://cran.us.r-project.org")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM