How to initialise a map while setting an airflow variable? - airflow

I want to use an argument of map type coming from airflow variable from the DAG. My Map<String,List[String]> looks like this:
Map("stringValue" -> List("abc", "def", "ghi"))
How should I initialise this value in my airflow variable UI?
I tried this in my DAG and UI:
Variable.get("stringValue", Map("stringValue" -> List("abc", "def", "ghi")))

Related

Binding Ipywidgets value to a variable

I am using ipywidgets to take user inputs in Jupyter Notebook. Below, I put the code for most two frequent types I am using:
import ipywidgets as widgets
widgets.BoundedIntText(
min=0,
max=90,
step=1,
description='Surface Tilt (in °)',
disabled=False
)
widgets.RadioButtons(
options=['South', 'East-West'],
description='Orientation',
disabled=False
)
However, I can't figure out from the documentation, how can I save the user input to a variable so I can use it next cells.
Any help or alternative solutions/packages to facilitate will be appreciated.
Best,
Debayan
First you need to assign a name to each widget, as #ac24 mentioned. Then by using the value property you can get the value for each widget.
By running the code below you can print the values for both of your widgets:
import ipywidgets as widgets
BoundedIntText_widget = widgets.BoundedIntText(
min=0,
max=90,
step=1,
description='Surface Tilt (in °)',
disabled=False
)
RadioButtons_widget = widgets.RadioButtons(
options=['South', 'East-West'],
description='Orientation',
disabled=False
)
print(BoundedIntText_widget.value , RadioButtons_widget.value)

Passing variable names into fre command

I'm trying to pass set of variables into select input, and use its names to expss::fre command.
I have object with column names and its labels like 'myvars':
a1_1, "Do you like apples?"
a1_2, "Do you like oranges?"
I'm using them:
selectInput(vars, "Select variable", myvars)
Then, in 'server' section i would like to use it for generating simple frequency table.
output$view <- renderTable( {
fre(input$variable)
}
The problem is that in fre I have to pass variable with dataset name:
fre(data$a1_1)
So I tried with eval, quo, !!, paste0("data$",input$vars) and more, but didn't succeed.
when I try with switch:
switch(input$vars, "a1_1"=fre(data$a1_1), "a1_2"=fre(data$a1_2))
It works fine, but I need more flexible solution.
How to do this well?
The following code should do the trick:
output$view <- renderTable({
fre(data[[input$vars]])
})

How to create R functions with private variables?

How do I create a set of R functions that all access the same private variable?
Let's say I want to create readSetting(key) and writeSetting(key,value) functions that both operate on the same hidden list settings. If I try it like so...
local( {
settings <- list()
readSetting <<- function ( key ) settings[[key]]
writeSetting <<- function ( key, value ) settings[[key]] = value
} )
...then readSetting and writeSetting are not visible outside of the local call. If I want them to be visible there, I have to first assign
readSetting <- writeSetting <- NULL
outside the local call. There must be a better way, because my code isn't DRY if I have to say in two different ways which variables are public.
(The context of this work is that I'm developing an R package, and this code will be in an auxiliary file loaded into the main file via source.)
This question is related to How to limit the scope of the variables used in a script? but the answers there do not solve my problem.
You can simulate somthing like that using R6Class package and the following very rough code:
Privates <- R6Class("Privates",
public=list(
readSetting = function(key) {
private$settings[[key]]
},
writeSetting = function(key,value) {
private$settings[[key]] <<- value
}
),
private=list(
settings = list()
)
)
a <- Privates$new()
a$writeSetting("a",4)
a$readSetting("a")
Directly reading o setting the a$setting would not work.

R & gwidgets2 - Collecting values from gRadio widget

My problem is very basic (I am a beginner user in R). I am trying to collect the value selected from a gradio widget (gwidgets2 package for R).
I am using a similar script as this simplified one :
U=vector(mode="character")
DF=function() {
Win=gbasicdialog(handler=function(h,...) {
T=svalue(A)
print(T)
# I can print but not assign the value using : assign (U,T, .GlobalEnv)
})
A<-gradio(c("1","2","3"), selected=1,container=Win,)
out <- visible(Win)
}
DF()
Using this script, I am able to print the value selected in the gradio widget, but when I try to assign this value to another variable passed to the global environment, I get an error.
It is strange as this structure of script works fine to collect values from other widgets (like gtable). What am I doing wrong ?
Thanks for the help.
I am not sure what goes wrong, but was able to run your code with a small change:
DF <- function() {
Win <- gbasicdialog(
handler = function(h, ...) {
.GlobalEnv$varT = svalue(A)
print(varT)
}
)
A <- gradio(c("1", "2", "3"), selected = 1, container = Win)
out <- visible(Win)
}
DF()
A small advice: avoid using the single letters T or F, as in your code T might be interpreted as TRUE and not object T.

Assigning namespace variables inside of a function

I'm struggling to assign a namespace variable inside of a function. Consider this example using the CRAN package "qcc": qcc() generates a plot, but
the display options of that plot are controlled by qcc.options().
When working in global, everything is fine:
library(qcc)
qcc.options(bg.margin="red") # sets margin background colour, i.e.
# qcc:::.qcc.options$bg.margin is "red"
qcc(rnorm(100), type = "xbar.one") # generates a plot with red margin
But when working in the local environment of a function, qcc and qcc.options seem to use the namespace differently:
foo <- function(x){
qcc.options(bg.margin=x)
qcc(rnorm(100), type = "xbar.one")
}
foo("green") # generates a default plot with grey margins
Here is an ugly hack:
foo <- function(x){
old.qcc.options <- get(".qcc.options", asNamespace("qcc"))
assign(".qcc.options", qcc.options(bg.margin=x), asNamespace("qcc"))
res <- qcc(rnorm(100), type = "xbar.one")
assign(".qcc.options", old.qcc.options, asNamespace("qcc"))
invisible(res)
}
foo("green")
Of course, the scoping issues would be better solved by changing qcc.options. You should contact the package maintainer about that.
This is because of where qcc.options stores its .qcc.options variable. Working in global, this is qcc:::.qcc.options, but when you're inside a function, it is storing it in a local variable just called .qcc.options, thus when you try to use plot.qcc (called by qcc) it retrieves options from the global (non-exported) qcc:::.qcc.options rather than the local .qcc.options.
Here's a function that shows what's happening with the options:
bar <- function(x){
pre <- qcc:::.qcc.options
pre.marg <- qcc.options("bg.margin")
qcc.options(bg.margin=x)
post1 <- qcc:::.qcc.options
post2 <- .qcc.options
post.marg <- qcc.options("bg.margin")
qcc(rnorm(100), type = "xbar.one")
list(pre,post1,post2,pre.marg,post.marg)
}
bar('green')
If you look at the results, you'll see that qcc.options creates the local variable and changes its value of bg.margin to "green" but this isn't the object that's subsequently referenced by plot.qcc.
Seems like you should request some code modifications from the package maintainer because this is probably not the best setup.
EDIT: A workaround is to use assignInNamespace to use the local variable to overwrite the global one. (Obviously, this then changes the parameter globally and would affect all subsequent plots unless the parameter is updated.)
foo <- function(x){
qcc.options(bg.margin=x)
assignInNamespace('.qcc.options',.qcc.options,ns='qcc')
qcc(rnorm(100), type = "xbar.one")
}
foo('green')

Resources