0

I am trying to avoid a whole lot of repeating myself (copy/pasting) by using functions in my R code. I have a working function as per below, but I can't figure out how to build the IP/file path address inside the function for different sites. My problem is how to glue the unique usernames at each site.

Hopefully it makes sense when you read the code below and comments within. Apologies for any confusion, I'm finding it hard to explain as simply as possible

library(tidyverse)
library(data.table) 
library(lubridate)

#SiteA
siteip <- "10.10.10.10"
sitename <- "SiteA"
site <- "sitea"
PW <- "passwd" #same at all sites

# SiteB will need different variables to glue
# I've commented these out for ease of reading (turned text red as code was wrong)

# siteip <- 20.20.20.20
# sitename <- "SiteB     
# site <- "siteb"        
# PW <- "passwd" #same at all sites


# This months date format
Year <-format(Sys.Date(), format="%Y")
Month <- format(Sys.Date(), format="%B")
MM <- format(Sys.Date(), format="%m")

# Last months date format
LM <- format(Sys.Date() %m+% months(-1), format="%B")
Lmon <- format(Sys.Date() %m+% months(-1), format="%m")
LY <- format(Sys.Date() %m+% months(-1), format="%Y")

### Total GPS function

gps <- function(w,x,y,z) {
  w <- glue::glue("ftp://{sitename}:{PW}@{siteip}/1data/{site}/{LY}/{LM}/{site}}{LY}-{Lmon} GPS.txt") # last month data
  w <- fread(w, header = FALSE, select = c(1, 3, 4),
             col.names = c("DateTime", "Latitude", "Longitude"), sep = " ")
  x <- glue::glue("ftp://{sitename}:{PW}@{siteip}/1data/{site}/{Year}/{Month}/{site}}{Year}-{MM} GPS.txt") # This month data
  x <- fread(x, header = FALSE, select = c(1, 3, 4),
                  col.names = c("DateTime", "Latitude", "Longitude"), sep = " ")
  y <- unique(rbindlist(list(w, x))) # combine last month/this month
  z <- y %>%
    filter(between(as_datetime(DateTime), Sys.Date() - 10, Sys.Date())) # filter for last ten day's of data
}

siteagps <- gps(siteagps)
# works up to here

sitebgps <- gps(sitebgps)
# can't get this working
prezzo
  • 1
  • 1
  • What is `siteagps` and `sitebgps` ? Can you show first few rows of your expected output? – Ronak Shah Sep 21 '20 at 06:30
  • It seams you haven't really understood how functions in R are working. you are passing four parameters `w,x,y,z` but are redefing them within the function before you use them. and then when you call the function you are only passing one object. – Bertil Baron Sep 21 '20 at 08:45
  • What you want to do is to build a function that takes the parameters that are different between SiteA and SiteB as parameters and then use them in glue – Bertil Baron Sep 21 '20 at 08:47
  • @RonakShah siteagps and aiteebgps are just the final data tables i am after from separate sites, trying to use the same function to get the same result at each. I'm not concerned about the output for this question as I have working code, it is more about how to substitute in different variables (sitename and siteip) to the dynamic ip/path – prezzo Sep 21 '20 at 22:22
  • @BertilBaron you're correct, I do not really understand the functions still, maybe I am trying to be to clever without actually knowing what I'm doing. I was hoping to really streamline the whole code as I need to repeat this at 30+ sites. Am I better off removing this line from the function? `w <- glue::glue("ftp://{sitename}:{PW}@{siteip}/1data/{site}/{LY}/{LM}/{site}}{LY}-{Lmon} GPS.txt") ` – prezzo Sep 21 '20 at 22:27
  • It would be helpful if you still update your question with few rows of `siteagps` and `sitebgps` and show expected output for first few rows based on that. What you are doing might be obvious to you but not so much for us. – Ronak Shah Sep 21 '20 at 23:55

0 Answers0