2

I'm trying to create an interactive shiny app in R that changes when you select a different "assignment group". The plot is a density chart that tracks the resolution time of a case (works fine outside of the app).

Why does the density plot not show up when I run the app?

I am not sure where the problem is, but when I try to run the app, the plot never renders and the select on the side panel shows multiple entries for the same group (I just want one for each group)...and R crashes half the time.

The app won't crash if I use a smaller data set (1000 rows, 3 columns), but is there a way for me to improve the code so that it runs with my dataset(about 50,000 rows and 3 columns)?

Unfortunately, I cannot share the actual data, but here's mock data I generated and tested--same issues only R doesn't crash because it's a smaller data set. The code below will run, but in the app, I only see the key--no chart, and the scaling does not refit when you resize the window;

Here's the code;

library(shiny)
library(ggplot2)

trax_cond <- data.frame(
    id = 1:6, 
    Assigned_Group3 = c("District of Columbia", "Ohio", "Oklahoma", "Tennessee", "Tennessee", "New York"),
    Total.Time.Days = c(5.9, 9.1, 7.8, 3, 8.3, 12.2)
)
#check variable class in each column
sapply(trax_cond, class)
trax_cond$Assigned_Group3 <- as.factor(trax_cond$Assigned_Group3)
trax_list <- as.list(trax_cond)

ui <- fluidPage(
  sidebarPanel(
  selectInput(inputId = "Assigned_Group3", label = "Assigned Groups:", choices = trax_list$Assigned_Group3),
  mainPanel(
    tags$h1("Density Plot"),
    plotOutput("P1")
   )
  )
)

###
server <- function(input, output){

  selectedData <- reactive({
    trax_cond[,trax_cond$Assigned_Group3]
  })


  output$P1 <- renderPlot({
    library(ggplot2)
    qplot(Total.Time.Days, data=trax_cond, geom="density", fill=Assigned_Group3, alpha=I(.5), 
           main="Distribution Days to Resolve", xlab="Resolution Time (Days)", 
           ylab="Density")
  })
}

shinyApp(ui,server)

This produces: enter image description here

and if I resize the window the output is cut off enter image description here

How can I fix the issues, and make sure the output resizes when you resize the window?

ellie
  • 45
  • 1
  • 5
  • When asking for help you should include a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input data. It doesn't have to be your real data. Feel free to use a built in dataset for example. But right now i see too many problems to easily fix without something to test with. Have you watched [shiny tutorials](https://www.rstudio.com/resources/webinars/shiny-developer-conference/) yet? I think that might clear up a lot of confusion. – MrFlick Sep 20 '17 at 20:19
  • @MrFlik Thanks for the advice--I'll add some sample data in a second – ellie Sep 20 '17 at 20:22
  • I've edited the question so it should be easier to replicate. Please let me know if more detail is needed. – ellie Sep 20 '17 at 21:02

1 Answers1

0

Without being able to test the code, One error that I see is within the server code:

selectedData <- reactive({
trax_cond[trax_cond$Assigned_Group3]
})

This should be:

selectedData <- reactive({
trax_cond[,input$Assigned_Group3]
})

Hope this helps.

Edit:

See below for code with a few minor edits to yours:

When you pass a user specified input from the ui to the server, you access that input using input$inputId. As seen with input$Assigned_Group3. With reactive data, you refer to the reactive data in the qplot command as selectedData().

library(shiny)
library(ggplot2)

trax_cond <- data.frame(
  id = 1:21, 
  Assigned_Group3 = c("District of Columbia", "Ohio", "Oklahoma", 
"Tennessee", "Tennessee", "New York"),
Total.Time.Days = c(5.9, 9.1, 7.8, 3, 8.3, 12.2)
)
#check variable class in each column
sapply(trax_cond, class)
trax_cond$Assigned_Group3 <- as.factor(trax_cond$Assigned_Group3)
trax_list <- as.list(trax_cond)

ui <- fluidPage(
  sidebarPanel(
    selectInput(inputId = "Assigned_Group3", label = "Assigned Groups:", 
  choices = trax_list$Assigned_Group3),
   mainPanel(
     tags$h1("Density Plot"),
      plotOutput("P1")
    )
  )
)

###
server <- function(input, output){

  selectedData <- reactive({
    trax_cond[trax_cond$Assigned_Group3 == input$Assigned_Group3,]
  })


  output$P1 <- renderPlot({
    library(ggplot2)
    qplot(Total.Time.Days, data=selectedData(), geom="density", alpha=I(.5), 
          main="Distribution Days to Resolve", xlab="Resolution Time 
               (Days)", 
          ylab="Density")
  })
}

shinyApp(ui,server)
Jorge
  • 392
  • 3
  • 14
  • thanks for the comment. I've made this change to the code above, but the same problem still occurs. – ellie Sep 20 '17 at 21:23
  • @ellie problem that you defined is very vague, can you be more specific? – pogibas Sep 20 '17 at 21:24
  • @PgGibas sure, I will be more specific. I have added screenshots to the question, showing the issue. Basically, there is no plot, and the image does not resize. I cannot tell if the plot reacts to different input because there is no plot but seeing as the legend does not change, I do not think it does. Does this help? – ellie Sep 20 '17 at 23:03
  • thanks @Jorge for the explanation and the fix! This works great – ellie Sep 21 '17 at 21:19