I created some tables in document “RExample.r” and they look fine.
When I pass them to RmarkDown ihave 3 results:
- Example_With_Out_Bucle_1: works fine.
- Example_With_Out_Bucle_2: dosent even show in the html.
- Example_With_Bucle: Shows only code.
Can you help me make example 3 work?
I created some sample tables, for me its important to execute the RmarkDown directly from the R document.
# 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")
The results of the tables are as in this image:
This is my code in 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_Ann")
# 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), "nn")
# 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("nn") # Space between divisions
}
#```
And this are my results:
As you can see, Only the chunk with out loop and with out design are working.
Other interesting fact is that in Viewer the tables show with the propper design:
Can you please help me?