Closed iciarfernandez closed 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() + .......
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
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() + .......
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?
can you check that x
has only unique values?
and it would help if you can show me the output for x
and sliderTibble
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.
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?
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!!
Yep, correct
I think I found the issue:
slider_label <-sliderTibble$label[sliderTibble$value ==ageslider]
Missed $label
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...
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))))
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
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?
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 :)
ahha finally!!! no worries! so many details to keep in mind :/ so hard to lose track of what goes where
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:
Setting the value in the slider object as "18 - 24" rather than "1", and setting the ageslider also as "18 - 24" in the plot function
Listing the value as 1 without quotes in both the value in the slider object and the ageslider = in the plot function.
Instead of creating a tibble, just having a list and making the value in both the slider object & the ageslider in the plot function "18 - 24".
I have also tried specifying the value in the slider object as "1", both with and without quotes, and the ageslider value in the plot function as "18-24".
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...")
Thank you so much, sorry, this issue is so annoying and I can't seem to figure it out.