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.
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:
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
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.
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.