Discrepancy in nested functions output with suboptimal variable mapping in between RStudio and RGui. (solved: incompatible encoding)

I have a completely functional code which I developed in RStudio, but the output is different in RGui. I suspect the issue is not the best practice of using environments. The output in RGui is correct if I run fn5 outside of the nested function. My question is how to assign output of fn5 directly to Global Environment and fix other possible error producing suboptimal code.

fn5 <- function(Tracker, country, os){
costdata_Soll-Ausg.[[which(all_variables$Country == country & all_variables$OS Name` == os & all_variables$Tracker == Tracker)]] <<-
as.numeric(gsub(",",".", gsub("\ €", "", unlist(data2[((2+((days_in_month(date)[[1]])+ 4)(x-1))):(2+((days_in_month(date)[[1]])+ 4)(x-1)+days_in_month(date)[[1]]), column][[1]]))))
}

compressed_fn <- function(){

#more functions here

if (exists("costdata_Soll-Ausg.") == TRUE){
print("Cost data already imported. Skipping.")
} else {
costdata_Soll-Ausg. <<- list()
pmap(list(all_variables$Tracker, all_variables$Country, all_variables$OS Name), fn5)

#more functions here

}

final_fn <- function(d){

#more functions

compressed_fn()

#more functions

}

As a general rule you shouldn't be writing functions which change something in the global environment. See here for more info.

You should change your functions to a) only use variables which are passed directly to them as an argument and b) return a value or list of values rather than assigning directly to the global environment.

It's hard to say exactly how your code should be changed without knowing more about what you are trying to do but the code below gives a rough example of how you might change fn5 and compressed_fn:

fn5 <- function(Tracker, country, os){
  fn5_output <- as.numeric(gsub(",",".", gsub("\ €", "", unlist(data2[((2+((days_in_month(date)[[1]])+ 4)(x-1))):(2+((days_in_month(date)[[1]])+ 4)(x-1)+days_in_month(date)[[1]]), column][[1]]))))
  return(fn5_output)
}


compressed_fn <- function(all_variables){
  
  #more functions here
  
  if (exists("costdata_Soll-Ausg.") == TRUE){
    print("Cost data already imported. Skipping.")
  } else {
    costdata_Soll-Ausg. <- pmap(list(all_variables$Tracker, all_variables$Country, all_variables$OS Name), fn5)
    
    #more functions here
    
  }
  
  return(costdata_Soll-Ausg.)
}

Thank you for thorough and quick response! Unfortunately I cant resolve the issue at this time. My apologies for the complex and not reproducible example given. compressed_fn() will be only run once without any variables inside () therefore I cant use this example (there are also duplicate names etc), but it does not matter. I will reduce the problem to one question: how to you assign the output of pmap to global list inside/outside(doesnt matter) of nested function. The output costdata_Soll-Ausg´ is suppose to be a list.

in general you can assign any object to the global environment ( thought best practice says you probably shouldnt..) with assign(), envir param can be set to .GlobalEnv

Assign a Value to a Name

Description

Assign a value to a name in an environment.

Usage

assign(x, value, pos = -1, envir = as.environment(pos), inherits = FALSE, immediate = TRUE)

Arguments

x a variable name, given as a character string. No coercion is done, and the first element of a character vector of length greater than one will be used, with a warning.
value a value to be assigned to x .
pos where to do the assignment. By default, assigns into the current environment. See ‘Details’ for other possibilities.
envir the environment to use. See ‘Details’.

Thank you, but unfortunately the issue seems to be somewhere else. I took fn5 out of nested functions and this is how it currently stands:

fn5 <- function(Tracker, country, os){
  
  if (os == "android"){
    column <- which(Trackers == Tracker) + 1 
  } else {column <- which(Trackers == Tracker) + 1 + length(Trackers) + 2}
  
  y <- which(countries == country)
  print(y)
  
  if (y == 1){as.numeric(gsub(",",".", gsub("\\ €", "", unlist(data2[2:(2 + (days_in_month(date)[[1]])), column][[1]]))))
  } else {as.numeric(gsub(",",".", gsub("\\ €", "", unlist(data2[((2+((days_in_month(date)[[1]])+ 4)*(y-1))):(2+((days_in_month(date)[[1]])+ 4)*(y-1)+days_in_month(date)[[1]]), column][[1]]))))
  }
  
}

`costdata_Soll-Ausg.` <- pmap(list(all_variables$Tracker, all_variables$Country, all_variables$`OS Name`), fn5)

It works perfectly fine in RStudio, but not with RGui. data2 is a dataframe with columns as character strings. The issue is that instead of numeric output it procudes NA-s in RGui. Funnily enough if i susbstitute "column" and " y" with values it does produces requested output. Sourcing the code produces NA-s only.

Turns out that the problem is the encoding!! RGui reads gsub("\\ €" as gsub("\\ €". Any suggestions how to avoid that are welcome.

File - Save with encoding... Windows-1252 Solved the issue.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.