Hi! I'm trying to figure out why this code doesn't render properly in my Rmarkdown script, even though the report can be viewed and it creates the chart. I think the issue might be with the "params" in my Shiny code, but I'm not entirely sure. I've been trying to figure this out for two days, and I believe this might also be the reason why it's not working in the Shiny App.
First time in this forum and spanish native speaker, so let me know if I misunderstand myself.
Here is my shiny code:
library(shiny)
library(ggplot2)
library(dplyr)
library(DT)
library(rmarkdown)
ui <- fluidPage(
titlePanel("Tarea 3"),
tabsetPanel(
tabPanel("Carga de datos y descarga de reporte",
sidebarLayout(
sidebarPanel(
actionButton(inputId = "cargar_datos", label = "Cargar datos"),
downloadButton(outputId = "descargar_reporte", label = "Descargar Reporte")
),
mainPanel(
DTOutput("tabla_datos")
)
)),
tabPanel("Gráfico",
plotOutput("grafico_tendencia"))
)
)
server <- function(input, output, session) {
datos <- reactiveVal(NULL)
observeEvent(input$cargar_datos, {
df <- read.csv("T3_Datos.csv", sep = ";", stringsAsFactors = FALSE, header = TRUE)
df$Fecha <- as.Date(df$Fecha, format = "%d-%m-%Y")
df <- df %>% filter(!is.na(precio_cobre_centavosUSD))
datos(df)
})
output$tabla_datos <- renderDT({
req(datos())
datatable(datos(), options = list(pageLength = 10))
})
output$grafico_tendencia <- renderPlot({
req(datos())
ggplot(datos(), aes(x = Fecha, y = precio_cobre_centavosUSD)) +
geom_line() +
labs(x = "Fecha",
y = "Precio en Centavos (USD)"
) +
theme_minimal()
})
output$descargar_reporte <- downloadHandler(
filename = function() {
"reporte.html"
},
content = function(file) {
tempReport <- file.path(tempdir(), "RT3.Rmd")
file.copy("RT3.Rmd", tempReport, overwrite = TRUE)
df <- datos()
params <- list(datos = df)
rmarkdown::render(tempReport, output_file = file, params = params, envir = new.env(parent = globalenv()))
}
)
}
shinyApp(ui = ui, server = server)
````Preformatted text`