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 repr oducible ex ample (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:
A minimal reproducible example consists of the following items:
A minimal dataset, necessary to reproduce the issue
The minimal runnable code necessary to reproduce the issue, which can be run
on the given dataset, and including the necessary information on the used packages.
Let's quickly go over each one of these with examples:
Minimal Dataset (Sample Data)
You need to provide a data frame that is small enough to be (reasonably) pasted on a post, but big enough to reproduce your issue.
Let's say, as an example, that you are working with the iris data frame
head(iris)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 5.1 3.5 1.4 0.…
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())
})
```
system
Closed
November 17, 2021, 8:43am
6
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.