【问题标题】:Knit2html is not generating MD file, but referring me to rmarkdown::renderKnit2html 不是生成 MD 文件,而是指我 rmarkdown::render
【发布时间】:2016-07-04 00:23:18
【问题描述】:

我无法在 Stack-Overflow 或网络上找到与此相关的内容。

我收到此错误:

> library(knitr)
> knit2html("pa1_template.rmd")
Error in knit2html("pa1_template.rmd") : 
  It seems you should call rmarkdown::render() instead of knitr::knit2html() because pa1_template.rmd appears to be an R Markdown v2 document.

我刚刚使用 rmarkdown::render() 运行它,它创建了 HTML 文件。但是,我的任务要我通过 knit2html() 运行它并创建一个 md 文件。

当我通过 RStudio 的“Knit HTML”菜单选项运行 Rmd 文件时,它会很好地创建 HTML 文件。

任何指针表示赞赏。

这里是rmd文件的内容:

## Loading and preprocessing the data

Read the data file in.
```{r readfile}
steps<-read.csv("activity.csv",header=TRUE, sep=",")
steps_good<-subset(steps, !is.na(steps))

```

Sum the number of steps per day
```{r summarize/day}
steps_day<-aggregate(steps~date, data=steps_good, sum)
```

Create a histogram of the results
```{r histogram}
hist(steps_day$steps, main="Frequency of Steps/day", xlab="Steps/Day", border="blue", col="orange")
```

# What is the mean total number of steps taken per day?
Calculate the mean of the steps per day
```{r means_steps/day}
mean_steps<-mean(steps_day$steps)
mean_steps
```
Calculate the median of the steps per day
```{r median_steps/day}
med_steps<-median(steps_day$steps)
med_steps
```

#What is the average daily activity pattern?

Get the average steps per 5 minute interval
```{r avg_5_min}
step_5min<-aggregate(steps~interval, data=steps_good, mean)
```
Plot steps against time interval, averaged across all days
```{r plot_interval}
plot(step_5min$interval,step_5min$steps, type="l", main="steps per time interval",ylab="Steps",xlab="Interval")
```

On average, which interval during the day has the most steps.
```{r max_interval}
step_5min$interval[which.max(step_5min$steps)]
```

#Imputing missing values

How many NAs are there in the original table?
```{r NAs}
  steps_na<-which(is.na(steps))
  length(steps_na)
```
Merge 5 minute interval with original steps table
```{r merge}

  steps_filled<-merge(steps, step_5min,by="interval")
```

Replace NA values with mean of steps values for that time interval
```{r replace_na}
  steps_na<-which(is.na(steps_filled$steps.x))
  steps_filled$steps.x[steps_na]<-steps_filled$steps.y[steps_na]
```

Create a histogram of the results
```{r new_hist}
steps_day_new<-aggregate(steps.x~date, data=steps_filled, sum)
hist(steps_day_new$steps.x, main="Frequency of Steps/day", xlab="Steps/Day", border="blue", col="orange")
```

It looks like the imputing of NA values increases the middle bar (mean/median) height, but other bars seem unchanged.


Calculate the new mean of the steps per day
```{r new_means_steps/day}
mean_steps<-mean(steps_day_new$steps.x)
mean_steps
```
Calculate the new median of the steps per day
```{r new_median_steps/day}
med_steps<-median(steps_day_new$steps.x)
med_steps
```

It looks like the mean did not change, but the median took on the value of the mean, now that some non-integer values were plugged in. 


#Are there differences in activity patterns between weekdays and weekends?
Regenerate steps_filled, and flag whether a date is a weekend or a weekday.
Convert resulting column to factor.
```{r fill_weekdays}
  steps_filled<-merge(steps, step_5min,by="interval")
  steps_filled$steps.x[steps_na]<-steps_filled$steps.y[steps_na]
  steps_filled<-cbind(steps_filled, wkday=weekdays(as.Date(steps_filled$date)))
  steps_filled<-cbind(steps_filled,    day_type="", stringsAsFactors=FALSE)

  for(i in 1:nrow(steps_filled)){
    if(steps_filled$wkday[i] %in% c("Saturday","Sunday"))
      steps_filled$day_type[i]="Weekend"
    else
      steps_filled$day_type[i]="Weekday"
  }
  steps_filled$day_type<-as.factor(steps_filled$day_type)
```

Get average steps per interval and day_type
```{r plot_interva_day_type}
steps_interval_day<-aggregate(steps_filled$steps.x,by=list(steps_filled$interval,steps_filled$day_type),mean)
```

Plot the weekend and weekday results in a panel plot.
```{r day_type_plot}
weekday_intervals<-subset(steps_interval_day, steps_interval_day$Group.2=="Weekday",select=c("Group.1","x"))
weekend_intervals<-subset(steps_interval_day, steps_interval_day$Group.2=="Weekend",select=c("Group.1","x"))
par(mfrow=c(1,2))
plot(weekday_intervals$Group.1,weekday_intervals$x,type="l",xlim=c(0,2400), ylim=c(0,225),main="Weekdays",xlab="Intervals",ylab="Mean Steps/day")
plot(weekend_intervals$Group.1,weekend_intervals$x,type="l",xlim=c(0,2400), ylim=c(0,225),main="Weekends",xlab="Intervals",ylab="")

【问题讨论】:

    标签: r knitr r-markdown


    【解决方案1】:

    在 RStudio 中,您可以在 YAML 标头中添加 keep_md: true

    --- 
    title: "Untitled" 
    output: 
      html_document: 
        keep_md: true 
    ---
    

    使用此选项,您可以获得HTMLmd 文件。

    【讨论】:

    • 我将output: html_document: keep_md: true 全部放在一条线上,但它不喜欢那样。下次我会记住这一点。
    • 没错,缩进在 YAML 标头中非常重要。
    【解决方案2】:

    它使用 knit(),而不是 knit2html()

    【讨论】:

      【解决方案3】:

      试试这个:

      setwd("working_directory")
      library(knitr)
      knit("PA1_template.Rmd", output = NULL)
      

      添加output=NULL" 对我来说很关键。

      祝你好运!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-02-24
        • 2016-12-18
        • 2022-08-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-17
        • 1970-01-01
        相关资源
        最近更新 更多