【问题标题】:R shiny: RenderDataTable and RenderLeafletR 闪亮:RenderDataTable 和 RenderLeaflet
【发布时间】:2019-05-03 13:54:30
【问题描述】:

我是 R 的新手(闪亮),但学得很快。所以,对于一个可能很简单的问题,我深表歉意......我在 RenderDataTable 和 RenderLeaflet 的组合方面遇到了一些问题。

我先给你一些我已经准备好的代码: 首先,一个数据框和初始代码:

#three packages to plot maps    
library(tidyverse)
library(shiny)
library(leaflet)

#generate a dataset of species, distributions, densities and references to literature
df<- data.frame(
Number_Total = sample(c("5", "6", "1", "3")),
Species = sample(c("Ilione trifaria", "Pherbellia argyrotarsis", "Euthycera seguyi", "Ilione trifaria")),
Article= sample(c("Verbeke (1950)", "Verbeke (1950)", "Vala (1999)", "Vala (1999)")),
X = sample(c("1", "2", "3", "4")),
Y = sample(c("3", "2", "1", "1")))

#just make sure coordinates and numbers are numeric
df$X<-as.numeric(df$X) 
df$Y<-as.numeric(df$Y) 
df$Number_Total<-as.numeric(df$Number_Total)

#for the map you need to save dataframe as RDS
saveRDS(df, ".sample_data.rds")

然后,UI(在我看来还可以吗?):

ui <- fluidPage(titlePanel("Map and plot table at same time"),  

          sidebarLayout(
            sidebarPanel(
              selectizeInput('species', 'Choose species', 
                             choices = df$Species, multiple = FALSE, 
                             options = list(placeholder = 'select species'))
            ),
            mainPanel(
              leafletOutput("CountryMap", width = 600, height = 300),
              dataTableOutput("table")
              )
          )
)

还有服务器:

  server <- function(input, output, session) {  
  map_data <- reactive({
    df[df$Species %in% input$species, ]
  })

  output$CountryMap <- renderLeaflet({

    leaflet() %>% addTiles() %>% 
      setView(lng = 1, lat = 1, zoom = 5) %>%
      addCircles(lng = map_data() %>% pull(Y), lat = map_data() %>% pull(X), weight = 10, 
                 radius = sqrt(map_data() %>% pull(Number_Total))*15000, 
                 popup = map_data() %>% pull(Species))

  })



  output$table <- renderDataTable({
    df %>%
      filter(df$Species == input$distinct_vars)  %>% 
      select(Article) %>%
      unique()
  })
  }

当然,最好的路线:

shinyApp(ui = ui, server = server)

此代码为您提供了一个界面,您可以在其中在地图上绘制物种,效果很好。

但是,它还应该在地图下方给出一个DataTable,其中列出了参考文献?!我确定错误出在服务器端,但失败时找不到?

这是一个很大的问题,但是,我希望它是一个容易解决的问题。 非常感谢!!! 乔纳斯

【问题讨论】:

  • 什么是input$distinct_vars?它没有被指定为输入

标签: r shiny


【解决方案1】:

首先,distinct_vars 未被指定为输入。其次,我将数据集创建添加为新的reactive,以免每次都加载它。如果从rds读取,可以把读取文件的代码放到reactive函数dataset()中。

您的代码版本略有不同。看起来它正在工作:

library(tidyverse)
library(shiny)
library(leaflet)

#generate a dataset of species, distributions, densities and references to literature

ui <- fluidPage(titlePanel("Map and plot table at same time"),  

            sidebarLayout(
              sidebarPanel(
                selectizeInput('species', 'Choose species', 
                               choices = df$Species, multiple = FALSE, 
                               options = list(placeholder = 'select species'))
              ),
              mainPanel(
                leafletOutput("CountryMap", width = 600, height = 300),
                dataTableOutput("table")
              )
            )
)

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

  dataset <- reactive({
    df<- data.frame(
      Number_Total = sample(c("5", "6", "1", "3")),
      Species = sample(c("Ilione trifaria", "Pherbellia argyrotarsis", "Euthycera seguyi", "Ilione trifaria")),
      Article= sample(c("Verbeke (1950)", "Verbeke (1950)", "Vala (1999)", "Vala (1999)")),
      X = sample(c("1", "2", "3", "4")),
      Y = sample(c("3", "2", "1", "1")))

    #just make sure coordinates and numbers are numeric
    df$X<-as.numeric(df$X) 
    df$Y<-as.numeric(df$Y) 
    df$Number_Total<-as.numeric(df$Number_Total)

    df
  })

  map_data <- reactive({

    df <- dataset()


    df[df$Species %in% input$species, ]
  })

  output$CountryMap <- renderLeaflet({

    leaflet() %>% addTiles() %>% 
      setView(lng = 1, lat = 1, zoom = 5) %>%
      addCircles(lng = map_data() %>% pull(Y), lat = map_data() %>% pull(X), weight = 10, 
                 radius = sqrt(map_data() %>% pull(Number_Total))*15000, 
                 popup = map_data() %>% pull(Species))

  })



  output$table <- renderDataTable({
    dataset() %>%
      filter(Species == input$species)  %>% 
      select(Article) %>%
      unique()
  })
}

shinyApp(ui = ui, server = server)

【讨论】:

  • 谢谢,非常感谢!尤其是您的 cmets 让数据集成为反应式。最好的祝愿,乔纳斯
猜你喜欢
  • 2014-07-06
  • 2016-01-02
  • 2018-07-30
  • 2020-12-26
  • 2018-05-02
  • 2020-09-18
  • 2014-02-28
  • 2021-11-21
  • 1970-01-01
相关资源
最近更新 更多