0
我想要做的是使得for循環運行的輸出可用於Shiny App中的許多渲染輸出。 我創建了我的問題的簡單例子。 每個renderPrint()函數中的循環都是一樣的。 我可以用這種方式編寫代碼,以便for循環移出render *()函數之外嗎?R Shiny:可用於許多渲染*()函數的for循環輸出
我發現了一些例子說明如何在循環中使用反應物,但還沒有找到解決逆向任務的方法。 謝謝你的幫助和關注。
library(shiny)
ui <- fluidPage(sidebarLayout(
sidebarPanel(
numericInput(
inputId = "seed",
label = "Set seed:",
value = 1
),
numericInput(
inputId = "Number",
label = "Number:",
value = 1,
min = 1,
step = 1
)
),
mainPanel(
verbatimTextOutput("summary"),
dataTableOutput("table"),
verbatimTextOutput("data")
)
))
server <- function(input, output) {
a <- reactive({
set.seed(input$seed)
rnorm(input$Number, 2, 1)
})
b <- reactive({
5 * a()
})
rn <- reactive({
c(1:input$Number)
})
fun <- function(x, y) {
x + y
}
Table <- reactive({
data.frame(rn = rn(),
a = a(),
b = b())
})
output$table <- renderDataTable({
Table()
})
output$summary <- renderPrint({
for (i in Table()$rn) {
print (fun(Table()$a[i], Table()$b[i]))
}
})
output$data <- renderPrint({
for (i in Table()$rn) {
print (fun(Table()$a[i], Table()$b[i]))
}
})
}
shinyApp(ui = ui, server = server)