我在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,其中列出了参考文献?我很确定错误是在服务器端,但是我找不到失败的时间?
这是一个大问题,但是,我希望这是一个容易解决的问题。 非常感谢你!!! 乔纳斯(Jonas)
答案 0 :(得分:0)
首先,未将distinct_vars
指定为输入。其次,我将数据集创建添加为新的reactive
,以免每次都加载它。如果您是从rds读取的,则可以将读取文件的代码放入反应性函数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)