使用R与传单和DataTable与反应对象



我已经做了一个非常简化的版本的问题,我已经问了两天前在另一个帖子(让传单工作与数据表选择在R闪亮)我离解决办法更近了一步。有一个R数据框架,在马萨诸塞州的4个城市有12个地点。dataTable允许用户选择城市,而传单地图将只显示所选城市中的位置。

library(shiny)
library(DT)
library(leaflet)
#Massmpg <- (read.csv("Massmpg.csv"))
City <- c("Boston","Boston", "Boston", "Lowell","Lowell", "Lowell","Worcestor", "Worcestor","Worcestor","Springfield","Springfield","Springfield")
lat <- c(42.35, 42.355, 42.345, 42.63,42.625,42.635,42.27,42.265,42.275, 42.1,42.105,42.095)
lng <- c(-71.05,-71.045,-71.055,-71.316,-71.315,-71.317,-71.79,-71.785,-71.795,-72.6,-72.595,-72.605)
MassLocations <- data.frame(City, lat,lng)

# MassLocations has 4 cities with 3 locations each
ui <- fluidPage(titlePanel("Mass mpg by location"),

# Create a new Row in the UI for selectInputs
fluidRow(
column(4,
selectInput("City",
"City:",
c("All",
unique(as.character(MassLocations$City))))
),
),
# Create a new row for the table.
leafletOutput("map01"),
DT::dataTableOutput("table")

)

server <- function(input, output) {

dataShow <- reactive({
DT::renderDataTable(DT::datatable({
data <- MassLocations
if (input$City != "All") {
data <- data[data$City == input$City,]

}
data
}))
})

# Filter data based on selections
output$table <- dataShow()


# map
output$map01 <- renderLeaflet({
#pal <- colorNumeric("YlOrRd", domain=c(min(quakes$mag), max(quakes$mag)))
qMap <- leaflet(data = (dataShow())) %>% 
addTiles() %>%
addCircles(radius =3, color="red")
qMap
})

} 

# Run the application 
shinyApp(ui = ui, server = server)  '''

不幸的是,我得到一个错误后闪亮的窗口首先打开,然后崩溃。错误是收听http://127.0.0.1:7312*警告:错误:操作不允许没有活动响应上下文。

  • 你试图做一些只能在响应消费者内部完成的事情。55岁:错误:没有活动响应上下文,不允许操作。
  • 你试图做一些只能在响应消费者内部完成的事情。*

如果你把renderDataTable()放在reactive()之外

server <- function(input, output) {
# Filter data based on selections
dataShow <- reactive({
data <- MassLocations
if (input$City != "All") {
data <- data[data$City == input$City, ]
}
data
})
# Display
output$table <- DT::renderDataTable(
DT::datatable(dataShow()))
...

最新更新