I’m building a small shiny app to demo some causal inference concepts and I want to split all the independent variables from a linear regression formula and show them in multiple plots.
Strangely, when I look through the list of independent variables, shiny renders the last of them twice rather than rendering the first and then the second.
Here’s the UI part of my code:
ui <- fluidPage(
textInput("regression", "Regression formula", ""),
verbatimTextOutput("summary"),
plotOutput("plot1", width=fig.width, height=fig.height),
plotOutput("plot2", width=fig.width, height=fig.height),
plotOutput("plot3", width=fig.width, height=fig.height),
)
and here’s where I split the formula into multiple parts and try to render all the independent variables:
first_split <- unlist(strsplit(formula, "[~]"))
dependent <- str_trim(first_split[1])
independent <- unlist(lapply(strsplit(first_split[2], "[+]"), str_trim))
for( i in 1:length(independent))
{
whichplot <- paste("plot", toString(i), sep="")
print(whichplot) # this prints "plot1" and "plot2"
print(independent[i]) # this prints "prek" and "new_books"
output[[whichplot]] <- renderPlot({
ggplot(reading_scores, aes(x = .data[[dependent]])) +
geom_histogram(aes(color = as.factor(.data[[independent[i]]]), fill = as.factor(.data[[independent[i]]])),
position = "identity", bins = 30, alpha = 0.2) +
scale_color_manual(values = c("#00AFBB", "#E7B800")) +
scale_fill_manual(values = c("#00AFBB", "#E7B800"))
})
}
When I pass “scores ~ prek + new_books” as the formula, what should happen is that I get a hist of both ‘prek’ and ‘new_books’ but instead I just get ‘new_books’ twice. I suspect that shiny is re-rendering the plot but I can’t figure out why.
1