R - load_all() and adding a new function file - r

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
}

Related

IF statement in R function

I am trying to construct a function with an if statement within.
IN the code below,
I basically want to use different contr_x<- makeContrast.. command for different contr_x. ex) If contr_1 is in the input, it will use the first 1f's make constrast command, if contr_2 is in the input, it will use the second if's make constrast command...
But I am running in to the error that says the object contr_1 is not found. I am confused because in my understanding. contr_1 is just an input name, not an object. (not previously defined)
I am attaching the function code and the error below; I 'll appreciate any insight!!
code
run_limma <- function (model_x, support_x, fit_x,editing_x,contr_x,tmp_x){
message("starting modeling")
model_x<-model(support_x, model_x)
message("starting fitting")
fit_x<-limma_diff(editing_x, model_x,fit_x)
message("Making contrasts")
if (contr_x == "contr_1") {contr_x<-makeContrasts (diseaseAD - diseaseControl,
levels = colnames(coef(fit_x)))
}
if (contr_x == "contr_2") {contr_x<-makeContrasts (diseaseAD_MCI - diseaseControl,
levels = colnames(coef(fit_x)))
}
if (contr_x == "contr_3") {contr_x<-makeContrasts (diseaseMCI - diseaseControl,
levels = colnames(coef(fit_x)))
}
if (contr_x == "contr_4") {contr_x<-makeContrasts (diseasePD - diseaseControl,
levels = colnames(coef(fit_x)))
}
message("making tmp file")
tmp_x<-limma_cont(contr_x, fit_x, tmp_x)
tmp_x
}
error
run_limma(model_1, support_1, fit_1,editing_1,contr_1,tmp_1)
starting modeling
starting fitting
Making contrasts
Error in run_limma(model_1, support_1, fit_1, editing_1, contr_1, tmp_1) :
object 'contr_1' not found

readLines function not recognizing separating character "\t"

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.

Read txt files and separate by the chr

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)

How to improve formatting of slack messages using slackr?

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")

Poker deck simulation in R

I am working on a poker deck prability simulation. I would like to get the probability that player 1 is dealt a hand consisting of a single suit only.
I got the code below, however, I get the following error:
> checkDeck1()
Error in unique(deck[1:4]) : argument "deck" is missing, with no default
Code:
pokerdeck <- rep(LETTERS[1:4],13)
deck <- sample(x=pokerdeck, size=13) #Deck of player 1!
checkDeck1 <- function(deck) {
uniquedeck <- unique(deck[1:13])
## if it is only a single suit
if (length(uniquedeck)==1) {
rv <- TRUE
} else {
rv <- FALSE
}
return (rv)
}
checkDeck1()
You want to call
checkDeck1(deck)
In the code of your function checkDeck1, the scope of the variable deck is local - the fact that you name this variable like a global variable locally overwrites it.

Resources