Embed existing code into R Shiny

Hi, I am new to Shiny and have issues embedding my existing code into Shiny and make it responsive.

The intent of the code is to calculate by what factor salaries need to be increased to reduce a gender pay gap: Therefore, data is split based on the CompaRatio value defined in the code (value from 0-100). Then, TTC (salaries) of the dataset with low CompaRatio is increased by the increase factor initially defined. Based on this input, the "new" gender pay gaps are calculated using the Oaxaca package and the costs of the increase:

CompaThres <- 90
Increase <- 1.05

equity <- subset(data, data$Mean_CR < CompaThres & data$Gender_num == 1)
equity_inv <- subset(data, data$Gender_num == 0 | (data$Mean_CR >= CompaThres & data$Gender_num == 1))

equity$TTC_new <- equity$TTC * Increase
equity_inv$TTC_new <- equity_inv$TTC

Costs <- sum(equity$TTC_new - equity$TTC)
 
Equity_all <- rbind(equity, equity_inv)

Oaxaca_TTC_new <- oaxaca(formula = TTC_new ~ Personal_Pos_Grade.r + Achievement+ Age + ADM + BCPM + CO + CS + FI + GM + HR + IT + IR + LE + MK + OPS + OE + PR + RE + SA + WAF | Gender_oa , data = Equity_all, R = 1)

I would like to visualize this in Shiny with the user defining if salaries 1) from men, women, or all should be increased 2) below which CompaRatio value and 3) by what increase factor. The result should then display the new gender pay gap, costs of the increase and a graph in the main panel.

My question would be where I need to insert the code to make it work in Shiny and make it responsive. So far, I have the entire code before the Shiny part but Shiny cannot seem to work with objects defined prior. Thanks a lot in advance!

library(shiny)

# Define UI ----
ui <- fluidPage(
  titlePanel("Pay Equity Calculator"),
  
  sidebarLayout(
    sidebarPanel(
      helpText("Choose your options"),
      
      checkboxGroupInput("checkGroup", 
                         h3(strong("Scope")), 
                         choices = list("Female" = 1, 
                                        "Male" = 0),
                         selected = 1),
      
      sliderInput("range", 
                  label = h3(strong("CompaRatio Threshold")),
                  min = 0, max = 100, value = c(80)),
      
      sliderInput("increase", 
                  label = h3(strong("Increase in %")),
                  min = 0, max = 10, value = c(2))
    
    ),
  mainPanel(
    textOutput("range")
  )
  )
)
  

# Define server logic ----
server <- function(input, output) {
  output$range <- renderText({
    paste(input$range)
  })
}

# Run the app ----
shinyApp(ui = ui, server = server)

Hi there
I didn't get your question right. However, from my observations in your codes, you haven't used the variables you created before creating the app.
You need to ensure that you use them either in the UI, server, or both.
In the mean time you can share the data and will see how I can help out.

Thanks for your quick reply! My problem is exactly as you describe. I don't manage to use the variables in the app. This is how I would like to connect it in UI / Server:

  • The CompaThres value should be changed by sliderInput ("range") in shiny
  • The Increase value should be changed by sliderInput ("increase") in shiny
  • The subsets should be changed by checkboxGroupInput ("checkGroup") in shiny

Hope that clarifies. Will work on anonymizing my dataset and then upload. Thank you!

I find your code having a lot of errors even before the UI section

You are also trying to render an input instead of an output.

server <- function(input, output) {
    output$range <- renderText({
        paste(input$range)
    })
}
Created on 2022-01-14 by the reprex package (v2.0.1)

Managed to fix the code now so I get the correct values displayed for the costs and the gaps based on input values selected in the UI. What are you referring to when saying I have a couple of errors before the UI section?

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.