我创建了这个名为 MingiPolygons 的包。它标识由 ggplot 包形成的地图可视化中的多边形对象。我已经使它与 sf 包中的数据兼容。创建带有弹出消息的仪表板时,出现此错误:“警告:writeImpl 中的错误:要写入的文本必须是长度为一的字符向量”。 这是代码
library(tidyverse)
library(sf)
library(reactable)
library(rsconnect)
library(shiny)
library(shinythemes)
library(mingiPolygons)
library(shinyjs)
nameshp <- system.file("shape/nc.shp", package="sf")
tableau <- read.csv('childcare modified.csv')
# GET DATA FROM NORTH CAROLINA COUNTY
nc_tab <- tableau %>%
filter(state_name == "North Carolina")
dream <- st_read(nameshp, quiet=TRUE)
dream$vble <- dream$SID74
dream$vble2 <- dream$SID79
# merge datasets
d <- merge(x=dream, y=nc_tab, by.x = "FIPSNO", by.y="county_fips_code", all=TRUE)
server <- function(input, output, session){
output$plot <- renderPlot({
ggplot(d)+
geom_sf(aes(fill=vble))+
scale_fill_distiller(palette="YlGnBu", direction=1)
})
d_reactive <- reactiveVal(NULL)
tryCatch({
observeEvent(input$plot_click, {
d_reactive(d)
kristy <- mingiPolygons:::preWork(d_reactive(), "NAME")
county_choice <- mingiPolygons(kristy, input$plot_click, "NAME", 50, 4)
sel_county_data <- d_reactive()[d_reactive()$NAME == as.character(county_choice),]
if(!is.null(sel_county_data)){
showModal(modalDialog(
h3(as.character(sel_county_data[1, "NAME"]))
))
} else if (is.null(sel_county_data)){
showModal(ModalDialog(
h2("The data is the problem. There is no information")
))
}
})
})
}
ui <- fluidPage(
theme = shinytheme("cerulean"),
tags$div(class="jumbotron text-center",
style="margin-bottom:30px:margin-top:0px;height:10px;background-color: dodgerblue;color:ivory;font-size:30px",
"New Sample"),
fluidRow(column(12, plotOutput("plot", click='plot_click')))
)
shinyApp(ui=ui, server=server)
一些要求:
devtools::install_github("mmburu8/mingiPolygons")
欲了解更多信息:https://mmburu8.github.io/mingiPolygons/我的主要假设是变量“d”中的数据,用于评估多边形对象并包含未处理的信息。 任何建议都会有帮助。
对具有
sf
列的 geometry
对象进行子集化时,即使未明确选择,也会返回该对象。因此,在您的 h3
调用中,您不是传递字符串,而是传递 sf
对象,这让 h3
发出警告。
补救措施很简单:明确删除
geometry
列:
### [...]
h3(sel_county_data[1, "NAME", drop = TRUE])
## or
## h3(st_drop_geometry(sel_county_data)[1, "NAME"]))