0

I have a shinydashboard app where I want to be able to reactively subset data by multiple variables and then create a barplot of the resulting subset.

I've successfully created the reactive subsetting but the barplot shows, every time, that the y values are the same for every x values, even though the printed subset shows they have different values.

enter image description here

Relevant UI:

tabItem(tabName = "subset",
                            fluidRow(
                              box( uiOutput("codePanel")),
                              box(selectInput("groupDset", label="Select Category",
                                              choices = list("Evaluation"="evaluation",
                                                             "Incidence"="incidence",
                                                             "Involvement"="involve_idx",
                                                             "Funny" = "atts_raw_fun",
                                                             "Edgy" = "atts_raw_edgy",
                                                             "Disturbing" = "atts_raw_disturbing",
                                                             "Dramatic"= "atts_raw_dramatic",
                                                             "Mindless"= "atts_raw_mindless",
                                                             "Calming"= "atts_raw_calming",
                                                             "Intelligent"= "atts_raw_intelligent",
                                                             "Informative"= "atts_raw_informative",
                                                             "Scary"= "atts_raw_scary",
                                                             "Funny"= "atts_raw_funny",
                                                             "Relatable"= "atts_raw_relatable",
                                                             "Mean"= "atts_raw_mean",
                                                             "Size"="n_size"), selected = "evaluation")),
                              box(plotOutput(outputId = "bar1"))),

                            fluidRow(
                              box(width=NULL, DT::dataTableOutput("text"),style = "height:500px; overflow-y: scroll;overflow-x: scroll;"))
                              ),

This is the server side

filt <- selectInput("codeInput",label ="choose code",
                  choices = as.list(unique(data$show_name)))

 output$codePanel <- renderUI({ filt

})

dataset<-reactive({ 

 subset(data, show_name == input$codeInput)  

 })
  dataset2<-reactive({ 

subset(dataset(), select = c(input$groupDset,"grp_lev"))

  })


output$text<-renderDataTable(dataset())
output$text2<-renderDataTable(dataset2())

 #This plot seems to say that input$groupDset is a constant
output$bar1<-renderPlot({
ggplot(data=dataset2(), aes(x=grp_lev, y=input$groupDset, fill=grp_lev)) + geom_bar(stat = "identity")
}) 

When the plot is run from outside the shiny app with the same variables, but static, it works as expected and the y axis has tick marks. When called from the app, the y axis isn't articulated which makes me thing that input$groupDset must be the wrong data type. Running it y=as.numeric(input$groupDset) threw an error. Maybe because dataset2 is a closure function, the subsetting has to be formatted differently to slice it by reactive column value?

enter image description here

    ggplot(data=subset(data, show_name =='Show 1'), aes(x=grp_lev, y= incidence, fill=grp_lev)) + geom_bar(stat = "identity")
Heather
  • 129
  • 1
  • 11
  • If you use these functions outside the shiny app do they still show the same values for different inputs? If so, its likely an error in the shiny app code. If not, its likely an error with the bar plot function itself – morgan121 Nov 19 '18 at 06:04
  • When I call the barplot with static variables, outside of a shiny app, it works as expected. I think that `input$groupDset` must be the wrong data type but `aes(x=grp_lev, y=as.numeric(input$groupDset), fill=grp_lev)` didn't work – Heather Nov 19 '18 at 06:23
  • 1
    Maybe because input$groupDset is a character value ? You could try with aes_string(x="grp_lev", y=input$groupDset, fill="grp_lev")) ? – Bambs Nov 19 '18 at 14:42

0 Answers0