Fitting reactive objects

I have a short question regarding the usage of reactive elements in a markdown file. I am familiar with python and tried to implement something python-like in R.

I wrote a class, that needs to be initialized in the first step (R6)
In a second step, I fit the model parameters. Without fitting, the classes methods will be empty.

So using Shiny in Markdown, I can create an reactive class object, but how is it fitted?

myClass_object<- reactive({obj$new(data = X)}) 

reactive(myClass_object(),{myClass_object()$fit()})

This approach is not working.

Any sugguestions? I still could not figure out how this could work.

Hi!

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

Hi, here is an example of what I would like to do:

```{r markdown setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE,
                      message = FALSE,
                      warning = FALSE)
```

```{r}
data <- data.frame(a = c(1,2,3),
                   b = c(4,5,6))
```

```{r}
inputPanel(
  actionButton("calculate", "Calculate"),
  actionButton("add", "Add")
)
```

```{r}
myObj <- eventReactive(input$calculate,{
  return(cbind(data, data))
})

myObj <- eventReactive(input$add,{
  return(cbind(myObj(), myObj()))
})
```

```{r}
DT::renderDT({
  DT::datatable(myObj())
})
```

As you can see, i have a reactive value "myObj", that I would like to change every time the second action button is clicked.

---
runtime: shiny
output: html_document
---

```{r markdown setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE,
                      message = FALSE,
                      warning = FALSE)
```

```{r}
data <- data.frame(a = c(1,2,3),
                   b = c(4,5,6))
```

```{r}
inputPanel(
  actionButton("calculate", "Calculate"),
  actionButton("add", "Add")
)
```

```{r}

myObj <- reactiveVal(NULL)

observeEvent(input$calculate,{
  myObj(cbind(data, data))
})

 observeEvent(input$add,{
  myObj(cbind(myObj(), myObj()))
})
```

```{r}
DT::renderDT({
  DT::datatable(myObj())
})
```

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.