闪亮的应用程序速度非常慢,并且出现RegData错误,代码中的什么可能会导致这种情况?

闪亮的应用程序速度非常慢,并且出现RegData错误,代码中的什么可能会导致这种情况?,r,shiny,R,Shiny,我尝试在shiny上运行仪表板,结果速度非常慢,运行时也出现错误,所以我怀疑应用程序可能崩溃了?知道代码中的什么可能会导致这种情况吗 我刚开始编写线性回归的代码,我不确定代码的有效性 绘图不会显示在仪表板上 谢谢你 # Define UI ---- ui <- fluidPage( titlePanel("AirBnb NYC"), sidebarLayout( sidebarPanel(

我尝试在shiny上运行仪表板,结果速度非常慢,运行时也出现错误,所以我怀疑应用程序可能崩溃了?知道代码中的什么可能会导致这种情况吗

我刚开始编写线性回归的代码,我不确定代码的有效性

绘图不会显示在仪表板上

谢谢你

    # Define UI ----
    ui <- fluidPage(
      titlePanel("AirBnb NYC"),
      sidebarLayout(  
      sidebarPanel( 
      
          fluidRow(
             column(3,
                   selectInput("select", h3("Which Neighbourhood group ?"), choices = 
    c("Brooklyn","Manhattan","Queens","Staten Island", "Bronx"))),
            column(3,
                   selectInput("select2", h3("which Neighbourhood ?"), choices = "")),
            column(3,
                   selectInput("select1", h3("Room Type"), choices = ""))),
          p("Select the inputs for the Dependent Variable"),
          selectInput(inputId = "DepVar", label = "Dependent Variables", multiple = FALSE, choices = 
    colnames(AB_NYC_2019)),
          p("Select the inputs for the Independent Variable"),
          selectInput(inputId = "IndVar", label = "Independent Variables", multiple = FALSE, 
                  choices = list( "price"))
    ),
      


      mainPanel( leafletOutput("map",width = "100%",height = "800"), 
                 fluidRow(column(width = 6, plotOutput("data")),
                          column(width = 6, plotOutput("plot"))),
                 verbatimTextOutput(outputId = "RegSum"),
                 verbatimTextOutput(outputId = "IndPrint"),
                 verbatimTextOutput(outputId = "DepPrint"))
    ))

试试这个

AB_NYC_2019 <- AB_NYC_2019[1:50,]

