My input file contains many lines of tab-delineated information in a text file. Below would be a line from the text file:
100026 TGACTGCATGACGTACAC NM_006342.1 TACC3
My code is as follows:
constant_source <- 'constants.R'
source(constant_source)
source(classes_file)
processFile = function(filepath) {
con = file(filepath, "r")
while ( TRUE ) {
line = readLines(con, sep="\t")
print(line)
if (length(line) == 0 ) {
break
}
}
close(con)
}
The output, however, is as follows:
100026\tTGACTGCATGACGTACAC\tNM_006342.1\tTACC3
Why is the readLines function not respecting the separation parameter? I have been toying with this for a while and am stuck. Sorry about this; I just started learning R today. If it makes a difference, I am using RStudio.
Related
I have a question on reading large txt files and separate it based on the character "TIME".
Each "TIME" represents the pressure of a spatial area at a particular point in time.
How should I write the readtext functions that recognize the "TIME" characters and split them ?
I would first create a folder so that I can save the new files in it. Also, I would put the original data file in this folder.
# setwd("....") # Set the working directory as the folder you just created.
I saved the data structure that you provided in "data.txt"
The following lines will split your data (which is in "data.txt" in my computer) into files that have consecutive names, such as "data1.txt", "data2.txt", and so on.
incon = file("data.txt", "r")
i = 0
while (TRUE) {
line = readLines(incon, n = 1)
if (length(line) == 0) {
break
}
if (regexpr("TIME:", line) > 0) {
if (exists("outcon")) close(outcon)
i = i + 1
outcon = file(paste("data", i, sep=""), "w")
writeLines(line, outcon)
} else {
writeLines(line, outcon)
}
}
close(outcon)
I'm using slackr to send alert messages to a Slack channel. It works great except the message format is not great and I want to improve it.
install_github("hrbrmstr/slackr")
library(slackr)
slackr_setup(channel="#alerts", username="Mark Davis",
incoming_webhook_url = "https://hooks.slack.com/services/T31P8UDAB/BCH4HKQSC/*********",
api_token = "*********", echo = F)
alert="On Monday, 2018-09-03 # 2pm Pacific..."
slackr(alert)
Here is an example of how a message from slackr looks in Slack:
Here is an example of how I'd like it to look:
slackr doesn't seem to have many options in the way of formatting. I was thinking of building an image and inserting that, but I'm having trouble building an image out of a text file using R.
Perhaps there is another api I could call that could take my text and format it for slack?
I'm open to any suggestions.
Addendum:
Slackr has an option to upload files, so my latest attempt is to create an image from the text message and upload that object.
I am able to create a png file from the text message using the magick library. I created an image with a colored background, and I simply add the message text to the image:
library(magick)
alert_picture <- image_read('alert_480x150_dark_red.png')
alert_picture=image_annotate(alert_picture, DreamCloud_Alert, size = 20, gravity = "southwest",
color = "white", location = "+10+10")
image_write(alert_picture, path = "alert_picture.png", format = "png")
The image looks pretty good (although there doesn't seem to be an easy way to bold or underline specific words in the message), but the obstacle now is that I can't get the upload command to work.
slackr_upload(filename = "alert_picture.png")
I don't get any error messages but nothing is uploaded to slack.
I got around this issue by using the httr package to execute the post image function to slack.
Thanks to Adil B. for providing the solution:
Post Image to Slack Using HTTR package in R
I am not sure this is what you meant, but I solved allowing formatting like in a regular slack message by altering the slackr_bot() function and just removing the 2 sets of 3 back-ticks at the end of the code where it says text. Then just call it slackr_bot1() or something, and then you can post formatted messages. This is the function after the back-ticks removal:
slackr_bot1 <- function(...,
channel=Sys.getenv("SLACK_CHANNEL"),
username=Sys.getenv("SLACK_USERNAME"),
icon_emoji=Sys.getenv("SLACK_ICON_EMOJI"),
incoming_webhook_url=Sys.getenv("SLACK_INCOMING_URL_PREFIX")) {
if (incoming_webhook_url == "") {
stop("No incoming webhook URL specified. Did you forget to call slackr_setup()?", call. = FALSE)
}
if (icon_emoji != "") { icon_emoji <- sprintf(', "icon_emoji": "%s"', icon_emoji) }
resp_ret <- ""
if (!missing(...)) {
# mimics capture.output
# get the arglist
args <- substitute(list(...))[-1L]
# setup in-memory sink
rval <- NULL
fil <- textConnection("rval", "w", local = TRUE)
sink(fil)
on.exit({
sink()
close(fil)
})
# where we'll need to eval expressions
pf <- parent.frame()
# how we'll eval expressions
evalVis <- function(expr) withVisible(eval(expr, pf))
# for each expression
for (i in seq_along(args)) {
expr <- args[[i]]
# do something, note all the newlines...Slack ``` needs them
tmp <- switch(mode(expr),
# if it's actually an expresison, iterate over it
expression = {
cat(sprintf("> %s\n", deparse(expr)))
lapply(expr, evalVis)
},
# if it's a call or a name, eval, printing run output as if in console
call = ,
name = {
cat(sprintf("> %s\n", deparse(expr)))
list(evalVis(expr))
},
# if pretty much anything else (i.e. a bare value) just output it
integer = ,
double = ,
complex = ,
raw = ,
logical = ,
numeric = cat(sprintf("%s\n\n", as.character(expr))),
character = cat(sprintf("%s\n\n", expr)),
stop("mode of argument not handled at present by slackr"))
for (item in tmp) if (item$visible) { print(item$value, quote = FALSE); cat("\n") }
}
on.exit()
sink()
close(fil)
# combined all of them (rval is a character vector)
output <- paste0(rval, collapse="\n")
loc <- Sys.getlocale('LC_CTYPE')
Sys.setlocale('LC_CTYPE','C')
on.exit(Sys.setlocale("LC_CTYPE", loc))
resp <- POST(url = incoming_webhook_url, encode = "form",
add_headers(`Content-Type` = "application/x-www-form-urlencoded",
Accept = "*/*"), body = URLencode(sprintf("payload={\"channel\": \"%s\", \"username\": \"%s\", \"text\": \"%s\"%s}",
channel, username, output, icon_emoji)))
warn_for_status(resp)
}
return(invisible())
}
slackr_bot1("*test* on time")
I'm new to R and need to add a new function file to an existing package. For the programming and testing I used load_all() (from the devtools pkg) to have the original R files. I wrote my function and saved it in the same directory as the rest of the (original) R files.
But now when I do load_all(), this function is executed! I have no idea why. What am I doing wrong here?
library(devtools)
calc_curve<-function(){
CurvePars<-c(0,0)
doMIC<-readline("Do you want to calculate MIC? (y/n) \n")
if(doMIC=="y")
{
CAlb<-readline("Do you want to use C.Albicans/FLC standard curve? (y/n)\n")
#use the equation we calculated MIC=a*exp(b*Rad). V1=a, V2=b
if(CAlb=="y") CurvePars=c(7.17, -0.129)
else
{
exFile <- readline("Do you have a standard curve file? (y/n)\n ")
if(exFile=="y"){
setwd(getwd())
curveFile <- tcltk::tk_choose.files(caption = "Select the standard curve data file") ;
tempPars<-read.table(curveFile, header=FALSE,sep=",",dec=".");
CurvePars=unlist(tempPars)
# calculate MIC and add to data file
}
else{
calib<-readline("Do you want to provide data for MIC calibration? (y/n)\n")
if(calib=="y"){
MICFile <- tcltk::tk_choose.files(caption = "Select the MIC calibration file")
MICdata<- read.csv(MICFile, header=FALSE,sep="\t",dec=".");
MIC_length=length(MICdata[[1]])
R1<-0
MIC<-0
for (i in 1:MIC_length)
{
R1[i]<-RAD2.df[MICdata[i,1],"RAD20"];
MIC[i]<-MICdata[i,2]
}
fit<-lm(log(MIC)~R1)
A=summary(fit)$coefficients[1]
CurvePars<-exp(A)
B=summary(fit)$coefficients[2]
CurvePars[2]<-B
CurveFile <- readline( "How would you like to name the curve file?\n ")
# handle the case of a pre-existing file
#open(File=paste(CurveFile,".csv",sep=""),"w")
CurveFile=paste(getwd(),"/",CurveFile,".csv",sep="")
write.table(CurvePars,CurveFile,col.names = FALSE,row.names = FALSE)
}
else {CurvePars[1]=0
CurvePars[2]=0}
}
}
}
CurvePars
}
Take this simple python script for example:
#!/usr/bin/env python3
# /tmp/xxx.py
import time
for i in range(1000):
print(i)
time.sleep(1)
It continuously outputs numbers. I can call it from R like this:
system2("/tmp/xxx.py", stdin=?)
where stdin can be set to NULL, "", TRUE or a filename. But what I am looking for is a way to process these numbers in realtime. For example, whenever a number is printed from this python script, I want to multiply the number by Pi and then print it to the console. Is there a way to do this in R?
Not an expert, but I got something working.
First of all, I used the following /tmp/xxx.R executable Rscript instead of your python script as I found out python was buffering its output (not printing one line at a time) which makes it hard to test:
#!/usr/bin/env Rscript
for (i in 1:5) {
cat(i, "\n")
Sys.sleep(1)
}
Then the R code:
system('mkfifo /tmp/xxx.fifo')
f <- fifo("/tmp/xxx.fifo", 'r')
p <- pipe('/tmp/xxx.R > /tmp/xxx.fifo; echo OVER > /tmp/xxx.fifo', 'w')
while(TRUE) {
line <- readLines(f, n = 1)
if (length(line) > 0) {
if (line == "OVER") break
cat(pi * as.numeric(line), "\n")
}
Sys.sleep(0.1)
}
close(f)
close(p)
some of which was inspired from this: https://stackoverflow.com/a/5561188/1201032.
Hope it answers your question.
I need to be able to login to a system and get to get the tail -50 of the file. I have teh following set of code but this gets the whole file. Since the file is so huge, I need to run the script every 10 minutes and get the last 50 lines of the file. This is what I have:
library(RCurl)
for(k in 1:2) {
day1<-day-k
day1<-format(day1, "%Y%m%d")
day_vector<-c(day_vector, day1)
}
servers<-c("ServerA", "ServerB","ServerC")
for (i in 1:length(servers)) {
for(j in 1:length(day_vector)) {
ser_day<-paste(servers[i], day_vector[j], sep=".")
url<-paste0(servers[i], ".example.net/opt/files/appl_log_")
url<-paste(url, ser_day, sep="")
url<-paste0("sftp://", url)
tryCatch({
x<-getURL(url, userpwd="account1:passwd1", connecttimeout=60)
writeLines(x, paste0(servers[i], ".txt"))
},error = function(e)
{
print("unable to get the file")
})
Is there any way to get the tail of the file?