简体   繁体   English

如何根据计算结果在 Shiny 中显示文本 output?

[英]How do I display text output in Shiny depending on the result of a caluclation?

I am using Shiny and have three goals:我正在使用 Shiny 并有三个目标:

a) user is able to select variables from a drop down menu a) 用户可以从下拉菜单中选择 select 变量

b) Cramer's V is calculated and the result is displayed on the screen b) 计算 Cramer's V 并将结果显示在屏幕上

c) Depending on the result, a particular text output is displayed eg "this is a strong association" c) 根据结果,显示特定文本 output,例如“这是一个强关联”

I have successfully been able to complete goal a and b.我已经成功地完成了目标 a 和 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:我在 output$results 下得到的 output 如下:

The results equal: Cramer V 0.605 [1] "There is a strong association between the selected variables "结果相等:Cramer V 0.605 [1]“所选变量之间存在强关联”

I understand the output looks like this with the [1] and the "" because I am using renderPrint.我了解 output 与 [1] 和“”看起来像这样,因为我使用的是 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).但是,当我使用 renderText 时,我得到文本 output 产生正确的 output 但根本不显示 Cramer's V 的结果(目标 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:您可以在我的代码中看到我还尝试将文本 output 分别放在 output$text2 下,但它会产生以下结果:

Error: object 'x' not found.错误:未找到 object 'x'。

Can anyone please help solve this problem?任何人都可以帮助解决这个问题吗?

Thanks谢谢

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 .您的第二次尝试不起作用,因为变量 'x' 不是全局变量,它在output$results下定义,您无法在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")))))
  })

PS You had already put cramerV(cramerdata()) in the results() reactive element, so why are you rewriting that in the output$results . PS您已经将cramerV(cramerdata())放在results()反应元素中,那么为什么要在output$results中重写它。
PSS: try not to use the same name for variables and functions (like results here both as reactive element and output) PSS:尽量不要为变量和函数使用相同的名称(就像这里的results一样作为反应元素和输出)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM