R Shiny-是否可以嵌套反应函数?

问题描述

在R-Shiny中。试图破坏一个很长的反应函数(数千行!)。 假设地,是否可以嵌套条件反应函数,类似于:

STATE_filter <- reactive({
 
   if(input$selectcounty ends with "-AL") {
    run AL_filter()
  }
  else if (input$selectstate ends with "-AR"){
    run AR_filter()
  }
  else {
    return("ERROR")
  }
})

编辑

非假设地,我试图基于美国各县的用户选择输入来创建嵌套的反应式过滤功能。选择县后,将在模式对话框中弹出circlepackeR图 。这是我正在使用的数据:

dput(head(demographics))
structure(list(NAME = c("Autauga-AL","Baldwin-AL","Barbour-AL","Bibb-AL","Blount-AL","Bullock-AL"),STATE_NAME = c("AL","AL","AL"),gender = structure(c(2L,2L,2L),.Label = c("female","male"),class = "factor"),hispanic = structure(c(2L,.Label = c("hispanic","nonhispanic"),race = structure(c(6L,6L,6L),.Label = c("asian","black","islander","native","two or more","white"),makeup = structure(c(2L,.Label = c("in combination","one race","two or more"),r_count = c(456L,1741L,114L,96L,320L,44L),pathString = c("world/male/nonhispanic/white/one race","world/male/nonhispanic/white/one race","world/male/nonhispanic/white/one race")),row.names = c(NA,class = "data.frame")

下面是我正在使用的反应函数的一个示例。这是一万多行的一小部分子集,我想通过按状态划分行来“嵌套”它(AL代表阿拉巴马州,AR代表阿肯色州),因此它是一段简洁的代码。

demographics_filter <- reactive({
   if(input$selectcounty == "Autauga-AL") {
    race_autauga <- subset.data.frame(demographics,NAME=="Autauga-AL")
    nodes_autauga <- as.Node(race_autauga)
  } 
  else if(input$selectcounty== "Baldwin-AL") {
    race_baldwinAL <-subset.data.frame(demographics,NAME=="Baldwin-AL")
    nodes_baldwinAL<- as.Node(race_baldwinAL)
  } 
 else if(input$selectcounty== "Ashley-AR") {
    race_AshleyAR <-subset.data.frame(race,NAME=="Ashley-AR")
    nodes_AshleyAR<- as.Node(race_AshleyAR)
  }
  else {
    return("ERROR!")
  }
})

最后,这是服务器中利用此功能的图形:

     output$circle_graph_of_demographics <- renderCirclepackeR({
      circlepackeR(demographics_filter(),size = "r_count"
    })  

解决方法

就个人而言,如果单个功能/反应式的长度为1000线,则肯定存在通过重构进行改进的空间!

我对您提供给我们的demographics_filter反应式感到奇怪的是,对于有效数据,它返回NULL,对于无效数据,它返回"ERROR!",因此我不确定如何在output$circle_graph_of_demographics中成功使用它。如果您不需要它返回任何内容,那么使用eventReactive(input$selectcounty,{...})会更合适吗?

似乎您需要根据input$selectcounty的值更改来创建(一组)节点和(一组)过滤后的数据帧。目前尚不清楚为什么在Autauga-Alinput$selectcounty的情况下为什么需要Baldwin-AR的节点和子集,这就是为什么我将“集合”放在方括号中。 / p>

根据您告诉我们的内容(没有MWE,无法确定确切的内容适合您的需求),我会做类似的事情:

demographics_filter <- reactive({
  req(input$selectcounty)
  subset.data.frame(demographics,NAME==input$selectcounty)
})

demographics_node <- reactive({
  as.Node(demographics_filter())
})

应该提供一个紧凑的解决方案,该解决方案对于县和州名称的更改具有鲁棒性。如果我对您的理解正确,那么这将用七行替换成千上万行。显然,您可能需要重构其余代码以考虑所做的更改。

如果您确实需要过滤后的数据帧和节点集,那么我将执行以下操作:

v <- reactiveValues(
       demographics_filter=list(),demographics_nodes=list()
     )

eventReactive(input$selectcounty,{
  req(input$selectcounty)
  v$demographics_filter[[input$selectcounty]] <- subset.data.frame(demographics,NAME==input$selectcounty)
  v$demographics_node[[input$selectcounty]] <- as.Node(v$demographics_filter[[input$selectcounty]])
})

同样,它是一个紧凑,强大的解决方案,您可能需要在其他地方重构代码以考虑更改。

我的所有代码都未经测试,因为我没有MWE可以使用。

,

知道了!

是的,您(I)可以嵌套反应函数。

{{1}}

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...