简体   繁体   English

从列表中的向量中提取元素名称

[英]Extract element names from a vector within list

I'm building a shiny app and i would like to extract the name of a vector value within a list to use in the plot title.我正在构建一个闪亮的应用程序,我想在列表中提取向量值的名称以在情节标题中使用。

I have this list with variable names:我有这个带有变量名称的列表:

vars <- list(
  acidentes = c("Veículo" = "veic", "Tipo de acidente" = "tipo_acid",
                "# ciclistas" = "ciclistas"),
  vitimas = c("Sexo" = "sexo", "Idade" = "idade"),
  tempo = c("Ano" = "ano", "Mes" = "mes", "Dia da semana" = "dia_semana",
            "Hora do dia" = "hora_dia", "Período do dia" = "periodo_dia"),
  geo = c("Distritos" = "distritos")
)

vars

$acidentes
Veículo Tipo de acidente      # ciclistas 
"veic"  "tipo_acid"           "ciclistas" 

$vitimas
Sexo   Idade 
"sexo" "idade" 

$tempo
Ano    Mes    Dia da semana  Hora do dia  Período do dia 
"ano"  "mes"  "dia_semana"   "hora_dia"   "periodo_dia" 

$geo
Distritos 
"distritos" 

Inside shiny, my attempt was:在闪亮的内部,我的尝试是:

l <- names(vars[vars == input$variavel])
n <- names(vars[[l]])[vars[[l]] == input$variavel]
tit <- paste("Taxa média de fatalidade, por", n)

I can't understand why, but this works when my choice is the "distritos" variable, but not with another.我不明白为什么,但是当我选择的是“distritos”变量而不是另一个变量时,这会起作用。

If I run this code outside shiny environment with "distritos" i get this:如果我使用“distritos”在闪亮的环境之外运行此代码,我会得到:

l <- names(vars[vars == "ano"])
n <- names(vars[[l]])[vars[[l]] == "distritos"]
paste("Taxa média de fatalidade, por", n)
[1] "Taxa média de fatalidade, por Distritos"

But if I call another variable, for example "anos", it doesn't works.但是如果我调用另一个变量,例如“anos”,它就不起作用。

l <- names(vars[vars == "anos"])
n <- names(vars[[l]])[vars[[l]] == "anos"]
Error in vars[[l]] : attempt to select less than one element in get1index

I had tried using vars[vars == "anos"] and vars[vars %in% "anos"] and the result is the same.我曾尝试使用vars[vars == "anos"]vars[vars %in% "anos"] ,结果是一样的。 Works for "distritos" but not for another variable.适用于“distritos”,但不适用于其他变量。

Why is this not working?为什么这不起作用? Have someone an ideia on how to run this?有人知道如何运行它吗?

Sorry for my english and many thanks, in advance.对不起,我的英语,非常感谢,提前。

由于它是命名vectorlist ,我们可以使用lapply/sapply循环遍历list ,在比较后提取向量的names并将list元素unlist listvector (如果需要)

unlist(sapply(vars, function(x) names(x[x == "ano"])))

It works with the "distritos" example because that is only a length 1 character vector - so when you vars[vars == "distritos"] it matches the value of that vector.它适用于“distritos”示例,因为它只是一个长度为 1 的字符向量 - 所以当你vars[vars == "distritos"]它匹配该向量的值。

You need to either check on the names in the list (not the names of each individual character vector) or you need to look at the lower level using something like this:您需要检查列表中的名称(而不是每个单独字符向量的名称),或者您需要使用以下内容查看较低级别:

l <- "ano"
names(vars[sapply(vars, function(x) l %in% x)])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM