'Selecting columns using if else and pipes

I am new to R programming and I have encountered the following problem. The intention of my code is to retrieve either column 'vacancy' or 'vacancy2' based on an input y. However, when I tried to run the following, I received an error message that says vacancy does not exist.

y <-2 
data %>% 
    dplyr::select(vacancy2) %>% 
    {if (y==2) dplyr::select(vacancy) else .}

When I tried to do the following, there is no issue, I am able to retrieve just the column 'vacancy'.

data %>% dplyr::select(vacancy)

(I will need to use both if and pipes as I am building an R shiny app and requires the user's input to indicate the y-value and subsequently choose the columns.)

Could anyone please help to advise why the code above does not work? Many thanks in advance!

(edited to add the following): I have defined in the UI:

fluidRow(
    column(6,
           radioButtons(inputId = "yearAnalysis",
                        label = "Select comparison",
                        choices = c("Only 2021", "Last 3 years"),
                        selected = "Only 2021"
           ),
           selectInput(inputId = "yaxis",
                       label = "Select response variable",
                       choices = c("Vacancy", "Subscribed", "Taken"),
                       selected = "Vacancy")
    ),
    column(6,
           withSpinner(plotOutput(outputId = "BoxplotA")))
)

I have defined the following in server (I am not sure how to combine the 2 inputs so I've separated them):

filt_data <- reactive({
        data %>% 
        {if (input$yaxis == "Vacancy") {
            dplyr::select(Vacancy.2021,Vacancy.2020,Vacancy.2019)
        } else if (input$yaxis == "Subscribed"){
            dplyr::select(Subscribed.2021,Subscribed.2020,Subscribed.2019)
        } else {
            dplyr::select(Taken.2021,Taken.2020,Taken.2019)
        }}
    })
        

filt_data2 <- reactive({
    if (input$yearAnalysis == "Only 2021") {
        filt_data2 <- filt_data[1]
    } else {
        filt_data2 <- filt_data
    }
})

output$BoxplotA <- renderPlot({
    ggplot(stack(filt_data2), aes(x = ind, y = values)) +
        geom_boxplot()
    
})

Back to my initial post, I tried to use dplyr::select outside of R Shiny, but it didn't work too.



Solution 1:[1]

library(tidyverse)

# example data
data <-
  iris %>%
  head() %>%
  transmute(vacancy = Sepal.Length, vacancy2 = Sepal.Width) %>% 
  as_tibble()
data
#> # A tibble: 6 × 2
#>   vacancy vacancy2
#>     <dbl>    <dbl>
#> 1     5.1      3.5
#> 2     4.9      3  
#> 3     4.7      3.2
#> 4     4.6      3.1
#> 5     5        3.6
#> 6     5.4      3.9

y <- 2 
data %>% 
  transmute(
    vacancy = list(vacancy, vacancy2) %>% pmap_dbl(~ ifelse(y == 2, .x, .y))
  )
#> # A tibble: 6 × 1
#>   vacancy
#>     <dbl>
#> 1     5.1
#> 2     4.9
#> 3     4.7
#> 4     4.6
#> 5     5  
#> 6     5.4

y <- 1
data %>% 
  transmute(
    vacancy = list(vacancy, vacancy2) %>% pmap_dbl(~ ifelse(y == 2, .x, .y))
  )
#> # A tibble: 6 × 1
#>   vacancy
#>     <dbl>
#> 1     3.5
#> 2     3  
#> 3     3.2
#> 4     3.1
#> 5     3.6
#> 6     3.9

Created on 2022-03-16 by the reprex package (v2.0.0)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 danlooo