1

I've used R Optim function to generate simulated annealing output. And output values are printed in the console. Now I want to develop R Shiny application which prints this output while running the simulation.

Is there a possible way to get this output into the ui.R?

Thisara Watawana
  • 344
  • 4
  • 15
  • 1
    Please provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Based on your question I'd say you need the [textOutput](https://shiny.rstudio.com/reference/shiny/latest/textOutput.html) ui element. In the server function the last argument should be something like `paste0(optim(foo))` or just `optim(foo)`. Don't use `print` instead of `paste` because that will only print to the console. – Juergen Jul 03 '18 at 09:51

1 Answers1

1

You only need to use reactive on server and then return the text with renderPrint or renderText. See the example:

library(shiny)

fr <- function(x) {   ## Rosenbrock Banana function
  x1 <- x[1]
  x2 <- x[2]
  100 * (x2 - x1 * x1)^2 + (1 - x1)^2
}

# Define UI for application that draws a histogram
ui <- fluidPage(
  titlePanel("Sim Values"),

  sidebarLayout(
    sidebarPanel(
      sliderInput("range", 
                  label = "Initial values for the parameters to be optimized over:",
                  min = -5, max = 5, value = c(-5, 5))

    ),
    mainPanel(
      textOutput("optim_out"),
      textOutput("optim_out_b")
    )
  )
)

# Define server logic required to draw a histogram
server <- function(input, output) {
   out <- reactive(optim(c(input$range[1], input$range[2]), fr))
   output$optim_out <- renderPrint(out())
   output$optim_out_b <- renderText(paste0("Par: ", out()$par[1], " ", out()$par[2], " Value: ", out()$value))
}

# Run the application 
shinyApp(ui = ui, server = server)