如何在传单弹出窗口中添加来自不同csv文件的文本数据?

时间:2018-06-01 06:49:12

标签: r leaflet plotly shinydashboard

每个人,我都是新手写的闪亮的应用程序和新的使用传单包。我正在尝试创建一个闪亮的应用程序,其中我坚持在两个不同的CSV文件的单张弹出窗口中显示文本数据。

我正在为墨尔本住房制作一个闪亮的应用程序。一世 我的传单地图为我提供了地图上带标记的输出。但是,当我点击这些标记时,我希望从两个不同的CSV文件中显示文本,这些文件是学校和移民数据。截至目前,当我点击标记时,它只显示学校的数据。

学校数据包含学校名称和学校排名 移民数据包含移民国家,百分比

以下是我的代码。

    library(shiny)
    library(shinydashboard)
    library(leaflet)
    library(dplyr)
    library(plotly)

    house <- read.csv("house.csv",header = T,stringsAsFactors = F)
    school <- read.csv("Melbourne School data.csv", header = T, stringsAsFactors = F)

    head(school)

    d <- ggplot(house, aes(x=house$Type, y=house$Price)) + geom_bar(stat = "identity")

    suburb_vs_price <- house[c("Suburb","Price")] %>% na.omit() #remove 2688 NA observations
    top10sub_by_averprice <- suburb_vs_price %>% group_by(Suburb) %>%
      summarise(Average = sum(Price)/n()) %>%
      arrange(desc(Average)) %>% head(10)


    house$Distance <- as.integer(house$Distance)

    ui <- shinyUI(dashboardPage(
      dashboardHeader(title = "Melbourne Housing"),
      dashboardSidebar(

        #mainPanel(
          # Use imageOutput to place the image on the page
        #  htmlOutput("picture")
        #),

        sidebarMenu(id='sidebar',
                    menuItem("Introduction", tabName = "introduction", icon = icon("dashboard")),
                    menuSubItem("Prices", tabName = "Prices", icon = icon("th")),
                    menuSubItem("HouseType", tabName = "HouseType", icon = icon("home")),
                    menuSubItem("Suburbs", tabName = "Suburbs", icon = icon("map-marker")),
                    menuSubItem("CBD", tabName = "CBD", icon = icon("align-justify")),
                    menuSubItem("Building", tabName = "Building", icon = icon("align-justify"))

        ), 
        conditionalPanel("input.tabs=='introduction'",
                         fluidRow()
        ),
        conditionalPanel("input.tabs=='CBD'",
                         fluidRow(tabItems(tabItem(tabName = "new", "New Test")))

        )),

      dashboardBody(

        tabItems(

          tabItem(tabName = "Prices", leafletOutput("myprice"),sliderInput("slider",h3("Price Slider"), round = TRUE, value = range(house$Price), min = min(house$Price), max = max(house$Price))),
          tabItem(tabName = "introduction", h4("Melbourne is the second most populous city in Australia and it is ranked the world's most liveable city. The housing market in Melbourne is 
                                               changing rapidly, before you think of moving to Melbourne, you should see this!"), 
                  br(), h4("In this webpage, I have created visualizations that provide different kinds of information about the housing market in Melbourne. The data for these visualizations were extracted from the dataset melbourne_housing_data, which contains 17.408 randomly sampled and geocoded house sales for the last 6 months in Melbourne."),
                  br(), h4("To see the first visualization scroll down or click on the next tab below")),
          tabItem(tabName = "CBD", leafletOutput("mydistance"), title = h3("Distance From CBD"), sliderInput("sliderDistance", "Distance Slider", value = range(house$Distance), min = min(house$Distance), max = max(house$Distance))),
          tabItem(tabName = "Building", h2("Building sizes"), leafletOutput("mybuilding")),
          tabItem(tabName = "Suburbs", h2("Suburb Data"), leafletOutput("mysuburb"), sliderInput("pslider",h3("Price Slider"), round = TRUE, value = range(house$Price), min = min(house$Price), max = max(house$Price)), sliderInput("sliderDist", "Distance Slider", value = range(house$Distance), min = min(house$Distance), max = max(house$Distance)),radioButtons("dist", "House type:",
                                                                                                                                                                                                                                                                                                                                  c("HOUSE, COTTAGE, VILLA, TERRACE" = "h",
                                                                                                                                                                                                                                                                                                                                    "UNIT, DUPLEX" = "u",
                                                                                                                                                                                                                                                                                                                                    "TOWN HOUSE" = "t")), selectInput('top10', 'TOp 10 Suburbs', c(Choose='', top10sub_by_averprice[1]), selectize=FALSE)),
          tabItem(tabName = "HouseType",h2("Housetype Category"),leafletOutput("myhousetypee"), radioButtons("dist1", "House type:",
                                                                                                            c("HOUSE, COTTAGE, VILLA, TERRACE" = "h",
                                                                                                              "UNIT, DUPLEX" = "u",
                                                                                                              "TOWN HOUSE" = "t")))
          )
      ) 
      ))

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


      src = "https://res.cloudinary.com/redballoon/c_fill,f_auto/v1523942375/NEW%20Subcat%20Hero%20Images/things-to-do-in-melbourne.jpg"
      output$picture<-renderText({c('<img src="',src,'">')}) 

      output$style_tag <- renderUI({
        if(input$sidebar=='introduction')
          return(tags$head(tags$style(HTML('.content-wrapper {background-color:PALEGOLDENROD;}'))))

     #   if(input$sidebar=='page2')
    #      return(tags$head(tags$style(HTML('.content-wrapper {background-color:red;}'))))
      })

      output$myprice <- renderLeaflet({ 
        temp <- house %>% filter(Price > input$slider[1] & Price < input$slider[2])
        temp <- na.omit(temp)

        center_lon = median(temp$Longtitude)
        center_lat = median(temp$Lattitude)

        leaflet(house$Suburb) %>% addTiles() %>%
          addCircles(lng = ~Longtitude, lat = ~Lattitude, data = temp, radius = 3, popup = paste("Price : ", as.character(temp$Price), "<br>",
                                                                                                                        "Suburb : ", as.character(temp$Suburb)),
                     color = c("red"))  %>%

          # controls
          setView(lng=center_lon, lat=center_lat, zoom=12)})

      output$mydistance <- renderLeaflet({ 
        temp <- house %>% filter(Distance > input$sliderDistance[1] & Distance < input$sliderDistance[2])
        temp <- na.omit(temp)
        center_lon = median(temp$Longtitude)
        center_lat = median(temp$Lattitude)

        leaflet(house$Distance) %>% addTiles() %>%
          addCircles(lng = ~Longtitude, lat = ~Lattitude, data = temp, layerId = temp$Distance, radius = 3, popup = paste0("Distance : ", as.character(temp$Distance), "<br>",
                                                                                                                          "Price : ", as.character(temp$Price), "<br>",
                                                                                                                          "Suburb : ", as.character(temp$Suburb)),
                     color = c("red"))  %>%

          # controls
          setView(lng=center_lon, lat=center_lat, zoom=12)
      })
      output$myhousetypee <- renderLeaflet({ 
        temp <- house %>% filter(Type == input$dist1)
        temp <- na.omit(temp)

        center_lon = median(temp$Longtitude)
        center_lat = median(temp$Lattitude)

        mcol = c()
        if(input$dist1 == "h")
        {mcol = "red"}
        if(input$dist1 == "u")
        {mcol = "green"}
        if(input$dist1 == "t")
        {mcol = "blue"}

        leaflet(house$Type) %>% addTiles() %>%
          addCircleMarkers(lng = ~Longtitude, lat = ~Lattitude, data = temp, radius = 3, popup = paste("Bedroom : ", as.character(temp$Bedroom2), "<br>",
                                                                                                       "Bathroom : ", as.character(temp$Bathroom), "<br>",
                                                                                                       "Rooms: ", as.character(temp$Rooms), "<br>",
                                                                                                       "Cars : ", as.character(temp$Car)),
                           color = mcol)  %>%

          # controls
          setView(lng=center_lon, lat=center_lat, zoom=12)})

      output$mybuilding <- renderLeaflet({ 

        getColor <- function(house) {
          sapply(house$BuildingArea, function(BuildingArea) 
            {
            if(BuildingArea > 147) 
            {"red"} 
            }
            )
        }

        center_lon = median(house$Longtitude)
        center_lat = median(house$Lattitude)

        leaflet(house$BuildingArea) %>% addTiles() %>%
          addCircles(lng = ~Longtitude, lat = ~Lattitude, data = house, radius = ~(house$BuildingArea) , popup = paste("Building Area : ", as.character(house$BuildingArea), "<br>",
                                                                                                                                   "Landsize : ", as.character(house$Landsize)),
                           color = getColor(house))  %>%

          # controls
          setView(lng=center_lon, lat=center_lat, zoom=12)})


      output$mysuburb <- renderLeaflet({
        #temp <- house %>% filter(CouncilArea==input$suburb)
        #selectInput("sub","Suburb",choices=unique(temp$Suburb),selected=unique(temp$suburb)[1])

        #temp <- house %>% filter(Regionname == input$suburb)
        temp <- house %>% filter(Price > input$pslider[1] & Price < input$pslider[2])
        temp <- temp %>% filter(Type == input$dist)
        temp <- temp %>% filter(Distance > input$sliderDist[1] & Distance < input$sliderDist[2])
        temp <- temp %>% filter(Suburb == input$top10)
        temp <- na.omit(temp)

        school <- school %>% filter(Suburbs == input$top10)


        center_lon = median(temp$Longtitude)
        center_lat = median(temp$Lattitude)

        leaflet(school) %>% addTiles() %>%
          addMarkers(~temp$Longtitude, ~temp$Lattitude, popup = ~paste("<table>
                                                                         <thead><tr><td >Suburb</td><td>School Name</td><td> Ranking</td></tr></thead>
                                                                         <tbody><tr><td>", Suburbs, "</td><td>",Schools, "</td><td>", ICSEA.Rank, "</td></tr></tbody>
                                                                         </table"))

      #  leaflet(temp$Suburb) %>% addTiles() %>%
      #    addCircles(lng = ~Longtitude, lat = ~Lattitude, data = temp, radius = 8, 
      #               popup = paste(sep = "<br/>", mal),
      #               color = c("red"))  %>%
                     #addMarkers(data = school, popup = as.character(school$Schools, school$ICSEA.Rank), clusterOptions = markerClusterOptions()) %>%

          # controls
      #    setView(lng=center_lon, lat=center_lat, zoom=12)

        })


    }

    shinyApp(ui, server)

感谢您的帮助!

0 个答案:

没有答案