简体   繁体   中英

Reactive qplot in shiny app not working

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: 在此处输入图片说明

and if I resize the window the output is cut off 在此处输入图片说明

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM