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

阿文德·苏里亚纳拉亚娜(Arvind Suryanarayana)

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

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

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

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中。其他州的人口和密度值。我能够创建“人口”的静态可视化内容,如下所示:

在此处输入图片说明

密度有一个类似的示例,我正在尝试创建Shiny应用程序,在该应用程序中我可以在这两个应用程序之间进行切换,以使情节发生相应的变化。现在,我已经尝试了以下代码:

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)

但是我收到一个错误消息:“离散值提供给连续刻度”。

更新2:这是我正在使用的数据集的链接:数据集

格莱美

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

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

错误“提供给连续刻度的离散值”是由两个问题引起的。

i)您在“人口”和“密度”列中均通过了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传递值,具体取决于所选内容。相反,您传递的是下拉框的输出-“人口”或“密度”。可以使用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中的错误代码。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

尝试运行WebView应用程序时出现错误

来自分类Dev

创建Kivy应用程序时总是出现此错误

来自分类Dev

尝试使用Rails创建phonegap应用程序时出现问题

来自分类Dev

尝试为我的电子应用程序创建安装程序时,生成未找到错误

来自分类Dev

尝试在Heroku上部署Node.js / Express / Socket.io应用程序时出现应用程序错误

来自分类Dev

尝试调试 xamarin.android 应用程序时总是出现“应用程序处于中断模式”错误

来自分类Dev

尝试使用Application Loader上载应用程序时出现错误ITMS-9000

来自分类Dev

Android Studio:尝试在Android设备上部署应用程序时出现细分错误

来自分类Dev

尝试测试Symfony 2应用程序时出现SQL错误-列已存在,表丢失

来自分类Dev

尝试打开应用程序时出现Heroku错误代码H13

来自分类Dev

尝试设置示例Web应用程序时出现Firebase OAuth错误

来自分类Dev

尝试安装应用程序时出现“依赖关系无法满足”错误

来自分类Dev

尝试从C#调用VB6应用程序时出现文件/访问错误

来自分类Dev

尝试安装应用程序时出现“依赖关系无法满足”错误

来自分类Dev

尝试安装Shopify应用程序时出现500个内部服务器错误

来自分类Dev

尝试安装应用程序时出现INSTALL_FAILED_DEXOPT / UID_CHANGED错误

来自分类Dev

每当尝试加载应用程序时,为什么weblogic中会出现jcrontab错误?

来自分类Dev

尝试运行应用程序时出现VS2015错误:无效的指针

来自分类Dev

尝试使用 Jetty 服务器加载 Web 应用程序时出现 HTTP 错误:503

来自分类Dev

尝试在 Xcode 中更新应用程序时出现证书错误

来自分类Dev

Rails:尝试运行 rails 应用程序时出现冻结错误

来自分类Dev

创建新的反应应用程序时出现“系统找不到指定的路径”错误

来自分类Dev

创建应用程序时出现 Jhipster Ionic 错误:getJhipsterAppConfig 不是函数

来自分类Dev

在启动我的Android应用程序时出现错误

来自分类Dev

下载应用程序时出现Testflight错误

来自分类Dev

运行应用程序时出现控制台错误

来自分类Dev

调试应用程序时出现Android Studio dexDebug错误

来自分类Dev

部署应用程序时出现Wildfly 9.0.2错误

来自分类Dev

归档IOS应用程序时出现协同设计错误

Related 相关文章

  1. 1

    尝试运行WebView应用程序时出现错误

  2. 2

    创建Kivy应用程序时总是出现此错误

  3. 3

    尝试使用Rails创建phonegap应用程序时出现问题

  4. 4

    尝试为我的电子应用程序创建安装程序时,生成未找到错误

  5. 5

    尝试在Heroku上部署Node.js / Express / Socket.io应用程序时出现应用程序错误

  6. 6

    尝试调试 xamarin.android 应用程序时总是出现“应用程序处于中断模式”错误

  7. 7

    尝试使用Application Loader上载应用程序时出现错误ITMS-9000

  8. 8

    Android Studio:尝试在Android设备上部署应用程序时出现细分错误

  9. 9

    尝试测试Symfony 2应用程序时出现SQL错误-列已存在,表丢失

  10. 10

    尝试打开应用程序时出现Heroku错误代码H13

  11. 11

    尝试设置示例Web应用程序时出现Firebase OAuth错误

  12. 12

    尝试安装应用程序时出现“依赖关系无法满足”错误

  13. 13

    尝试从C#调用VB6应用程序时出现文件/访问错误

  14. 14

    尝试安装应用程序时出现“依赖关系无法满足”错误

  15. 15

    尝试安装Shopify应用程序时出现500个内部服务器错误

  16. 16

    尝试安装应用程序时出现INSTALL_FAILED_DEXOPT / UID_CHANGED错误

  17. 17

    每当尝试加载应用程序时,为什么weblogic中会出现jcrontab错误?

  18. 18

    尝试运行应用程序时出现VS2015错误:无效的指针

  19. 19

    尝试使用 Jetty 服务器加载 Web 应用程序时出现 HTTP 错误:503

  20. 20

    尝试在 Xcode 中更新应用程序时出现证书错误

  21. 21

    Rails:尝试运行 rails 应用程序时出现冻结错误

  22. 22

    创建新的反应应用程序时出现“系统找不到指定的路径”错误

  23. 23

    创建应用程序时出现 Jhipster Ionic 错误:getJhipsterAppConfig 不是函数

  24. 24

    在启动我的Android应用程序时出现错误

  25. 25

    下载应用程序时出现Testflight错误

  26. 26

    运行应用程序时出现控制台错误

  27. 27

    调试应用程序时出现Android Studio dexDebug错误

  28. 28

    部署应用程序时出现Wildfly 9.0.2错误

  29. 29

    归档IOS应用程序时出现协同设计错误

热门标签

归档