# Define UI ----
ui <- fluidPage(
  titlePanel("AirBnb NYC"),
  sidebarLayout(
    sidebarPanel(
      selectInput("select", h3("Which Neighbourhood group ?"), choices =
                    c("Brooklyn","Manhattan","Queens","Staten Island", "Bronx")),
      selectInput("select2", h3("which Neighbourhood ?"), choices = ""),
      selectInput("select1", h3("Room Type"), choices = ""),

      p("Select the inputs for the Dependent Variable"),
      selectInput(inputId = "DepVar", label = "Dependent Variables", multiple = FALSE, choices =
                    colnames(AB_NYC_2019)),
      p("Select the inputs for the Independent Variable"),
      selectInput(inputId = "IndVar", label = "Independent Variables", multiple = FALSE,
                  choices = list( "price"))
    ),

    mainPanel( leafletOutput("map",width = "100%",height = "800"),
               fluidRow(column(width = 6, plotOutput("data")),
                        column(width = 6, plotOutput("plot"))),
               verbatimTextOutput(outputId = "RegSum"),
               verbatimTextOutput(outputId = "IndPrint"),
               verbatimTextOutput(outputId = "DepPrint"))
  )
)


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

  #Define parameters of search
  observe({
    req(input$select)
    x <- AB_NYC_2019 %>% dplyr::filter(neighbourhood_group == input$select) %>% select(neighbourhood)
    updateSelectInput(session, "select2", "select your neighbourhood", choice = unique(x))
  })

  observe({
    req(input$select2)
    productdata <- AB_NYC_2019$room_type[AB_NYC_2019$neighbourhood == input$select2]
    updateSelectInput(session, "select1", "Which room type?", choices = unique(productdata))
  })



  #Create map
  color <- colorFactor(palette = c("red", "green", "blue", "purple", "yellow"),
                       unique(AB_NYC_2019$neighbourhood_group))

  filteredData <- reactive({
    req(input$select)
    filter(AB_NYC_2019, neighbourhood_group == input$select)})

  output$map <- renderLeaflet({
    req(filteredData())
    map <- leaflet(filteredData()) %>% addProviderTiles(providers$Esri.WorldTopoMap) %>%
      setView(lng = -73.98928, lat = 40.75042, zoom = 10)  %>%
      addCircleMarkers(
        lng=~longitude, # Longitude coordinates
        lat=~latitude, # Latitude coordinates
        stroke=TRUE, # Circle stroke
        weight = 0.1,
        radius = 0.5,
        fillOpacity=0.5,
        color=~color(neighbourhood_group),
        label = paste("Name:", filteredData()$name, 
                      "\nPrice:", filteredData()$price, 
                      "\nReviews:", filteredData()$number_of_reviews)
        ) %>%
      addLegend("bottomright", pal = color, values = ~neighbourhood_group,
                title = "Neighbourhood groups",
                opacity = 1
      )

  })

  #filter map
  observe({
    req(filteredData())
    
    labs <- lapply(seq(nrow(filteredData())), function(i) {
      paste0( '<p> Name: ', filteredData()[i, "name"], '<p></p>', 'Price: ', 
              filteredData()[i, "price"],'</p><p>', 'Reviews: ',
              filteredData()[i, "number_of_reviews"], '</p>' ) 
    })
    
    leafletProxy("map", data = filteredData()) %>%
      clearShapes() %>%
      addMarkers(~longitude, ~latitude,
                 label = lapply(labs, htmltools::HTML),
                 labelOptions = labelOptions(textsize = "12px"))
  })

  lm1 <- reactive({
    req(filteredData())
    lm(reformulate(input$IndVar, input$DepVar), data = filteredData())})
  output$DepPrint <- renderPrint({input$DepVar})
  output$IndPrint <- renderPrint({input$IndVar})
  output$RegSum <- renderPrint({
    req(lm1())
    summary(lm1())})

  #Get many plots
  output$data <- renderPlot({
    ggplot(AB_NYC_2019, aes(price)) +
      geom_histogram(bins = 30, aes(y = ..density..), fill = "purple") +
      geom_density(alpha = 0.2, fill = "purple") +
      theme_bw() +
      ggtitle("Distribution of price",
              subtitle = "The distribution is very skewed") +
      theme(axis.title = element_text(), axis.title.x = element_text()) +
      geom_vline(xintercept = round(mean(AB_NYC_2019$price), 2), size = 2, linetype = 3)
  })

  output$plot <- renderPlot({
    AB_NYC_2019 %>% filter(price >= mean(price)) %>% group_by(neighbourhood_group, room_type) %>%
      tally %>%
      ggplot(aes(reorder(neighbourhood_group,desc(n)), n, fill = room_type)) +
      theme_bw() +
      xlab(NULL) +
      ylab("Number of objects") +
      ggtitle("Number of above average price objects",
              subtitle = "Most of them are entire homes or apartments") +
      geom_bar(stat = "identity")
  })

}

shinyApp(ui, server)
AB_纽约大学2019%
clearShapes()%>%
添加标记(~经度,~纬度,
label=lappy(实验室,htmltools::HTML),
labelOptions=labelOptions(textsize=“12px”))
})

