打印 R 优化功能输出在 R 闪亮



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

有没有办法将此输出放入 ui。R?

您只需要在服务器上使用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)

最新更新