I met a wired situation. After add new code The code cannot knit out.
function_name <- function (...)
{
output <- if (output_format == "list") {
evolved.ts
} else if (output_format == "tsibble") {
as.tsibble(evolved.ts)
}
return(output)
}
You could read the arguments with args <- list(...):
function_name <- function (...)
{
args <- list(...)
# Code
output <- if (args$output_format == "list") {
evolved.ts
} else if (output_format == "tsibble") {
as.tsibble(evolved.ts)
}
return(output)
}
Related
I want to define a function operator that will get a plot as input and in case it produces an error it should return another plot.
My try:
handle_plot_error <- function(f) {
wrapper <- function(...) {
tryCatch(
{
f(...)
},
error = function(e) {
hist(1:3)
}
)
}
wrapper
}
But when I try:
handle_plot_error(plot(1,1))
it returns:
function(...) {
tryCatch(
{
return(f(...))
},
error = function(e) {
return(hist(1:3))
}
)
}
<bytecode: 0x7ff29dc38668>
<environment: 0x7ff29d9fab00>
Why are you using wrapper ? You may try :
handle_plot_error <- function(f) {
tryCatch(
{
f
},
error = function(e) {
hist(1:3)
}
)
}
where :
#No error plot
handle_plot_error(plot(1, 1)) #returns
and
#Error plot
handle_plot_error(plot(1:5, a = 2)) #returns
I am new to R, and I am trying to create a user-defined function and run it inside a for loop. The function is defined in the same script before it is called. However, when the function is called, the code inside the function doesn't run. No syntax errors occur.
Here is the function definition:
placeInVector <- function(subjectID, morpho) {
if (morpho == "Alg") {
algVec <- c(algVec, subjectID)
}
else if (morpho == "Anem") {
anemVec <- c(anemVec, subjectID)
}
else if (morpho == "Barn") {
barnVec <- c(barnVec, subjectID)
}
else if (morpho == "Biv") {
bivVec <- c(bivVec, subjectID)
}
else if (morpho == "BrBryo") {
brbryoVec <- c(brbryoVec, subjectID)
}
else if (morpho == "ColTuni") {
coltuniVec <- c(coltuniVec, subjectID)
}
else if (morpho == "EnBryo") {
enbryoVec <- c(enbryoVec, subjectID)
}
else if (morpho == "Nothing") {
nothingVec <- c(nothingVec, subjectID)
}
else if (morpho == "SolTuni") {
soltuniVec <- c(soltuniVec, subjectID)
}
else if (morpho == "Spng") {
spngVec <- c(spngVec, subjectID)
}
else if (morpho == "TubeWm") {
tubewmVec <- c(tubewmVec, subjectID)
}
}
Here is where the function is called later on in the script:
for (subject in subjects$SubjectID) {
currentRow <- finalIDs %>%
filter(SubjectID == subject)
morphoID <- as.character(currentRow$FinalIDs1)
if (morphoID != "NONE") {
placeInVector(subject, morphoID)
}
}
Note: all of the vectors referenced in the function definition (algVec, anemVec, etc.) are defined as empty vectors.
As far as I understand, you are trying to add elements to vectors. Because I don't have a reproducible example, I will use my own. Here, add_to_vector adds elements to a and b:
a <- c(1,2)
b <- c("a","b")
add_to_vector <- function(vec, to_add){
vec <- c(vec, to_add)
return(vec)
}
a <- add_to_vector(a, 3)
b <- add_to_vector(b, "c")
Hopefully, this gives you an idea of how R works.
Output
> a
[1] 1 2 3
> b
[1] "a" "b" "c"
I have functions and would like to save from the IF function the variable "sick [i]" to take advantage of it
for(i in 1:licznik){
print_func <- function(a, b)
{
if(a > b)
{
print('wspolczynnik jest wiekszy' )
print(sick[i])
}
}
print_func(a[i], b[i])
}
How to do it
?
Try this from R for Data Science.
out <- vector("list", length(licznik))
for (i in seq_along(licznik)) {
if(a[[i]] > b[[i]]) {
print('wspolczynnik jest wiekszy')
print(sick[[i]])
out[[i]] <- sick[[i]]
}
}
str(unlist(out))
I'm running the below code to extract key phrases from a raw data file. While i am successfully able to do it, i am not able to get the frequency or count of the extracted keywords which would help me understand the ranking of the occurrence of the keywords since i am using GraphNEL. Is there any way i can get the key phrase count? TIA.
ConstructTextGraph <- function(n)
{
word_graph <- new("graphNEL")
i <- 1
while (i < length(words) ) {
if ( IsSelectedWord(words[i]) ) {
links <- GetWordLinks(i,n)
if (links[1] != "") {
cat(i," ",words[i]," - ",paste(c(links),collapse=" "),"\n")
if ( length(which(nodes(word_graph)==words[i]))==0 ) {
word_graph <- addNode(words[i],word_graph)
}
for (j in 1:length(links)) {
if ( length(which(nodes(word_graph)==links[j]))==0 ) {
word_graph <- addNode(links[j],word_graph)
word_graph <- addEdge(words[i],links[j],word_graph,1)
}
else {
if ( length(which(edges(word_graph,links[j])[[1]]==words[i]))>0 ) {
prev_edge_weight <- as.numeric(edgeData(word_graph,words[i],links[j],"weight"))
edgeData(word_graph,words[i],links[j],"weight") <- prev_edge_weight+1
}
else {
word_graph <- addEdge(words[i],links[j],word_graph,1)
}
}
}
}
}
i <- i+1
}
word_graph
}
Please let me know if more information is needed.
I have multiple functions handing around arguments that may be missing.
e.g. i have
mainfunction <- function(somearg) {
mytest(somearg)
fun <- function() { subfunction(somearg) }
fun()
}
with the interesting aspect that the only interaction of mytest(somearg) with the arg is that it tests if the argument isn’t missing:
mytest = function(somearg) {
print(missing(somearg))
}
subfunction then again tests if it’s missing and treats it accordingly:
subfunction = function(somearg) {
if (missing(somearg))
somearg = NULL
else
somearg = matrix(somearg, cols = 2)
# somearg is used here…
}
the kicker is that, with somearg missing, this doesn’t work: matrix(somearg, cols = 2) throws
argument "somearg" is missing, with no default
during debugging, i found the following:
at the start of mainfunction, missing(somearg) returns TRUE
in mytest, missing(somearg) returns TRUE
insubfunction, missing(somearg) returns FALSE (!!!!)
therefore the matrix branch is hit, but in reality, somearg is missing, so it fails…
wat.
the #BenBolker way:
mainfunction <- function(somearg = NULL) {
mytest(somearg)
fun <- function() { subfunction(somearg) }
fun()
}
mytest = function(somearg) {
print(is.null(somearg))
}
subfunction = function(somearg) {
if (is.null(somearg))
somearg = 1:10
else
somearg = matrix(somearg, ncol = 2)
somearg
}
Another way, using explicit missing argument
mainfunction <- function(somearg) {
is_missing <- missing(somearg)
mytest(is_missing)
fun <- function() { subfunction(somearg, is_missing) }
fun()
}
mytest = function(x) { print(x) }
subfunction = function(somearg, is_arg_missing) {
if (is_arg_missing)
somearg = 1:10
else
somearg = matrix(somearg, ncol = 2)
somearg
}
A third way, using plain missing arg passing:
mainfunction <- function(somearg) {
is_missing <- missing(somearg)
mytest(somearg)
fun <- function() {
if (is_missing) subfunction() else
subfunction(somearg)
}
fun()
}
mytest = function(somearg) {
print(missing(somearg))
}
subfunction = function(somearg) {
if (missing(somearg))
somearg = 1:10
else
somearg = matrix(somearg, ncol = 2)
somearg
}