正在使用的绘图中的lm1
th
。我不知道那是什么。也许你的意思是
theme\u bw()
。此外,最好在数据帧中仅使用50个观察值来运行代码,因为添加数千个标记太慢。一旦将
RegData
替换为
filteredData()
th
在绘图中使用
theme\u bw()
,并在必要时使用
req()
,绘图工作正常。要测试它,请使用数据帧中的前50行或前100行。如果有必要,我不确定req()函数是什么?有关
req()
,请参阅此行现在不起作用:label=paste(“Name:,AB_NYC_2019$Name,
,“Price:,AB_NYC_2019$Price,
”你知道为什么吗?看起来地图过滤器不起作用了。您需要使用标签中
filteredData()
中的变量,而不是
AB_NYC_2019
中的变量。请查看更新的代码。修复了标签显示在多行上的问题。非常感谢。我想做一个交互式回归,但它不像我做的那样有效
    shinyApp(ui = ui, server = server)
AB_NYC_2019 <- AB_NYC_2019[1:50,]

# Define UI ----
ui <- fluidPage(
  titlePanel("AirBnb NYC"),
  sidebarLayout(
    sidebarPanel(
      selectInput("select", h3("Which Neighbourhood group ?"), choices =
                    c("Brooklyn","Manhattan","Queens","Staten Island", "Bronx")),
      selectInput("select2", h3("which Neighbourhood ?"), choices = ""),
      selectInput("select1", h3("Room Type"), choices = ""),

      p("Select the inputs for the Dependent Variable"),
      selectInput(inputId = "DepVar", label = "Dependent Variables", multiple = FALSE, choices =
                    colnames(AB_NYC_2019)),
      p("Select the inputs for the Independent Variable"),
      selectInput(inputId = "IndVar", label = "Independent Variables", multiple = FALSE,
                  choices = list( "price"))
    ),

    mainPanel( leafletOutput("map",width = "100%",height = "800"),
               fluidRow(column(width = 6, plotOutput("data")),
                        column(width = 6, plotOutput("plot"))),
               verbatimTextOutput(outputId = "RegSum"),
               verbatimTextOutput(outputId = "IndPrint"),
               verbatimTextOutput(outputId = "DepPrint"))
  )
)


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

  #Define parameters of search
  observe({
    req(input$select)
    x <- AB_NYC_2019 %>% dplyr::filter(neighbourhood_group == input$select) %>% select(neighbourhood)
    updateSelectInput(session, "select2", "select your neighbourhood", choice = unique(x))
  })

  observe({
    req(input$select2)
    productdata <- AB_NYC_2019$room_type[AB_NYC_2019$neighbourhood == input$select2]
    updateSelectInput(session, "select1", "Which room type?", choices = unique(productdata))
  })



  #Create map
  color <- colorFactor(palette = c("red", "green", "blue", "purple", "yellow"),
                       unique(AB_NYC_2019$neighbourhood_group))

  filteredData <- reactive({
    req(input$select)
    filter(AB_NYC_2019, neighbourhood_group == input$select)})

  output$map <- renderLeaflet({
    req(filteredData())
    map <- leaflet(filteredData()) %>% addProviderTiles(providers$Esri.WorldTopoMap) %>%
      setView(lng = -73.98928, lat = 40.75042, zoom = 10)  %>%
      addCircleMarkers(
        lng=~longitude, # Longitude coordinates
        lat=~latitude, # Latitude coordinates
        stroke=TRUE, # Circle stroke
        weight = 0.1,
        radius = 0.5,
        fillOpacity=0.5,
        color=~color(neighbourhood_group),
        label = paste("Name:", filteredData()$name, 
                      "\nPrice:", filteredData()$price, 
                      "\nReviews:", filteredData()$number_of_reviews)
        ) %>%
      addLegend("bottomright", pal = color, values = ~neighbourhood_group,
                title = "Neighbourhood groups",
                opacity = 1
      )

  })

  #filter map
  observe({
    req(filteredData())
    
    labs <- lapply(seq(nrow(filteredData())), function(i) {
      paste0( '<p> Name: ', filteredData()[i, "name"], '<p></p>', 'Price: ', 
              filteredData()[i, "price"],'</p><p>', 'Reviews: ',
              filteredData()[i, "number_of_reviews"], '</p>' ) 
    })
    
    leafletProxy("map", data = filteredData()) %>%
      clearShapes() %>%
      addMarkers(~longitude, ~latitude,
                 label = lapply(labs, htmltools::HTML),
                 labelOptions = labelOptions(textsize = "12px"))
  })

  lm1 <- reactive({
    req(filteredData())
    lm(reformulate(input$IndVar, input$DepVar), data = filteredData())})
  output$DepPrint <- renderPrint({input$DepVar})
  output$IndPrint <- renderPrint({input$IndVar})
  output$RegSum <- renderPrint({
    req(lm1())
    summary(lm1())})

  #Get many plots
  output$data <- renderPlot({
    ggplot(AB_NYC_2019, aes(price)) +
      geom_histogram(bins = 30, aes(y = ..density..), fill = "purple") +
      geom_density(alpha = 0.2, fill = "purple") +
      theme_bw() +
      ggtitle("Distribution of price",
              subtitle = "The distribution is very skewed") +
      theme(axis.title = element_text(), axis.title.x = element_text()) +
      geom_vline(xintercept = round(mean(AB_NYC_2019$price), 2), size = 2, linetype = 3)
  })

  output$plot <- renderPlot({
    AB_NYC_2019 %>% filter(price >= mean(price)) %>% group_by(neighbourhood_group, room_type) %>%
      tally %>%
      ggplot(aes(reorder(neighbourhood_group,desc(n)), n, fill = room_type)) +
      theme_bw() +
      xlab(NULL) +
      ylab("Number of objects") +
      ggtitle("Number of above average price objects",
              subtitle = "Most of them are entire homes or apartments") +
      geom_bar(stat = "identity")
  })

}

shinyApp(ui, server)