【问题标题】:R: Cannot read shiny objects Error: Reading objects from shiny output object not allowedR:无法读取闪亮的对象错误:不允许从闪亮的输出对象读取对象
【发布时间】:2020-04-04 10:09:23
【问题描述】:
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

【问题讨论】:

    标签: r shiny leaflet


    【解决方案1】:

    这部分我觉得很奇怪

    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)
    })
    

    【讨论】:

    • 还是没有运气,我试过了:genre_shp
    • 在这一点上,您提到了应用程序失败的地方——所有其他的都应该运行它的最后一部分以将数据连接到传单图
    • 我已经稍微更新了答案 - 你可以尝试运行这个简单的版本,看看你是否能让mymapui 端正确呈现?
    • 出现与 SmokeyShakers 建议的修复相同的问题。当我运行服务时,genre_shp 确实成功更新了摘要输出的数据,但它不适用于绘图。当我将标签设置为genre_shp $variable时我知道返回,当我将它们转换回来时我得到一个返回,但是对于标签以外的所有内容都使用genre_shp,但是该返回不包含传单输出。我应该在哪里得到 -> 错误:参数的“类型”(列表)无效
    【解决方案2】:

    这是不行的:

      text <- reactive({input$genre})
      text2 <- reactive({text()})
    
      genre_shp <- Allgenre[Allgenre$genre == text2,]
    

    前两行不是必需的。第三个是不可能的,因为text2 是一个反应值。 做吧:

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

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

    【讨论】:

    • 这里有一点好消息。这确实允许读取数据集并通过输出摘要表示,但我的传单图仍然没有出现
    • 我仍然得到:.getReactiveEnvironment()$currentContext() 中的错误:如果没有活动的反应上下文,则不允许操作。 (你试图做一些只能从反应式表达式或观察者内部完成的事情。)
    • 当我执行诸如genre_shp()$Avg Price 之类的操作时,似乎不喜欢我的标签的设置方式。当我保留所有内容但将标签更改为 orgData$Avg Price(我有用于测试的一部分数据)时,应用程序会运行,但下拉菜单适用于我的统计数据摘要,但不适用于我的传单
    • 啊,所以你不能这样做genre_shp(),除非你处于反应式上下文中。
    • 在闪亮的应用程序中,我得到了这个错误来代替我的传单图:错误:无效的参数“类型”(列表)
    猜你喜欢
    • 2014-12-04
    • 2017-01-03
    • 2020-02-29
    • 2021-07-18
    • 2018-10-29
    • 2015-04-13
    • 1970-01-01
    • 1970-01-01
    • 2017-09-19
    相关资源
    最近更新 更多