1

I think I'm missing something with respect to reactives in my Shiny app. Here's a MRE that shows my problem printing y.

library(shiny)

ui <- fluidPage(
   titlePanel("Test"),
   textAreaInput("text", "Text", "", width = "400px"),
   verbatimTextOutput("text"),
   actionButton("do", "Run"),
   textOutput("result")
   )

server <- function(input, output) {
  observeEvent(input$do, {
  y <- reactive({
    x <- ""
    t <- 1
    while (t < 5) {
      x <- paste(input$text, x, sep=",")
      t <- t + 1
    }
  })
  })
  output$result <- renderPrint({y})
}

shinyApp(ui = ui, server = server)
2
  • in this basic example, if you type "help" into the input box text, I'd like to run and print x, which would be help,help,help,help,
    – Eric Green
    Oct 23, 2017 at 14:23
  • i'm creating this reactive y that is disconnected from what i really want, which is the iteratively pasted x
    – Eric Green
    Oct 23, 2017 at 14:24

2 Answers 2

5

You shouldn't put a reactive value inside of an observeEvent or observe call. In fact, it has been advised by Joe Cheng to never nest either observe or reactive functions within either themselves or the other. They are separate things used for different purposes. Since you want the reactive y to be created based on when input$do is clicked, you should use eventReactive:

server <- function(input, output) {
  y <- eventReactive(input$do, {
    x <- ""
    t <- 1
    while (t < 5) {
      x <- paste(input$text, x, sep=",")
      t <- t + 1
    }
    return(x)
  })

  output$result <- renderText({y()})
}

I changed renderPrint() to renderText() so that it displays your desired output. Also, since y is a reactive, you need to add y() after it in the renderText() call. I also added return(x) to the eventReactive call, otherwise t would be the value returned.

1
  • Ah, ok @tbradley. This is very helpful.
    – Eric Green
    Oct 23, 2017 at 14:33
1

I the problem is that your call to reactive() does not return anything. Wrapping an expression inside reactive assigns the return value of the expression to a variable each time a reactive value inside the expression is changed. Reactive values are usually all input$... variables, and those that you store in reactiveValues() objects.

If I get you right you want to change and print y every time the "run" button is hit. Save this to a reactiveValue() collection (similarly accessible like a list), and then put this inside your renderPrint function.

From your code I reckon that you want y to be the value of x after the while loop.

library(shiny)

ui <- fluidPage(
  titlePanel("Test"),
  textAreaInput("text", "Text", "", width = "400px"),
  verbatimTextOutput("text"),
  actionButton("do", "Run"),
  textOutput("result")
)

server <- function(input, output) {
  values <- reactiveValues()
  observeEvent(input$do, {
    x <- ""
    t <- 1
    while (t < 5) {
      x <- paste(input$text, x, sep=",")
      t <- t + 1
    }
    values$y <- x
  })
  output$result <- renderPrint({values$y})
}

shinyApp(ui = ui, server = server)
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.