STAT547-UBC-2019-20 / group05

Repository for our team project (group 05)
0 stars 3 forks source link

Slider not working #52

Closed iciarfernandez closed 4 years ago

iciarfernandez commented 4 years ago

Hi again @yuliaUU, I posted a similar issue for my assignment 5. I still cannot figure out how to make my slider work, this is my code below. I dropped the "prefer not to say" value as I tried to address your feedback for Milestone 5, and then created a tibble. Other things I have tried besides the code below are:

I'm not quite sure of what's wrong still, as none of the above work, and the tibble I created looks fine, one column are the labels ("18-24", "25-34" etc) and the other are the values ("1, 2, 3...")

satisfaction_decision <- function(ageslider = "1"){

  p1 <- survey_data %>% 
    filter(age == ageslider) %>%
    ggplot(aes(y=satisfaction_decision, fill=satisfaction_decision)) + 
    geom_bar() +
    scale_fill_simpsons(alpha=0.6) +
    theme_minimal() +
    theme(panel.grid.major = element_blank(), 
          panel.grid.minor = element_blank(),
          legend.position = "none",
          axis.title.y = element_blank(),
          axis.title.x = element_blank(),
          plot.title = element_text(size = 16, hjust = 0.35)) + 
    ggtitle("Self-reported satisfaction with decision to pursue a PhD by age") 

  ggplotly(p1) 

}
# SLIDER
# quickly drop the 'prefer not to say'
x <- levels(survey_data$age)
is.na(x) <- x == "Prefer not to say"
x <- x[!is.na(x)]
ageKey <- tibble(label = x,
                 value = c(1, 2, 3, 4, 5, 6))
slider <- dccSlider(id='ageslider',
                    min=1,
                    max=length(ageKey),
                    marks = setNames(as.list(ageKey), 
                                     c(1:length(ageKey))),
                    value = "1"
)

Thank you so much, sorry, this issue is so annoying and I can't seem to figure it out.

yuliaUU commented 4 years ago

Put this code at teh beginning of your satisfaction_decision

sliderTibble<- tibble(label = levels(survey_data$age), value = c(1:length(levels(survey_data$age))))
slider_label <-sliderTibble[Cut_options$value ==ageslider]
p1 <- survey_data %>% 
    filter(age == slider_label ) %>%
    ggplot(aes(y=satisfaction_decision, fill=satisfaction_decision)) + 
    geom_bar() + .......
iciarfernandez commented 4 years ago

Do I leave my code for the slider as is or delete it? Besides adding that?

It gives me an error because "cut_options" doesn't exist

yuliaUU commented 4 years ago

for slider:

x <- levels(survey_data$age)
slider <- dccSlider(id='ageslider',
                    min=1,
                    max=length(ageKey),
                    marks = setNames(as.list(x), 
                                     c(1:length(x))),
                    value = "1"
)

Sorry, forgot to change names! corrected for function:

sliderTibble<- tibble(label = x, value = c(1:length(x)))
slider_label <-sliderTibble[sliderTibble$value ==ageslider]
p1 <- survey_data %>% 
    filter(age == slider_label ) %>%
    ggplot(aes(y=satisfaction_decision, fill=satisfaction_decision)) + 
    geom_bar() + .......
iciarfernandez commented 4 years ago

Thank you!! I think it must be almost there, it's giving me a different error now. I have attached a screenshot so it's easier to see, I think it has to do with the tibble?

Screenshot (8)

yuliaUU commented 4 years ago

can you check that x has only unique values?
and it would help if you can show me the output for x and sliderTibble

iciarfernandez commented 4 years ago

Yes, it does have unique values. Here are the outputs for both x and sliderTibble. I also tried writing x as unique(levels(survey_data$age)) to see if that would work, but it's still returning the same vector length error.

Screenshot (10)

yuliaUU commented 4 years ago

I think it complains when labels have spaces, can you try to modify x so theta all options have no space in them ( it is easy for first five categories, but for the last one may be used >65?) x<- c("18-24","25-34","35-44","45-54","55-64",">65") Can you try with this one?

iciarfernandez commented 4 years ago

I tried with that line but still no luck :( Just to make sure, that line should just be above the code for the slider object, right? Sorry that this is being such as hassle!!

yuliaUU commented 4 years ago

Yep, correct

I think I found the issue: slider_label <-sliderTibble$label[sliderTibble$value ==ageslider]

Missed $label

iciarfernandez commented 4 years ago

Yay!!! The slider now appears fine on the dash app, but for some reason the graph is not showing at all. I didn't change anything else in the code though :/ Thank you so much for your help, I suppose I'll try to figure it out tomorrow...

yuliaUU commented 4 years ago

Because we changed x, it cant find factors without spaces so we need to do sliderTibble<- tibble(label = levels(survey_data$age), value = c(1:length(levels(survey_data$age))))

iciarfernandez commented 4 years ago

Both the graph and the slider are showing now, but still not linked :/ As in, the graph doesn't change anything when I move the slider. I updated the code with all the changes under app.R in this repo

yuliaUU commented 4 years ago
data <- survey_data
  p <- ggplot(data, aes(x = satisfaction_decision, y = !!sym(yaxis))) +
    geom_jitter(alpha = 0.12,
                color = "#E6C350") +
    xlab("Satisfaction with decision to pursue a PhD") +
    ylab(y_label) +
    ggtitle(paste0("Self-reported satisfaction with decision to pursue a PhD vs ", y_label, " \n (1 being lowest rating)")) +
    theme_minimal() +
    theme(axis.text.x=element_text(angle = 30, hjust=1))

  # passing c("text") into tooltip only shows the contents of the "text" aesthetic specified above
  ggplotly(p)
}

# plot 2 
satisfaction_decision <- function(ageslider = "1"){
  sliderTibble <- tibble(label = levels(survey_data$age), value = c(1:length(levels(survey_data$age))))
  slider_label <- sliderTibble$label[sliderTibble$value == ageslider]
  p1 <- survey_data %>% 
    filter(age == slider_label) %>%
    ggplot(aes(y=satisfaction_decision, fill=satisfaction_decision)) + 
    geom_bar() +
    scale_fill_simpsons(alpha=0.6) +
    theme_minimal() +
    theme(panel.grid.major = element_blank(), 
          panel.grid.minor = element_blank(),
          legend.position = "none",
          axis.title.y = element_blank(),
          axis.title.x = element_blank(),
          plot.title = element_text(size = 16, hjust = 0.35)) + 
    ggtitle("Self-reported satisfaction with decision to pursue a PhD by age") 

  ggplotly(p1) 

}

you need to be careful with naming, satisfaction_decision: is a column in ggplot(data, aes(x = satisfaction_decision, y = !!sym(yaxis))) it is also name of teh function:satisfaction_decision <- function(ageslider = "1"){ and it is also an id:satisfaction_decision <- dccGraph( id = 'satisfaction_decision', figure = satisfaction_decision() # gets initial data using argument defaults )

maybe create a unique name for all of this items?

iciarfernandez commented 4 years ago

It worked!! I just changed satisfaction_decision <- function to graph2 instead. I had no idea that would cause so many issues, I'm so sorry it's taken so long! Thank you so much @yuliaUU :)

yuliaUU commented 4 years ago

ahha finally!!! no worries! so many details to keep in mind :/ so hard to lose track of what goes where