尝试创建 Shiny 应用程序时出错

Getting an error when trying to create a Shiny app

我有两个 'Total Population' 和 'Population Density' 使用形状文件创建的地图。现在,我正在尝试构建一个闪亮的应用程序,以便我可以从总人口更改为人口密度,并且情节应该相应地改变。当我 运行 代码时,我得到以下错误代码:

警告:错误:ggplot2 不知道如何处理 class 矩阵的数据

这是我一直在尝试使用的代码:

library(shiny)
library(ggplot2) #Loading necessary libraries


ui <- fluidPage(
  selectInput("mr",
              label="Type of Plot",
              choices=c("Total Population", "Density"),
              selected="Total Population"),
  plotOutput("curv") #Giving an input name and listing out types to choose in the Shiny app
)

server <- function(input, output){

  output$curv <- renderPlot({
    ggplot() +
      geom_polygon(data = final.plot==input$mr, 
                   aes(x = long, y = lat, group = group, fill = Population), 
                   color = "black", size = 0.20) + 
      coord_map()+
      scale_fill_distiller(name="Population", palette = "YlGn")+
      labs(title="Population in Australia")
  }) # Output with the data file and input string to change when input changes.
}

shinyApp(ui = ui, server = server)

非常感谢任何帮助。

更新:

我的数据集如下所示:

     id     long       lat order  hole piece
1 Ashmore and Cartier Islands 123.1169 -12.25333     1 FALSE     1
2 Ashmore and Cartier Islands 123.1206 -12.25611     2 FALSE     1
3 Ashmore and Cartier Islands 123.1222 -12.25861     3 FALSE     1
4 Ashmore and Cartier Islands 123.1239 -12.25528     4 FALSE     1
5 Ashmore and Cartier Islands 123.1258 -12.25333     5 FALSE     1
6 Ashmore and Cartier Islands 123.1275 -12.25619     6 FALSE     1
                          group Population Density
1 Ashmore and Cartier Islands.1         NA      NA
2 Ashmore and Cartier Islands.1         NA      NA
3 Ashmore and Cartier Islands.1         NA      NA
4 Ashmore and Cartier Islands.1         NA      NA
5 Ashmore and Cartier Islands.1         NA      NA
6 Ashmore and Cartier Islands.1         NA      NA

这存储在名为 "final.plot" 的 DataFrame 中。其他州有人口和密度值。我能够创建人口的静态可视化,它看起来像这样:

密度有一个类似的,我正在尝试创建闪亮的应用程序,我可以在这两个应用程序之间切换,以便情节相应地改变。现在我尝试了以下代码:

library(shiny)
library(ggplot2) #Loading necessary libraries


ui <- fluidPage(
  selectInput("pop",
              label="Type of Plot",
              choices=c("Population", "Density"),
              selected="Total Population"),
  plotOutput("curv") #Giving an input name and listing out types to choose in the Shiny app
)

server <- function(input, output){

  output$curv <- renderPlot({
    ggplot() +
      geom_polygon(data = final.plot, 
                   aes(x = long, y = lat, group = group, fill = input$pop), 
                   color = "black", size = 0.25) + 
      coord_map()+
      scale_fill_distiller(name="Density", palette = "Spectral")+
      labs(title="Population in Australia")
  })
}
shinyApp(ui = ui, server = server)

但我收到一条错误消息 "Discrete value supplied to continuous scale"。

更新 2: 这是我正在使用的数据集的 link: Dataset

我快速浏览了您的代码并提出了一些建议。

1) 在提供您的数据集时,您可以使用函数 dput() - 这会写入您的 data.frame 的文本表示形式,回答您问题的人可以简单地将其粘贴到 R 中。例如:

dput(final.plot)

这将生成文本输出,我可以通过在输出前加上 final.plot <- 前缀将其分配给数据框。我已经重新创建了您的数据框并使用 dput() 将其输出为下面的文本。现在其他用户可以快速剪切和粘贴您的数据:

请注意此数据集有误 - 见下文

final.plot <- structure(list(id = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "Ashmore and Cartier Islands", class = "factor"), 
                             long = c(123.1169, 123.1206, 123.1222, 123.1239, 123.1258, 123.1275),
                             lat = c(-12.25333, -12.25611, -12.25861, -12.25528, -12.25333, -12.25619),
                             order = 1:6, hole = c(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE),
                             piece = c(1L, 1L, 1L, 1L, 1L, 1L),
                             group = structure(c(1L, 1L, 1L, 1L, 1L, 1L),
                             .Label = "Ashmore and Cartier Islands.1", class = "factor"), 
                             Population = c(NA, NA, NA, NA, NA, NA),
                             Density = c(NA, NA, NA, NA, NA, NA)),
                             .Names = c("id", "long", "lat", "order", "hole", "piece", "group", "Population", "Density"),
                             class = "data.frame",
                             row.names = c(NA, -6L))

