Skip to content

Commit 51d71b5

Browse files
committed
return elements as rmarkdown
1 parent 3646524 commit 51d71b5

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

R/tm_a_regression.R

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,10 +1036,13 @@ srv_a_regression <- function(id,
10361036
card_fun <- reactive({
10371037
req(plot_r(), source_code_r())
10381038
teal.reporter::report_document(
1039+
10391040
"## Plot",
1040-
plot_r(),
1041+
plot_to_base64(plot_r()),
10411042
"## Source Code",
1042-
source_code_r()
1043+
format_as_markdown_chunk(source_code_r()),
1044+
"## Table for testing",
1045+
knitr::kable(head(iris))
10431046
)
10441047
})
10451048
###
@@ -1049,3 +1052,33 @@ srv_a_regression <- function(id,
10491052
)
10501053
})
10511054
}
1055+
1056+
1057+
plot_to_base64 <- function(plot, width = 5, height = 4, dpi = 100) {
1058+
# Temporary file to save the plot
1059+
tmpfile <- tempfile(fileext = ".png")
1060+
1061+
# Save the plot as a PNG file
1062+
ggsave(tmpfile, plot = plot, width = width, height = height, dpi = dpi)
1063+
1064+
# Read the binary data and encode as base64
1065+
# base64enc::base64encode(tmpfile)
1066+
base64_string<- knitr::image_uri(tmpfile)
1067+
sprintf("![Plot](%s)", base64_string)
1068+
}
1069+
#
1070+
# plot_to_base64(qqplot(x = 1:2, y = 1:2))
1071+
1072+
format_as_markdown_chunk <- function(r_code) {
1073+
# Wrap the code inside a Markdown R chunk
1074+
sprintf("```{r}\n%s\n```", r_code)
1075+
}
1076+
#
1077+
# # Example usage:
1078+
# r_code <- "ggplot(mtcars, aes(x = wt, y = mpg)) +\n geom_point() +\n ggtitle('Scatter plot')"
1079+
#
1080+
# markdown_chunk <- format_as_markdown_chunk(r_code)
1081+
#
1082+
# # Print the Markdown-formatted output
1083+
# cat(markdown_chunk)
1084+

0 commit comments

Comments
 (0)