创建R降价PDF报告时出错

时间:2016-09-05 06:14:29

标签: r r-markdown

从R markdown文件创建PDF报告时出错。以下是错误的片段:

   Error in --dayBikeData <- read.csv("D:\\Madhav\\Study\\MSIS\\PredictiveLearning\\Week-1\\Homework\\Bike-Sharing-Dataset\\day.csv") : 
  object 'dayBikeData' not found
Calls: <Anonymous> ... handle -> withCallingHandlers -> withVisible -> eval -> eval
Execution halted

我在会话中有这个对象-dayBikeData,但它仍然提供错误,不知道如何继续这个。

从csv文件中获取数据的代码:

```{r}

dayBikeData <- read.csv("D:\\Madhav\\Study\\MSIS\\PredictiveLearning
                        \\Week-1\\Homework\\Bike-Sharing-Dataset\\day.csv")

# Performs each of the operation asked in the question
basicOperations <- function(inputData){
  lenData <- length(inputData)
  avg <- round(mean(inputData, na.rm = TRUE), digits = 2) # mean calculation
  standardDeviation <- round(sd(inputData), digits = 2) # Standard deviation
  sem <- round(standardDeviation/sqrt(lenData), digits = 2)
  # Formula for CI is mean - error where error is 
  error = round(qnorm(0.975)*standardDeviation/sqrt(lenData), digits = 2)
  lower_ci <- avg - error
  upper_ci <- avg + error

  # resultList <- list(obs = lenData, mean = avg, standarDeviation = sd,
  #                    standardMeanError= sem, lowerCI = lower_ci, upperCI = upper_ci

  resultList <- c(lenData, avg, standardDeviation, sem,lower_ci,upper_ci)
  print(resultList)
}

#Calculations for the Year Wise Data
# dData2011 <- dayBikeData[dayBikeData$yr==0,]
# dData2012 <- dayBikeData[dayBikeData$yr==1,]
dData2011ResultSet <- basicOperations(dayBikeData[dayBikeData$yr==0,]$cnt)
dData2012ResultSet <- basicOperations(dayBikeData[dayBikeData$yr==1,]$cnt)

#Calculations for the Holiday Wise Data
# dDataHoliady_0 <- dayBikeData[dayBikeData$holiday ==0,]
# dDataHoliady_1 <- dayBikeData[dayBikeData$holiday ==1,]
dDataHoliady0ResultSet <- basicOperations(dayBikeData[dayBikeData$holiday ==0,]$cnt)
dDataHoliady1ResultSet <- basicOperations(dayBikeData[dayBikeData$holiday ==1,]$cnt)

#Calculations for the WorkingDay Wise Data

# dDataWorkingDay_0 <- dayBikeData[dayBikeData$workingday ==0,]
# dDataWorkingDay_1 <- dayBikeData[dayBikeData$workingday ==1,]
dDataWorkingDay0ResultSet <- basicOperations(dayBikeData[dayBikeData$workingday ==0,]$cnt)
dDataWorkingDay1ResultSet <- basicOperations(dayBikeData[dayBikeData$workingday ==1,]$cnt)


#Calculations for the Temperature wise data

avgTemp <- mean(dayBikeData$temp, na.rm = TRUE)
dDataTempGreaterEq  <- dayBikeData[dayBikeData$temp >= avgTemp,]
dDataTempLess <- dayBikeData[dayBikeData$temp < avgTemp,]
dDataTempGreaterEqResultSet <- basicOperations(dDataTempGreaterEq$cnt)
dDataTempLessResultSet <- basicOperations(dDataTempLess$cnt)

#Calculations for the Weather wise data
# dDataWeather_1 <- dayBikeData[dayBikeData$weathersit ==1,]
# dDataWeather_2 <- dayBikeData[dayBikeData$weathersit ==2,]
# dDataWeather_3 <- dayBikeData[dayBikeData$weathersit ==3,]
dDataWeather1ResultSet <- basicOperations(dayBikeData[dayBikeData$weathersit ==1,]$cnt)
dDataWeather2ResultSet <- basicOperations(dayBikeData[dayBikeData$weathersit ==2,]$cnt)
dDataWeather3ResultSet <- basicOperations(dayBikeData[dayBikeData$weathersit ==3,]$cnt)

#Calculations for the Season wise data
# dDataSeason_1 <- dayBikeData[dayBikeData$season ==1,]
# dDataSeason_2 <- dayBikeData[dayBikeData$season ==2,]
# dDataSeason_3 <- dayBikeData[dayBikeData$season ==3,]
# dDataSeason_4 <- dayBikeData[dayBikeData$season ==4,]
dDataSeason1ResultSet <- basicOperations(dayBikeData[dayBikeData$season ==1,]$cnt)
dDataSeason2ResultSet <- basicOperations(dayBikeData[dayBikeData$season ==2,]$cnt)
dDataSeason3ResultSet <- basicOperations(dayBikeData[dayBikeData$season ==3,]$cnt)
dDataSeason4ResultSet <- basicOperations(dayBikeData[dayBikeData$season ==4,]$cnt)



#Constrcut a row wise data
resultData <- rbind(dData2011ResultSet, dData2012ResultSet, dDataHoliady0ResultSet,
                    dDataHoliady1ResultSet,dDataWorkingDay0ResultSet, 
                    dDataWorkingDay1ResultSet,dDataTempGreaterEqResultSet,
                    dDataTempLessResultSet, dDataWeather1ResultSet, 
                    dDataWeather2ResultSet, dDataWeather3ResultSet,dDataSeason1ResultSet, 
                    dDataSeason2ResultSet, dDataSeason3ResultSet,dDataSeason4ResultSet)
colnames(resultData) <- c("N","Mean","SD" , "SEM","Lower_CI", "UPPER_CI")


rownames(resultData) <- c("Year-0", "Year-1", "Holiday-0", "Holiday-1", "WorkingDay-0", 
                          "WorkingDay-1","Temperature >=","Temperature <", "Weather-1",
                          "Weather-2","Weather-3","Season-1","Season-2", "Season-3", 
                          "Season-4")

df.resultData <- as.data.frame(resultData)
df.resultData["Value"] <- NA
df.resultData$Value <- c(2011, 2012, 0,1, 0,1,1, 0, 1,2,3,1,2,3,4)

df.resultData = df.resultData[,c(7,1,2,3,4,5,6)]
library(knitr)
# print(xtable(df.resultData), type = "latex")
kable(df.resultData, format = "markdown")
write.csv(df.resultData, file = "D:\\X\\Study\\MSIS\\PredictiveLearning\\OutputResult.csv")

2 个答案:

答案 0 :(得分:2)

您的文件路径错误...中间有一个新行和大量空格。

> "D:\\Madhav\\Study\\MSIS\\PredictiveLearning
+                         \\Week-1\\Homework\\Bike-Sharing-Dataset\\day.csv"
[1] "D:\\Madhav\\Study\\MSIS\\PredictiveLearning\n                        \\Week-1\\Homework\\Bike-Sharing-Dataset\\day.csv"

因此文件无法正确读取,因此该对象在knitr会话中不可用。

答案 1 :(得分:0)

我从UCI机器学习库下载了您的数据集,将您的降价保存在新文件夹中,通过删除路径来调整文件名,运行它,并且工作正常。

所以我可能你的会话已损坏,或路径错误或其他什么。尝试我做的,它应该工作。

证明:

enter image description here