Dynamic naming of viewofs?

Is there a way to achieve something like this? The following code throws the error Assigning to rvalue. I’m working in a Quarto document.

function test(name){

   viewof name = Inputs.checkbox(
     ["test1", "test2"]
   )

}

test("checkbox_name")

No, viewof can only be used as prefix to a cell name when declaring or referencing a cell. What is your use case?

1 Like

Thanks Mootari.

The use case is that at my work when we receive a new dataset, my colleagues like it when I quickly provide some interactive bar charts and histograms for exploration. The format for these visualizations is generally:

  • A picklist to select the variable of interest;
  • Some checkboxes to filter by cohort or some other grouping variable;
  • The resulting chart.

Reminder that I’m working in a Quarto document.

I usually create a few of these plots, each corresponding to a different group of variables (for example maybe one for exploring baseline covariates, another for exploring outcome values, etc). Since I find myself doing this so often, I thought it would be nice to have a function that generates all 3 of these pieces (the picklist, checkboxes, and chart) simultaneously for a particular input dataset, rather than needing to manually define the new viewofs and modify the code to filter the plotted dataset based on the values of those viewofs.

You can’t declare variable names dynamically in JavaScript. The closest thing you could do is something like this:

function newCheckbox() {
  return Inputs.checkbox(["test1", "test2"]);
}
viewof checkbox_name = newCheckbox()

If you want to declare a set of inputs, which you could also wrap in a helper factory function as above, see Inputs.form: