Using bold text in a loop for a Word document in Rmarkdown

旧街凉风 提交于 2020-05-18 18:37:12

问题


I'm new to using Rmarkdown. I have a loop from which I extract a set of results for each iteration. This is fine. However, I am unable to get the text to print as bold. Here is some example code:

```{r echo=FALSE}
x <- data.frame(var1 = c(1,0.99,0.98,0.97,0.95), 
            var2 = c(1,0.995,0.99,0.98,0.97),
            var3 = c(1,0.98,0.96,0.94,0.90)
            )

year <- 1980:2010


# Do stuff across the loop:
for(j in 1:nrow(x)){
  temp_var1 <- constant1 * var1[j] * var2[j] * var3[j]
  temp_var2 <- constant2 * var1[j] * var2[j] * var3[j]


  q025 <- round(sapply(temp_var1[, ], quantile, probs=0.025))
  q975 <- round(sapply(temp_var2[, ], quantile, probs=0.975))
  proj <- as.data.frame(cbind(year, q025, q975))
  row.names(proj) <- NULL

  # Print stuff:
  **print(x[j,])**
  print(proj)
  cat('\n\n\n\n')   
} # close j loop
```

Thanks for any help.


回答1:


I'm not really sure what you want to do with the for-loop code but you can copy this short Rmarkdown snippet to your own new Rmarkdown document to render bold text from within a code chunk:

## Display some bold text from inside a chunk

```{r results='asis'}
cat("I am a normal statement")
cat("**some bold text**")
```

## Bold text with a for loop

```{r results='asis'}
for(i in 1:5){
  cat("Number: **", i, "**\n\n", sep = "")
}
```


来源:https://stackoverflow.com/questions/47416858/using-bold-text-in-a-loop-for-a-word-document-in-rmarkdown

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!