0

我正在尝试circlepackeR根据用户输入制作图表。我想知道在我正在使用的包中是否有可能,我是否应该完全使用不同的方法,或者我的代码是否有错误。我已经盯着它太久了。

这是我想要完成的基础知识。当用户从选项中选择一个县时,出现selectInput()一个模式对话框,其中显示该所选县的种族/民族/性别构成的圆圈包装。在我尝试通过使用反应函数根据选择的输入过滤数据来对数据帧进行子集化之前,效果很好。当我将数据从反应式过滤器转换为节点时会出现错误(“下标越界”、“请提供 json 对象或列表”、“在没有活动反应式上下文的情况下不允许操作..”)

这是我的代码:

#libraries
library(shiny)
library(shinydashboard)
library(data.tree)
library(circlepackeR) 
library(dplyr)

用户界面:

ui <- dashboardPage(
    dashboardHeader(),
    dashboardSidebar(
        selectInput("selectcounty", "Select County", unique(counties1$NAME))
    ),
    dashboardBody()
   )

服务器:

server <- function(session, input, output) {
#1. observe event, render modal dialogue for select input  
    observeEvent(input$selectcounty, {
        click <- input$selectcounty
        if(is.null(click))
            return() 
        {showModal(modalDialog(
            footer = NULL,
            easyClose = T,
            circlepackeROutput(outputId = "race1", width = "100%", height = "400px")
        ))
            }
    }) 

    ###### CIRCLE TREE MAP OF SELECT INPUT #######
#2. subset data
    subset_race<- reactive({
     dplyr::filter(race, race[NAME]==input$selectcounty)
    })

 ### *this is where the problem is I think --- can't convert to nodes from a reactive function?   
    subset_nodes <- reactive({as.Node(subset_race())})
  
#3. display in circle packer graph
    output$race1 <- renderCirclepackeR({
      circlepackeR(subset_nodes, size = "r_count", color_min = "hsl(56,80%,80%)", color_max = "hsl(341,30%,40%)")
    })   
}
shinyApp(ui = ui, server = server)

这是我的数据:

#ETHNICITY/RACE/GENDER DATA
dput(head(race))
structure(list(NAME = c("Autauga-AL", "Autauga-AL", "Autauga-AL", 
"Autauga-AL", "Autauga-AL", "Autauga-AL"), STATE_NAME = c("AL", 
"AL", "AL", "AL", "AL", "AL"), gender = structure(c(2L, 1L, 2L, 
1L, 2L, 1L), .Label = c("female", "male"), class = "factor"), 
    hispanic = structure(c(2L, 2L, 1L, 1L, 2L, 2L), .Label = c("hispanic", 
    "nonhispanic"), class = "factor"), race = structure(c(12L, 
    12L, 12L, 12L, 3L, 3L), .Label = c("asian", "asian in combination", 
    "black", "black in combination", "HNAC_FEMALE", "HNAC_MALE", 
    "native", "native in combination", "NHNAC_FEMALE", "NHNAC_MALE", 
    "two or more", "white", "white in combination"), class = "factor"), 
    r_count = c(20138L, 21077L, 740L, 652L, 5171L, 5927L), pathString = c("world/male/nonhispanic/white", 
    "world/female/nonhispanic/white", "world/male/hispanic/white", 
    "world/female/hispanic/white", "world/male/nonhispanic/black", 
    "world/female/nonhispanic/black")), row.names = c(1L, 3109L, 
6217L, 9325L, 12433L, 15541L), class = "data.frame")


###US COUNTY DATA
dput(head(counties1))
structure(list(NAME = "Autauga-AL", Year = 2018L, ID = 1001L, 
    STATE_NAME.x = "AL", All.Ages.in.Poverty.Percent = 13.8, 
    GEOID = "01001", ALAND = "1539614693", AWATER = "25744269", 
    INTPTLAT = "+32.5322367", INTPTLON = "-086.6464395", X = -86.643, 
    Y = 32.535, charpov = "13.8", not_pov = 86.2, charnot_pov = "86.2"), row.names = 98L, class = "data.frame")

这是我第一次尝试圆形包装。我缺少什么信息?

4

1 回答 1

0

@Limey 在我关于在此处嵌套反应函数的其他 SO 问题中回答了这个问题:R Shiny - Is it possible to nest reactive functions?

基本上,我需要 req() 来要求用户在反应式过滤器中选择输入:

subset_race <- reactive({
  req(input$selectcounty)
  subset.data.frame(race, NAME==input$selectcounty)
})
于 2020-08-25T00:04:11.643 回答