R:无法读取闪亮的对象错误:不允许从闪亮的输出对象读取对象

时间:2019-12-11 07:11:18

标签: r shiny leaflet

genres <- c("Pop", "Rap")
bins <- c(0, 10, 20, 50, 100, 200, 500, 1000, Inf)
pal <- colorBin("Greens", domain = c(0,2000), bins = bins)

ui <- dashboardPage(
  skin = "green",
  dashboardHeader(title = "Concert Ticket Prices Spotify & Seat Geek"),
  dashboardSidebar(selectInput("genre", label = "Genre",choices = genres, selected = "Rap")),

  dashboardBody(

    fluidRow(box(width = 12,leafletOutput(outputId = "mymap"))),
    downloadButton('downloadData', 'Download Data Set (CSV)'),
    fluidRow(box(width = 12, dataTableOutput(outputId = "summary_table"))),
    fluidRow(textOutput(outputId = "n1")),
  )



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



  text <- reactive({input$genre})
  text2 <- reactive({text()})



  genre_shp <- Allgenre[Allgenre$genre == text2,]








  labels <- sprintf(
  "<strong>%s</strong><br/>Average Price: $%g <br/>%s </br> %s, %s <br/> %s ",
  genre_shp$Artist, genre_shp$`Avg Price`, genre_shp$Venue, genre_shp$City, genre_shp$name, genre_shp$`Data & Time`
) %>% lapply(htmltools::HTML)



  output$mymap <- renderLeaflet(
    leaflet(genre_shp) %>%
  setView(-96, 37.8, 4) %>%
  addProviderTiles("MapBox", options = providerTileOptions(
    id = "mapbox.light",
    accessToken = Sys.getenv('MAPBOX_ACCESS_TOKEN'))) %>%
  addPolygons(
    fillColor = ~pal(genre_shp$`Avg Price`),
    weight = 2,
    opacity = 1,
    color = "white",
    dashArray = "3",
    fillOpacity = 0.7,
    highlight = highlightOptions(
      weight = 5,
      color = "#666",
      dashArray = "",
      fillOpacity = 0.7,
      bringToFront = TRUE),
    label = labels,
    labelOptions = labelOptions(
      style = list("font-weight" = "normal", padding = "3px 8px"),
      textsize = "15px",
      direction = "auto")) %>%
  addLegend(pal = pal, 
            values = genre_shp$`Avg Price`, opacity = 0.7, title = NULL,
    position = "bottomright")

    )
  genres <- c("Pop", "Rap")
bins <- c(0, 10, 20, 50, 100, 200, 500, 1000, Inf)
pal <- colorBin("Greens", domain = c(0,2000), bins = bins)

ui <- dashboardPage(
  skin = "green",
  dashboardHeader(title = "Concert Ticket Prices Spotify & Seat Geek"),
  dashboardSidebar(selectInput("genre", label = "Genre",choices = genres, selected = "Rap")),

  dashboardBody(

    fluidRow(box(width = 12,leafletOutput(outputId = "mymap"))),
    downloadButton('downloadData', 'Download Data Set (CSV)'),
    fluidRow(box(width = 12, dataTableOutput(outputId = "summary_table"))),
    fluidRow(textOutput(outputId = "n1")),
  )



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



  text <- reactive({input$genre})
  text2 <- reactive({text()})



  genre_shp <- Allgenre[Allgenre$genre == text2,]








  labels <- sprintf(
  "<strong>%s</strong><br/>Average Price: $%g <br/>%s </br> %s, %s <br/> %s ",
  genre_shp$Artist, genre_shp$`Avg Price`, genre_shp$Venue, genre_shp$City, genre_shp$name, genre_shp$`Data & Time`
) %>% lapply(htmltools::HTML)



  output$mymap <- renderLeaflet(
    leaflet(genre_shp) %>%
  setView(-96, 37.8, 4) %>%
  addProviderTiles("MapBox", options = providerTileOptions(
    id = "mapbox.light",
    accessToken = Sys.getenv('MAPBOX_ACCESS_TOKEN'))) %>%
  addPolygons(
    fillColor = ~pal(genre_shp$`Avg Price`),
    weight = 2,
    opacity = 1,
    color = "white",
    dashArray = "3",
    fillOpacity = 0.7,
    highlight = highlightOptions(
      weight = 5,
      color = "#666",
      dashArray = "",
      fillOpacity = 0.7,
      bringToFront = TRUE),
    label = labels,
    labelOptions = labelOptions(
      style = list("font-weight" = "normal", padding = "3px 8px"),
      textsize = "15px",
      direction = "auto")) %>%
  addLegend(pal = pal, 
            values = genre_shp$`Avg Price`, opacity = 0.7, title = NULL,
    position = "bottomright")

    )

   output$summary_table <- renderDataTable(data.frame(genre_shp))



   output$downloadData <- downloadHandler(
    filename = function() { 
      paste('SelectedRows', '.csv', sep='')
      },
    content = function(file) {
      write.csv(genre_shp, file)
    }
    )




}
shinyApp(ui=ui, server = server)

我正在尝试构建一个使用下拉菜单进行更新的仪表板。我拥有的数据是将要举行的演唱会及其费用。我有一个较大的数据集,需要作图处理,因为我计划使用美国地图按类型进行绘图。但是,当我尝试从input $ genre获取数据时,不允许使用输出来过滤我的数据框以进行绘图。任何帮助,将不胜感激。谢谢 祝一切顺利 M

2 个答案:

答案 0 :(得分:0)

这部分对我来说很奇怪

text <- reactive({input$genre})
text2 <- reactive({text()})

genre_shp <- Allgenre[Allgenre$genre == text2,]

为什么不简单

output$mymap <- renderLeaflet( {
genre_shp <- Allgenre[Allgenre$genre == input$genre,]
leaflet(genre_shp)
})

答案 1 :(得分:0)

这是不行的:

  text <- reactive({input$genre})
  text2 <- reactive({text()})

  genre_shp <- Allgenre[Allgenre$genre == text2,]

前两行不是必需的。第三个是不可能的,因为text2是一个无功值。 只要做:

genre_shp <- reactive(Allgenre[Allgenre$genre == input$genre,])

然后使用genre_shp()作为传单参数。