我编写了一个脚本,该脚本从Json URL检索两个不同服务器的2个值,并将其附加到csv文件中。然后我添加了if语句,以检查在csv文件的特定列(具有4列:“ HogMapName”,“ HogGMode”,“ SomosMapName”,“ SomosGMode”)中输入的最后一个值是否相同以及是否相同然后将其写入相同的csv文件。
我的第一个问题是,如何更新此代码,其中从json url检索到的服务器数据(MapName,GameMode)进入相应的服务器 CSV文件中的命名列。 (例如,将Hog_data(MapName,GameMode)保存在csv文件中的“ HogMapName”和“ HogGMode”克隆下)。这是代码:
prspydata <- read_csv("prspydata.csv")
pr <- fromJSON("https://www.realitymod.com/prspy/json/serverdata.json? 1548630687630")
mapname_gamemode <- function(server_name){# takes ServerName as input and returns MapName and GameMode values from json data
i <- pr$Data$ServerName == server_name
return(list(pr$Data$MapName[i],pr$Data$GameMode[i]))
}
#store data of two different servers in data.frame(MapName, GameMode) by calling out mapname_gamemode()
Hog_data <- data.frame(mapname_gamemode("[PR v1.5.5.2] =HOG= Mixed Maps")[[1]],mapname_gamemode("[PR v1.5.5.2] =HOG= Mixed Maps")[[2]] )
Somos_data <- data.frame(mapname_gamemode("[PR v1.5.5.2] =SF= [Somos Fortes Brasil]")[[1]], mapname_gamemode("[PR v1.5.5.2] =SF= [Somos Fortes Brasil]")[[2]])
# write only if new mapname is not same as last
if(mapname_gamemode("[PR v1.5.5.2] =HOG= Mixed Maps")[[1]]!= tail(prspydata$HogMapName,1)){# checks if last element in mentioned column is same
write.table(Hog_data, file = "prspydata.csv", append = TRUE, row.names = FALSE, col.names = !file.exists("prspydata.csv"), sep = ",")
}
if(mapname_gamemode("[PR v1.5.5.2] =SF= [Somos Fortes Brasil]")[[1]] != tail(prspydata$HogMapName,1)){# checks if last element in mentioned column is same
write.table(Somos_data, file = "prspydata.csv", append = TRUE, row.names = FALSE, col.names = !file.exists("prspydata.csv"), sep = ",")
}
Here's the csv file image that shows how I want to append the data
第二个问题:如上图所示,“ if-statements”为未通过if子句的一台服务器写入一个空白单元格(即,如果在特定列中最后输入的值是相同的)。有没有一种方法可以在不留空白单元格的情况下追加数据?还是我必须在两个不同的csv文件中存储不同的服务器数据? 预先感谢!