首页 > 解决方案 > 将函数应用于小标题的每个案例

问题描述

我第二次参加 Stackoverflow。

我有一个名为 bw_test 的函数,它有几个像这样的参数:

  bw_test <- function(localip, remoteip, localspeed, remotespeed , duracion =30,direction ="both"){

comando <- str_c("ssh usuario@", localip ," /tool bandwidth-test direction=", direction," remote-tx-speed=",remotespeed,"M local-tx-speed=",localspeed,"M protocol=udp user=usuario password=mipasso  duration=",duracion," ",remoteip)

  resultado <- system(comando,intern = T,ignore.stderr  = T)

# resultado pull from a ssh server a vector like this:
# head(resultado)
#[1] "                status: connecting\r" "            tx-current: #0bps\r"       "  tx-10-second-average: 0bps\r"      
#[4] "      tx-total-average: 0bps\r"       "            rx-current: #0bps\r"       "  rx-10-second-average: 0bps\r"       

  resultado %<>%
    replace("\r","") %>%
    tail(17) %>% 
    trimws("both") %>%
    as_tibble %>%
    mutate(local=localip, remote=remoteip) %>%
    separate(value,sep=":", into=c("parametro","valor")) %>%
    head(15) 


  resultado$valor %<>%
    trimws() %>%
    str_replace("Mbps","") %>% str_replace("%","") %>% str_replace("s","")

  resultado  %<>% 
  spread(parametro,valor)  
  resultado %<>%
    mutate(`tx-percentaje`=as.numeric(resultado$`tx-total-average`)/localspeed) %>%
    mutate(`rx-percentaje`=as.numeric(resultado$`rx-total-average`)/remotespeed) 

  return(resultado)


    }

这个函数返回一个像这样的小标题:

A tibble: 1 x 19
  local remote `connection-cou… direction duration `local-cpu-load` `lost-packets` `random-data` `remote-cpu-loa…
  <chr> <chr>  <chr>            <chr>     <chr>    <chr>            <chr>          <chr>         <chr>           
1 192.… 192.1… 1                both      4        13               0              no            12              
# … with 10 more variables: `rx-10-second-average` <chr>, `rx-current` <chr>, `rx-size` <chr>,
#   `rx-total-average` <chr>, `tx-10-second-average` <chr>, `tx-current` <chr>, `tx-size` <chr>,
#   `tx-total-average` <chr>, `tx-percentaje` <dbl>, `rx-percentaje` <dbl>

因此,当我在 rbind 中调用该函数时,会得到每次运行在 tibble 上的结果:

rbind(bw_test("192.168.105.10" ,"192.168.105.18", 75,125),
      bw_test("192.168.133.11","192.168.133.9", 5 ,50),
      bw_test("192.168.254.251","192.168.254.250",  25,150))

我的结果是示例:

# A tibble: 3 x 19
  local remote `connection-cou… direction duration `local-cpu-load` `lost-packets` `random-data` `remote-cpu-loa…
  <chr> <chr>  <chr>            <chr>     <chr>    <chr>            <chr>          <chr>         <chr>           
1 192.… 192.1… 20               both      28       63               232            no            48              
2 192.… 192.1… 20               both      29       4                0              no            20              
3 192.… 192.1… 20               both      29       15               0              no            22              
# … with 10 more variables: `rx-10-second-average` <chr>, `rx-current` <chr>, `rx-size` <chr>,
#   `rx-total-average` <chr>, `tx-10-second-average` <chr>, `tx-current` <chr>, `tx-size` <chr>,
#   `tx-total-average` <chr>, `tx-percentaje` <dbl>, `rx-percentaje` <dbl>

我的问题是将函数应用于像这样的小标题的情况。

aps <- tribble(
  ~name, ~ip, ~remoteip , ~bw_test, ~localspeed,~remotespeed,
  "backbone_border_core","192.168.253.1", "192.168.253.3", 1,200,200,
  "backbone_2_site2","192.168.254.251", "192.168.254.250", 1, 25,150
}

我试图使用地图,但我得到了:

map(c(aps$ip,aps$remoteip,aps$localspeed,aps$remotespeed), bw_test)

el argumento "remotespeed" está ausente, sin valor por omisión 

我相信原因 c(aps$ip,aps$remoteip,aps$localspeed,aps$remotespeed) 首先提供所有 aps$ip 的案例,然后提供所有 aps$remoteip 等等。

我正在使用正确的策略?这是地图合适的方式

我做错了什么?

¿如何将函数应用于每一行以获取请求的小标题?

我会很感激你的帮助。

问候。

标签: rfunctiontibble

解决方案


尝试使用pmap_df.

output <- purrr::pmap_df(list(aps$ip, aps$remoteip, aps$localspeed, 
                              aps$remotespeed), bw_test)

推荐阅读