簡體   English   中英

如何根據計算結果在 Shiny 中顯示文本 output?

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

我正在使用 Shiny 並有三個目標:

a) 用戶可以從下拉菜單中選擇 select 變量

b) 計算 Cramer's V 並將結果顯示在屏幕上

c) 根據結果,顯示特定文本 output,例如“這是一個強關聯”

我已經成功地完成了目標 a 和 b。 我在目標三上嘗試了各種嘗試,但都無濟於事。

下面的這段代碼顯示了兩種不起作用的嘗試:

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)

我在 output$results 下得到的 output 如下:

結果相等:Cramer V 0.605 [1]“所選變量之間存在強關聯”

我了解 output 與 [1] 和“”看起來像這樣,因為我使用的是 renderPrint。 但是,當我使用 renderText 時,我得到文本 output 產生正確的 output 但根本不顯示 Cramer's V 的結果(目標 b)。

您可以在我的代碼中看到我還嘗試將文本 output 分別放在 output$text2 下,但它會產生以下結果:

錯誤:未找到 object 'x'。

任何人都可以幫助解決這個問題嗎?

謝謝

您的第二次嘗試不起作用,因為變量 'x' 不是全局變量,它在output$results下定義,您無法在output$text2中訪問它。 我無法運行您的整個代碼,因為您沒有提供必要的數據,但我想這會為您完成這項工作:

# 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您已經將cramerV(cramerdata())放在results()反應元素中,那么為什么要在output$results中重寫它。
PSS:盡量不要為變量和函數使用相同的名稱(就像這里的results一樣作為反應元素和輸出)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM