Output with a variable name.

This is part of server.R:

counter <- reactiveValues(n = 0)
  
output$counter <- renderPrint(print(counter$n))
  
observeEvent(input$addRow, {
  if (counter$n < 5)
    counter$n <- counter$n + 1
})
observeEvent(input$delRow, {
  if (counter$n > 0)
    counter$n <- counter$n - 1
})

output[[paste0("additional_row_", counter$n)]] <-  renderUI({ inputs() }) #for example, inputs is an h4("Hello"). It really is a couple of UI elements.. But first let's make it to work with only 1

And ui.R:

uiOutput("additional_row_1"),
uiOutput("additional_row_2"),
uiOutput("additional_row_3"),
uiOutput("additional_row_4"),
uiOutput("additional_row_5"),
actionButton("addRow", "Add Row"),
actionButton("delRow", "Delete Row"),
uiOutput("counter"),

The error says:

Error in .getReactiveEnvironment()$currentContext() : Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)

And I understand it, because I try to compute the name of an output. But how then can I add rows and delete rows of UI elements in my app?

I appreciate your help. Thank you very much

Hi Geiser,

This might help you:

https://shiny.rstudio.com/reference/shiny/1.0.1/insertUI.html

Inserting this function in an observEvent, so a reactive context will prevent you from getting this error.

Additionally there is a removeUI function. If you need more complex applied examples, there are plenty on the web.

1 Like

Thank you so much! Even not the actual answer, because the solution to the question was to add the insertion of the UI inside the observeEvents of the "input$addRow" and "input$delRow", your function saved me because the renderUI() function made a mess to all the dynamic inputs when adding rows. Thank you very much!

1 Like

This topic was automatically closed 7 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.