Home > database >  How do I display text output in Shiny depending on the result of a caluclation?
How do I display text output in Shiny depending on the result of a caluclation?

Time:09-23

I am using Shiny and have three goals:

a) user is able to select variables from a drop down menu

b) Cramer's V is calculated and the result is displayed on the screen

c) Depending on the result, a particular text output is displayed eg "this is a strong association"

I have successfully been able to complete goal a and b. I have tried various attempts at goal three but to no avail.

This one block of code below shows two attempts that do not work:

library(shinydashboard)
library(shiny)
library(dplyr)
library(DT)
library(rcompanion)

df <- data.frame(ACCIDENT_MASTER_single)

Cat1.Variables <- c("SEVERITY", "ATMOSPH_COND", "DAY_OF_WEEK", "CAT")
Cat2.Variables <- c("SEVERITY", "ATMOSPH_COND", "DAY_OF_WEEK", "CAT")

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"),
      h3(tableOutput("results")),
      textOutput("text2")
    )
  )
)


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, input$cat2) 
  })
  
  output$results <- renderPrint({
    cat(sprintf("\nThe results equal: \n"))
    x <- cramerV(cramerdata())
    print(x)
    
    if (x >.5) {
      return(paste("<span style=\"color:red\">There is a strong association between the selected variables </span>"))
    } else if
    (x > 0.3 && x <= 0.5) {
      "There is a medium association between the selected variables"
    } else if
    (x > 0.1 && x <= 0.3) {
      "There is a weak association between the selected variables"
    } else
      "There is a very weak association between the selected variables"
    
  })
  
    output$text2 <- renderText({ 
      
   
      if (x >.5) {
        return(paste("<span style=\"color:red\">There is a strong association between the selected variables </span>"))
      } else if
      (x > 0.3 && x <= 0.5) {
        "There is a medium association between the selected variables"
      } else if
      (x > 0.1 && x <= 0.3) {
        "There is a weak association between the selected variables"
      } else
        "There is a very weak association between the selected variables"

  })
})
shinyApp(ui, server)

The output I get under output$results are as follows:

The results equal: Cramer V 0.605 [1] "There is a strong association between the selected variables "

I understand the output looks like this with the [1] and the "" because I am using renderPrint. However, when I use renderText I get the text output producing the correct output but the result of Cramer's V is not displayed at all (goal b).

You can see in my code I have also tried to put the text output separately under output$text2 but it produces this result:

Error: object 'x' not found.

Can anyone please help solve this problem?

Thanks

CodePudding user response:

Your second attempt is not working because variable 'x' is not global, it's defined under output$results and you can't access it in output$text2. I cannot run your whole code since you didn't provide the necessary data but I guess this would do the job for you:

# in the ui change your tableOutput() to textOutput() for 'results':

    h3(textOutput("results"))

# in the server change your output$results to this:

output$results <- renderText({
    x <- cramerV(cramerdata())
    print(paste("The results equal:", 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")))))
  })

P.S. You had already put cramerV(cramerdata()) under the results() reactive element, so why are you not using that under the output$results.
P.S.S: try not to use the same name for variables and functions (like results here both as reactive element and output)

  • Related