R Shiny 应用程序中的文本未正确显示(虽然没有显示代码错误,但它没有执行我编写的操作)

问题描述 投票:0回答:1

我正在构建一个简单的收入搜索工具,个人可以输入自己的职称、月工资和城市,作为输出,他们可以了解自己的工资与控制其工作的地理区域内其他个人的中位数工资相比如何标题。 打印数据

dput(df[1:10,c(1,2,3,4,5,6)]) 

输出:

structure(list(grp = c("Accountant", "Accountant", "Accountant", "Accountant", "Accountant", "Accountant", "Accountant", "Accountant", "Accountant", "Accountant"), monthly_income = c(8000, 2500, 8500, 4500, 35000, 5500, 10000, 7000, 12000, 4000), yrs_experience = c(5, NA, NA, NA, 5, NA, 0, 3, 3, NA), location = c("Portland", "Portland", "Seattle", "Portland", "Seattle", "Seattle", "Portland", "Portland", "Portland", "Seattle"), qualifications = c("no_qual_preference", "no_qual_preference", "no_qual_preference", "no_qual_preference", "BA", "no_qual_preference", "BA", "no_qual_preference", "BA", "no_qual_preference"), gender_preferences = c("no_gender_preference", "female", "female", "no_gender_preference", "no_gender_preference", "no_gender_preference", "no_gender_preference", "no_gender_preference", "no_gender_preference", "no_gender_preference")), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame")) 

这是当前的代码,运行良好,没有错误,但它没有显示我希望看到的每个职位的文本。例如,我想看到如下文本输出: “您的月薪高于您所在行业、与您拥有相同资质和所在城市的员工的收入中位数”

library(shiny)
library(readxl)
library(dplyr)


ui <- fluidPage(sidebarLayout(
  sidebarPanel(
    #textInput("grp", "Occupation"), # dropdown menu for job titles
    selectizeInput(
      "grp",
      "Occupation",
      sort(salary_test_data$grp), 
      choices = append("", sort(unique(
        salary_test_data$grp
      ))),
      selected = "",
      multiple = F
    ),
    #choices = sort(unique(salary_test_data$grp)),
    selectizeInput("qualifications",
                   "Qualifications",
                   choices = append("", sort(
                     unique(salary_test_data$qualifications)
                   ))),
    verbatimTextOutput("comparison_results"),
    sliderInput(
       "yrs_experience",
      "Years of Experience",
       min = 0,
       max = 9,
       value = 0,
       post = " Year(s)"
     ),
    selectizeInput("location",
                   "City",
                   choices = append("", sort(
                     unique(salary_test_data$location)
                   ))),
    numericInput("monthly_income", "Monthly Pay", value = 0),
    actionButton("compare_btn", "Compare Salary")
  ),

mainPanel(plotOutput("salary_plot")
)
))


server <- function(input, output) {
  observeEvent(input$compare_btn, {
    user_Occupation <- input$grp
    user_experience <- input$yrs_experience
    user_monthly_pay <- input$monthly_income
    user_location <- input$location
    user_qualification <- input$qualifications
    

    # data frame named "salary_test_data" with columns .....
    filtered_data <- salary_test_data[salary_test_data$grp == user_Occupation  & 
                                         salary_test_data$location == user_location  & 
                                         salary_test_data$qualifications == user_qualification & 
                                         salary_test_data$yrs_experience == user_experience, ] 


    if (nrow(filtered_data) > 0) {
      comparison_results <- ifelse(user_monthly_pay >= median(filtered_data$monthly_income),
                                   "Your monthly salary is above the median income of workers employed in your profession,
                                   who share your qualifications, & city in Saudi Arabia",
                                   "Your monthly salary is below the median income of workers employed in your profession, who share your qualifications, & city in Saudi Arabia")
    } else {
       comparison_results <- "No data found for the given job title and/or city"
     }
      })
      }
 shinyApp(ui, server)
r dplyr shiny shinydashboard
1个回答
0
投票

问题是,没有名称为

output
comparison_results
,只有在
observeEvent
内创建的具有该名称的变量。相反,您可以使用
eventReactive
renderText
创建
output$comparison_results
,然后将其显示在您的 UI 中:

server <- function(input, output) {
  comparison_results <-
    eventReactive(input$compare_btn, {
      user_Occupation <- input$grp
      user_experience <- input$yrs_experience
      user_monthly_pay <- input$monthly_income
      user_location <- input$location
      user_qualification <- input$qualifications

      filtered_data <- salary_test_data[salary_test_data$grp == user_Occupation &
        salary_test_data$location == user_location &
        salary_test_data$qualifications == user_qualification &
        salary_test_data$yrs_experience == user_experience, ]

      if (nrow(filtered_data) > 0) {
        if (user_monthly_pay >= median(filtered_data$monthly_income)) {
          "Your monthly salary is above the median income of workers employed in your profession,
        who share your qualifications, & city in Saudi Arabia"  
        } else {
          "Your monthly salary is below the median income of workers employed in your profession,
        who share your qualifications, & city in Saudi Arabia"
        }
      } else {
        "No data found for the given job title and/or city"
      }
    })

  output$comparison_results <- renderText({
    comparison_results()
  })
  
}

© www.soinside.com 2019 - 2024. All rights reserved.