I am trying to display the result of Cramers's V in Shiny when a user selects their desired variables.
Based on the result, a reactive text output will be displayed.
I had it working ok, but I am having difficulty inserting a line break between the Cramer's V result and the resulting text output.
The code below actually produces the correct result but it also displays an "Error: argument 1 (type 'closure') cannot be handled by 'cat'" which seems to be coming from the output$results output (as it is displayed in h1).
My research indicates this might be due to the renderText command but the renderPrint causes other issues. Does anyone have advice? All I'm wanting to do is put in a line break...
library(shinydashboard)
library(shiny)
library(dplyr)
library(DT)
library(rcompanion)
df <- data.frame(ACCIDENT_MASTER_single)
Cat1.Variables <- c("LIGHT_CONDITION", "ATMOSPH_COND", "DAY_OF_WEEK", "SEVERITY", "ROAD_GEOMETRY")
Cat2.Variables <- c("LIGHT_CONDITION", "ATMOSPH_COND", "DAY_OF_WEEK", "SEVERITY", "ROAD_GEOMETRY")
ui <- fluidPage(
titlePanel("Calculate the strength of the relationship between categorical variables"),
sidebarLayout(
sidebarPanel(
selectInput("cat1", choices = Cat1.Variables, label = "Select a Categorical Variable:"),
selectInput("cat2", choices = Cat2.Variables, label = "Select a Categorical Variable:")
),
mainPanel(
textOutput("text1"),
h1(textOutput("results")),
htmlOutput("text")
)
)
)
server <- shinyServer(function(input, output) {
cramerdata <- reactive({
req(input$cat1, input$cat2)
df3 <- data.matrix(ACCIDENT_MASTER_single[c(input$cat1, input$cat2)])
df3
})
results <- reactive({
cramerV(cramerdata())
})
output$text1 <- renderText({
paste("You have selected variables:", input$cat1, "and", input$cat2)
})
output$results <- renderText({
x <- cramerV(cramerdata())
print(paste("Cramer's V equals:", x,
ifelse(x > 0.5, "There is a strong association between the selected variables",
ifelse(x > 0.3 && x <= 0.5, "There is a medium association between the selected variables",
ifelse(x > 0.1 && x <= 0.3, "There is a weak association between the selected variables",
"There is a very weak association between the selected variables")))))
output$text <- renderUI({
res <- paste("Cramer's V equals:", x)
res2 <- paste(ifelse(x > 0.5, "There is a strong association between the selected variables",
ifelse(x > 0.3 && x <= 0.5, "There is a medium association between the selected variables",
ifelse(x > 0.1 && x <= 0.3, "There is a weak association between the selected
variables", "There is a very weak association between the selected variables"))))
HTML(paste(res, res2, sep = '<br/>'))
})
})
})
shinyApp(ui, server)