R语言 以 Rmd 格式保存脚本



我有R脚本。

mydat=read.csv("C:/Users/Admin/Downloads/test.csv", sep=";",dec=",")
View(mydat)
str(mydat)
#deleted after FS
mydat$symboling.<-NULL
mydat$make.<-NULL
mydat$num.of.cylinders.<-NULL
mydat$fuel.type.<-NULL
mydat$aspiration.<-NULL
mydat$num.of.cylinders.<-NULL
#this vars have  small num. of obs.
mydat$engine.type.<-NULL
mydat$engine.location.<-NULL
mydat$num.of.doors.<-NULL 
mydat=na.omit(mydat)
#Feature Selection
FS=Boruta(normalized.losses.~.,data=mydat)
getSelectedAttributes(FS, withTentative = F)
plot(FS, cex.axis=0.5)
#get scatterplot
scatter.smooth(x=mydat$length.,y=mydat$normalized.losses.,main="normalized losse~length")

#split sample on train and sample
index <- sample(1:nrow(mydat),round(0.70*nrow(mydat)))
train <- mydat[index,]
test <- mydat[-index,]

我必须将其保存为Rmarkdown格式(html)。 当然,在Rstudio中,我可以做到这一点: file-new file-rmarkdown-HTML

我得到这个脚本

```{r cars}
summary(cars)
```

我不想手动编写此前缀"{r}"。 是否可以使代码中由注释分隔的那些部分

#
#

以降价格式保存? 例如,在输出中,我期望

```{r}
mydat$symboling.<-NULL
mydat$make.<-NULL
mydat$num.of.cylinders.<-NULL
mydat$fuel.type.<-NULL
mydat$aspiration.<-NULL
mydat$num.of.cylinders.<-NULL
```

您可以使用knitr包中的spin()函数。 它将生成一个.md文件(但您可以使用precious = TRUE参数保留中间.Rmd),使用 '#' chracter 作为 'documentation 参数:

.doc用于标识文档行的正则表达式;默认情况下,它遵循 ROXY 约定,但可以自定义,例如,如果要使用 ## 来表示文档,可以使用 '^##\s*'。

例如:

spin('test.R', precious = TRUE, doc = '#')

生产:

测试。马币

```{r }
mydat=read.csv("C:/Users/Admin/Downloads/test.csv", sep=";",dec=",")
View(mydat)
str(mydat)
```
deleted after FS
```{r }
mydat$symboling.<-NULL
mydat$make.<-NULL
mydat$num.of.cylinders.<-NULL
mydat$fuel.type.<-NULL
mydat$aspiration.<-NULL
mydat$num.of.cylinders.<-NULL
```
this vars have  small num. of obs.
...

test.md

```r
mydat=read.csv("C:/Users/Admin/Downloads/test.csv", sep=";",dec=",")
```
```
## Warning in file(file, "rt"): cannot open file 'C:/Users/Admin/Downloads/
## test.csv': No such file or directory
```
```
## Error in file(file, "rt"): cannot open the connection
```
```r
View(mydat)
```
...

你也可以看看stitch()函数和兄弟姐妹(stitch_rhtmlstitch_rmd),看看这里

最新更新