【发布时间】: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?它没有被指定为输入