RMarkDown 不支持循环和颜色设计

问题描述 投票:0回答:1

我在文档“RExample.r”中创建了一些表格,它们看起来不错。 当我将它们传递给 RmarkDown 时,我得到了 3 个结果:

  1. Example_With_Out_Bucle_1:工作正常。
  2. Example_With_Out_Bucle_2:甚至没有在 html 中显示。
  3. Example_With_Bucle:仅显示代码。

你能帮我让示例 3 工作吗?

我创建了一些示例表,对我来说直接从 R 文档执行 RmarkDown 很重要。

# example_script.R

# Create a list of example tables for different divisions
Lista_Tablas_10Semanas_Por_Division <- list()

# Create mock tables for 3 divisions with Weeks as columns and Sales, Orders, Items as rows
Lista_Tablas_10Semanas_Por_Division[["Division_A"]] <- data.frame(
  Metric = c("Sales", "Orders", "Items"),
  W202401 = c(4000, 150, 250),
  W202402 = c(4500, 200, 300),
  W202403 = c(3000, 120, 180),
  W202404 = c(5000, 250, 350),
  W202405 = c(3500, 180, 220)
)

Lista_Tablas_10Semanas_Por_Division[["Division_B"]] <- data.frame(
  Metric = c("Sales", "Orders", "Items"),
  W202401 = c(4200, 130, 240),
  W202402 = c(4700, 170, 290),
  W202403 = c(3300, 100, 190),
  W202404 = c(5200, 260, 340),
  W202405 = c(3700, 190, 210)
)

Lista_Tablas_10Semanas_Por_Division[["Division_C"]] <- data.frame(
  Metric = c("Sales", "Orders", "Items"),
  W202401 = c(3800, 160, 230),
  W202402 = c(4400, 180, 290),
  W202403 = c(3100, 140, 170),
  W202404 = c(5100, 230, 340),
  W202405 = c(3600, 200, 250)
)

# Check if the list is created properly
print(names(Lista_Tablas_10Semanas_Por_Division))

library(rmarkdown)
getwd()
setwd("C:\\Users\\fvarelaa\\Desktop")
getwd()
archivo_rmd <- "C:\\Users\\fvarelaa\\Desktop\\RMarkDownExample.Rmd"
# Ejecutar el archivo RMarkdown y generar el reporte en HTML
render(archivo_rmd, output_format = "html_document", output_file = "Example.html")

表格的结果如下图所示: Result in Console

这是我在 RmarkDown 中的代码:

---
title: "Example Report"
author: "Your Name"
date: "`r Sys.Date()`"
output: 
  html_document:
    df_print: paged
---

#```{r setup1, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, message = FALSE, warning = FALSE)
library(kableExtra)
library(plotly)
library(ggplot2)
library(scales)

# Variables de colores
purple_color <- "#4B0082"
white_color <- "#FFFFFF"
#```


#```{r Example_With_Out_Bucle_1}  
  kable(Lista_Tablas_10Semanas_Por_Division$Division_A) %>%
  kable_styling(bootstrap_options = c("striped", "hover", "condensed"))
#```


#```{r Example_With_Out_Bucle_2}

# Display the title for Division A
#cat("### Division: Division_A\n\n")

# Get the table for Division_A
table <- Lista_Tablas_10Semanas_Por_Division$Division_A

# Check if the table exists and print it using kable
if (!is.null(table)) {
  print(
    kable(table, format = "html", table.attr = "style='width:80%;'") %>%
    kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive")) %>%
    row_spec(0, bold = TRUE, color = "white", background = "#4B0082")
  )
} else {
  cat("No data available for this table.\n")
}

#```


#```{r example_With_Bucle}  
# Loop through each Division in Lista_Tablas_10Semanas_Por_Division
for (division in names(Lista_Tablas_10Semanas_Por_Division)) {
  
  # Display the division title
  cat("### Division: ", gsub("_", " ", division), "\n\n")
  
  # Extract the table for the current division
  tabla_10semanas <- Lista_Tablas_10Semanas_Por_Division[[division]]
  
  # Render the table using kable and kableExtra
  print(
    kable(tabla_10semanas, format = "html", table.attr = "style='width:80%;'") %>%
      kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive")) %>%
      row_spec(0, bold = TRUE, color = "white", background = "#4B0082")
  )
  
  cat("\n\n")  # Space between divisions
}
#```

这是我的结果: Image of the HTML Result

如您所见,只有没有循环且没有设计的块才有效。 其他有趣的事实是,在查看器中,表格显示了正确的设计: Tables with design in viewer 你能帮我吗?

r r-markdown
1个回答
0
投票

要按原样写入块输出,请设置

results='asis'
选项。并将
print()
替换为
cat()

tmp.Rmd:

---
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)

library(kableExtra)
library(magrittr)
```

```{r example_With_Bucle, results='asis'}  
for (division in names(Lista_Tablas_10Semanas_Por_Division)) {
  
  # Display the division title
  cat("### Division: ", gsub("_", " ", division), "\n\n")
  
  # Extract the table for the current division
  tabla_10semanas <- Lista_Tablas_10Semanas_Por_Division[[division]]
  
  # Render the table using kable and kableExtra
  kable(tabla_10semanas, format = "html", table.attr = "style='width:80%;'") %>%
    kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive")) %>%
    row_spec(0, bold = TRUE, color = "white", background = "#4B0082") %>%
    cat(sep =  "\n\n")
}
```

准备 MRE 并渲染:

# list of frames for minimal reproducible example
Lista_Tablas_10Semanas_Por_Division <- split(iris, ~Species) |> lapply(head)
rmarkdown::render("tmp.Rmd")

渲染结果: Rendered rmd

© www.soinside.com 2019 - 2024. All rights reserved.