错误 "Discrete value supplied to continuous scale" 是由两个问题引起的。

i) 您在 Population 和 Density 列中都传递了 NA。下面的数据框向这些列添加了一些(不切实际的)数字,当我 运行 孤立的绘图代码时错误被删除。

更正玩具数据集

final.plot <- structure(list(id = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "Ashmore and Cartier Islands", class = "factor"), 
                             long = c(123.1169, 123.1206, 123.1222, 123.1239, 123.1258, 123.1275),
                             lat = c(-12.25333, -12.25611, -12.25861, -12.25528, -12.25333, -12.25619),
                             order = 1:6, hole = c(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE),
                             piece = c(1L, 1L, 1L, 1L, 1L, 1L),
                             group = structure(c(1L, 1L, 1L, 1L, 1L, 1L),
                                               .Label = "Ashmore and Cartier Islands.1", class = "factor"), 
                             Population = c(1, 2, 3, 4, 5, 6),
                             Density = c(7, 3, 9, 1, 3, 6)),
                        .Names = c("id", "long", "lat", "order", "hole", "piece", "group", "Population", "Density"),
                        class = "data.frame",
                        row.names = c(NA, -6L))

ii) 当 运行 交互时,错误是因为您没有传递适当的数据来填写 fill = input$pop。您应该根据所选内容传递 final.plot$Population 或 final.plot$Density 中的值。您传递的是下拉框的输出 - "Population" 或 "Density"。这可以使用 renderPlot 中的 switch 语句来纠正:

# User input assigns appropriate data to selectedData which can be passed to other functions:
  selectedData <- switch(input$pop, 
                   "Population" = final.plot$Population,
                   "Density" = final.plot$Density)

2) 如果您能提供生成您在上面的更新中显示的静态地图的代码,将会很有帮助。在调试 Shiny 代码时,我发现最简单的方法是首先让函数以非交互方式运行,然后将其合并到 Shiny 中。我试图在下面提取您的绘图代码,但它没有产生预期的结果:

library(ggplot2) #Loading necessary libraries
library(mapproj)
library(maps)
ggplot() +
  geom_polygon(data = final.plot, 
               [![aes(x = long, y = lat, group = group, fill = Population), 
               color = "black", size = 0.25) + 
  coord_map()+
  scale_fill_distiller(name="Density", palette = "Spectral")+
  labs(title="Population in Australia")`

3) 我不熟悉在 R 中将数据绘制到地图上,但我相信您的应用程序需要加载 library(mapproj) 和 library(maps) 才能获得所需的结果。这是以上所有内容的总和:

library(shiny)
library(ggplot2) #Loading necessary libraries
#I added the two lines below:
library(mapproj) 
library(map)

ui <- fluidPage(

  selectInput("pop",
              label="Type of Plot",
              choices=list("Population", "Density"),
              selected="Population"), #NOTE: Total Population changed to Population so that it selects correct default value

  plotOutput("curv") #Giving an input name and listing out types to choose in the Shiny app
)

server <- function(input, output){

#You will probably want to simply import your dataframe final.plot using read.table etc:      
final.plot <- structure(list(id = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "Ashmore and Cartier Islands", class = "factor"), 
                                   long = c(123.1169, 123.1206, 123.1222, 123.1239, 123.1258, 123.1275),
                                   lat = c(-12.25333, -12.25611, -12.25861, -12.25528, -12.25333, -12.25619),
                                   order = 1:6, hole = c(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE),
                                   piece = c(1L, 1L, 1L, 1L, 1L, 1L),
                                   group = structure(c(1L, 1L, 1L, 1L, 1L, 1L),
                                                     .Label = "Ashmore and Cartier Islands.1", class = "factor"), 
                                   Population = c(1, 2, 3, 4, 5, 6),
                                   Density = c(7, 3, 9, 1, 3, 6)),
                              .Names = c("id", "long", "lat", "order", "hole", "piece", "group", "Population", "Density"),
                              class = "data.frame",
                              row.names = c(NA, -6L))

  output$curv <- renderPlot({
    #Assign value of selectedData based upon user input:
    selectedData <- switch(input$pop, 
                   "Population" = final.plot$Population,
                   "Density" = final.plot$Density)
    ggplot() +
      geom_polygon(data = final.plot, 
                   aes(x = long, y = lat, group = group, fill =  selectedData), 
                   color = "black", size = 0.25) + 
      coord_map()+
      scale_fill_distiller(name="Density", palette = "Spectral")+
      labs(title="Population in Australia")
  })
}
shinyApp(ui = ui, server = server)

现在您需要做的就是用您更新中显示的生成静态地图的代码替换闪亮应用程序中 renderPlot 中的错误代码。