在使用`.R`脚本的时候,可以使用`knitr::spin()`条件性地显示`.html`上的部分。

huangapple go评论56阅读模式
英文:

Conditionally display sections on .html using knitr::spin() on .R script

问题

I can help you with the translation of the code part. Here's the translation of the code you provided:

我正在使用`knitr::spin()`在一个.R脚本文件上呈现一个.html文件,并且我希望在其.html输出文件中有条件地显示部分内容。下面是我目前的代码示例。

```R
#'
#/*****************************************************************************/
#' **SECTION 0:** 这里有一些内容
#/*****************************************************************************/

# /*
x <- 2
# */

#'
#/*****************************************************************************/
#' **SECTION 1:** 这里有其他内容
#/*****************************************************************************/

# /*
y <- 5
# */

if(x==2 & y==1){
  
  #'
  #/*****************************************************************************/
  #' **SECTION 3:** 场景1
  #/*****************************************************************************/
  
} else if(x==2 & y==5){
  
  #'
  #/*****************************************************************************/
  #' **SECTION 3:** 场景2
  #/*****************************************************************************/
  
}


# /*
knitr::spin("spin_test.R", knit = TRUE)
# */

你可以告诉我需要做什么来帮助你修改脚本以获得期望的输出吗?

英文:

I'm rendering an .html file using knitr::spin() on a .R script file, and I want to conditionally display sections in its .html output file. I have some example code below of what I have so far.

#'
#/*****************************************************************************/
#' **SECTION 0:** Some stuff here
#/*****************************************************************************/

# /*
x <- 2
# */

#'
#/*****************************************************************************/
#' **SECTION 1:** Other stuff here
#/*****************************************************************************/

# /*
y <- 5
# */

if(x==2 & y==1){
  
  #'
  #/*****************************************************************************/
  #' **SECTION 3:** Scenario 1
  #/*****************************************************************************/
  
} else if(x==2 & y==5){
  
  #'
  #/*****************************************************************************/
  #' **SECTION 3:** Scenario 2
  #/*****************************************************************************/
  
}


# /*
knitr::spin("spin_test.R", knit = TRUE)
# */

How exactly can I modify my script above so that the output of spin_test.html looks like this:

SECTION 0: Some stuff here

SECTION 1: Other stuff here

SECTION 3: Scenario 2

As of now, my current output looks like this:

SECTION 0: Some stuff here

SECTION 1: Other stuff here
if(x==2 & y==1){
  
  #'
  #' **SECTION 3:** Scenario 1
  
} else if(x==2 & y==5){
  
  #'
  #' **SECTION 3:** Scenario 2
  
}
NULL

My goal is to get the appropriate conditional section in the .html output by solely using knitr:spin() on a .R script. Any advice or help would be greatly appreciated.

答案1

得分: 0

我能够按照这里的帖子1得到所需的输出。所有的功劳归功于Posit社区的用户MattP。

#'
#/*****************************************************************************/
#' **SECTION 0:** 这里有一些东西
#/*****************************************************************************/

# /*
x <- 2
# */

#'
#/*****************************************************************************/
#' **SECTION 1:** 这里有其他的东西
#/*****************************************************************************/

# /*
y <- 5
# */


#+ eval=(x==2 & y==1), echo=FALSE, results="asis"
glue::glue(sprintf("**SECTION 3:** 情景 1:x=%s 和 y=%s",x,y)) %>% 
  cat()

#+ eval=(x==2 & y==5), echo=FALSE, results="asis"
glue::glue(sprintf("**SECTION 3:** 情景 2:x=%s 和 y=%s",x,y)) %>% 
  cat()

# /*
knitr::spin("spin_test.R", knit = TRUE)
# */
英文:

I was able to get the desired the output by following the post here. All the credit to the user MattP from the Posit Community.

#'
#/*****************************************************************************/
#' **SECTION 0:** Some stuff here
#/*****************************************************************************/

# /*
x <- 2
# */

#'
#/*****************************************************************************/
#' **SECTION 1:** Other stuff here
#/*****************************************************************************/

# /*
y <- 5
# */


#+ eval=(x==2 & y==1), echo=FALSE, results="asis"
glue::glue(sprintf("**SECTION 3:** Scenario 1: x=%s and y=%s",x,y)) %>%
  cat()

#+ eval=(x==2 & y==5), echo=FALSE, results="asis"
glue::glue(sprintf("**SECTION 3:** Scenario 2: x=%s and y=%s",x,y)) %>%
  cat()

# /*
knitr::spin("spin_test.R", knit = TRUE)
# */

huangapple
  • 本文由 发表于 2023年6月6日 01:52:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76408866.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定