Im doing survey reporting and I need to produce the same likert scale question analysis repeatedly. I am doing this using a Child Markdown turned into a function and pmap. The child markdown produces a frequency response table, and a plot with tool tips made using ggiraph.
The child markdown works as intended in a function with the method I am using, however it prints '[[i]]' Null at the bottom of the last tab.
I have not been able to trackdown. In the repex below, if you are to render this markdown and click on the "Plot" tab for question 2, this is where the NULL values print.
My code for the main markdown document is as follows, not all the packages included are needed for the repex but are all the packages I use in my actaul project.
```{r, echo = FALSE, warning = FALSE, message = FALSE}
library(tidyverse)
library(readxl)
library(kableExtra)
library(janitor)
library(xlsx)
data <- data.frame(question_1 = sample(c("Never",
"Rarely",
"Sometimes",
"Often",
"Always",
NA),
replace = TRUE,
size = 100),
question_2 = sample(c("Never",
"Rarely",
"Sometimes",
"Often",
"Always",
NA),
replace = TRUE,
size = 100))
questions_list <- data.frame(title = c("How often do you drink water",
"How often do you exercise"),
variable = c("question_1",
"question_2"),
variable_title = c("Question 1",
"Question 2"))
Child_function <- function(variable,
title,
variable_title){
child_env <- new.env()
child_env$variable <- variable
child_env$title <- title
child_env$variable_title <- variable_title
res <- knitr::knit_child(
"C:/PROJECT-CLONES/REPEX/Questions-Child.Rmd",
envir = child_env,
quiet = TRUE
)
cat(res, sep = "\n")
}
```
```{r, results = 'asis', echo = FALSE}
pmap(questions_list, Child_function)
```
The code here is the code for the Child Markdown that produces the question analysis.
---
output: html_document
---
```{r, echo = FALSE, warning = FALSE}
library(tidyverse)
library(readxl)
library(knitr)
library(kableExtra)
library(ggiraph)
data%>%
select(contains(variable)) -> data
names(data)[1] <- "variable"
```
## `r variable_title` {.tabset}
<h4>`r title`</h4>
### Frequency Table
```{r, echo = FALSE}
Response_Options <- data.frame(variable = c("Never",
"Rarely",
"Sometimes",
"Often",
"Always",
"No response"))
data%>%
mutate(variable = ifelse(is.na(variable),
"No response",
variable))%>%
group_by(variable)%>%
summarise(Count = n())%>%
ungroup()%>%
mutate(Percent = (Count/sum(Count)) * 100)%>%
full_join(Response_Options, by = "variable")%>%
mutate_at(vars(Count, Percent),
as.character)%>%
mutate(Count = ifelse(is.na(Count),
"0",
Count))%>%
mutate(Percent = ifelse(is.na(Percent),
"0",
Percent))%>%
mutate_at(vars(Count, Percent),
as.numeric)%>%
mutate(variable = factor(variable,
levels = c("Never",
"Rarely",
"Sometimes",
"Often",
"Always",
"No response")))%>%
arrange(variable) -> Frequency_table
Frequency_table%>%
mutate(Percent = round(Percent, digits = 1))%>%
kable(caption = "",
col.names = c("Response",
"Frequency",
"Percentage"))%>%
kable_styling(bootstrap = "bordered")
```
### Plot
```{r, echo = FALSE}
Frequency_table%>%
ggplot(mapping = aes(x = variable,
y = Percent))
geom_bar(stat = 'identity')
geom_bar_interactive(stat = "identity",
width = 1,
color = "black",
fill = "#EC833A",
aes(tooltip = paste0(variable,
": ",
as.character(round(Percent, digits = 1)),
"%")))
labs(title = "Response Distribution",
y = "Percent",
x = "")
theme_light()
theme(plot.title = element_text(hjust = 0),
axis.line = element_line(size = .2),
panel.grid = element_line(linetype = 2, color = "#6d6e71"),
panel.border = element_blank(),
plot.caption = element_text(color = "#6d6e71",
hjust = 0),
axis.text.x = element_text(angle = 45, vjust = 1, hjust=1)) -> Plot
Plot_Interact <- girafe(ggobj = Plot)
Plot_Interact
```
Apologies for the long repex. Ive been trying to figure this out all morning and any help is greatly appreciated. Ive tried commenting out all portions of code in the child markdown to see what might be generating the NULLs but I havent had any luck.
CodePudding user response:
You could make pmap
output invisible
:
```{r, results = 'asis', echo = FALSE}
invisible(pmap(questions_list, Child_function))
```