R-Shiny 中的 circlepackeR - 根据用户输入创建圆形包装图

Posted

技术标签:

【中文标题】R-Shiny 中的 circlepackeR - 根据用户输入创建圆形包装图【英文标题】:circlepackeR in R-Shiny - create circle packing graph based on user inputs 【发布时间】:2020-11-13 14:42:12 【问题描述】:

我正在尝试根据用户输入制作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")

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

【问题讨论】:

【参考方案1】:

@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)
)

【讨论】:

以上是关于R-Shiny 中的 circlepackeR - 根据用户输入创建圆形包装图的主要内容,如果未能解决你的问题,请参考以下文章

R-Shiny:在文件输入上选择输入反应

R-Shiny:如何显示使用 saveHTML() 函数创建的动画和控件?

D3.js中Circle Packing详解

D3.js中Circle Packing详解

将 ggplot 与 r-shiny 一起使用时出错

带有 if 语句的 ReactiveTimer R-Shiny