在R Shiny中打印R optim函数输出

时间:2018-07-03 09:30:27

标签: r shiny

我已经使用R Optim函数来生成模拟退火输出。输出值将打印在控制台中。现在,我想开发R Shiny应用程序,该应用程序在运行模拟时会打印此输出。

是否有可能将输出输出到ui.R中?

1 个答案:

答案 0 :(得分:1)

您只需要在服务器上使用reactive,然后使用renderPrintrenderText返回文本。参见示例:

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)