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

Text not displayed correctly in R Shiny app (although no code error is shown but it's not doing what I write)

提问人:maldini1990 提问时间:11/16/2023 最后编辑:maldini1990 更新时间:11/16/2023 访问量:31

问:

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

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(shinydashboard)
# library(shinyWidgets)
#library(readxl)


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) {
  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"  
        } else {
          "Your monthly salary is below the median income of workers employed in your profession,
        who share your qualifications, & city"
        }
      } else {
        "No data found for the given job title and/or city"
      }
    })
  
  output$comparison_results <- renderText({
    comparison_results()
  })
  
}
R dplyr 闪亮 仪表板

评论


答:

1赞 stefan 11/16/2023 #1

问题是没有 with name,只有一个在 中创建了具有该名称的变量。相反,您可以使用 和 a 来创建,然后它会显示在您的 UI 中:outputcomparison_resultsobserveEventeventReactiverenderTextoutput$comparison_results

salary_test_data <- 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(dplyr)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectizeInput(
        "grp",
        "Occupation",
        sort(salary_test_data$grp),
        choices = append("", sort(unique(
          salary_test_data$grp
        ))),
        selected = "",
        multiple = F
      ),
      selectizeInput("qualifications",
        "Qualifications",
        choices = append("", sort(
          unique(salary_test_data$qualifications)
        ))
      ),
      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(
      verbatimTextOutput("comparison_results")
    )
  )
)


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()
  })
}

shinyApp(ui, server)

enter image description here

评论

0赞 maldini1990 11/16/2023
感谢您的明确解释,但不幸的是,我仍然面临同样的问题。我尝试单独运行上面的确切代码,以及从以下位置开始运行整个代码:ui <- fluidPage(sidebarLayout....但是,我仍然没有得到任何文本输出。
1赞 stefan 11/16/2023
不好意思。应该提到我把 移到了 .按原样,它将显示在滑块输入正上方的侧边栏面板内。verbatimTextOutputmainPanel
0赞 maldini1990 11/16/2023
谢谢,我已经编辑了这篇文章以反映我认为是你的代码,但我不完全确定这是否是你运行的,因为不幸的是我仍然遇到同样的问题。
1赞 stefan 11/16/2023
我刚刚进行了包含UI代码的编辑,并检查了这将提供屏幕截图中显示的输出。