使用 iris 資料,我在 r 閃亮的反應式運算式中運行一個回圈,以生成我想在后續輸出函式中訪問的以下變數species_table內容。我已按照此鏈接中的指導進行操作,但稍后在呼叫species_function函式時,我收到一條錯誤訊息,提示找不到species_table。如何檢索輸出函式中的species_table內容以顯示在 ui 中?
library(shiny)
data(iris)
ui <- fluidPage(
mainPanel(
fluidRow(selectInput("portfolio", label = "Select Species", choices = unique(iris$Species))),
fluidRow(tableOutput("result")))
)
server <- function(input, output) {
species_list <- reactiveValues(a = "setosa", b = "versicolor", c = "virginica")
species_function <- reactive({
species_table <- list()
for (i in reactiveValuesToList(species_list)){
local({
j <- i
species_table[[reactive({j})()]] <<- iris[iris$Species==reactive({j})(),]
})
}
print(species_table[[input$portfolio]])
return(list(species_table[["setosa"]], species_table[["versicolor"]], species_table[["viginica"]]))
})
output$result <- renderTable({
species_table2 <- list()
for (p in reactiveValuesToList(species_list)){
local({
q <- p
species_table2[[reactive({q})()]] <<- species_function()[[species_table]][[reactive({q})()]][1:5, c("Sepal.Length", "Sepal.Width")]
})
}
print(species_table2[[input$portfolio]])
return(species_table2[[input$portfolio]])
})
}
shinyApp(ui = ui, server = server)
uj5u.com熱心網友回復:
您species_table在定義它的反應之外不可用。相反,回傳一個命名串列,你會得到你想要的輸出。試試這個
library(shiny)
data(iris)
ui <- fluidPage(
mainPanel(
fluidRow(selectInput("portfolio", label = "Select Species", choices = unique(iris$Species))),
fluidRow(tableOutput("result"))
)
)
server <- function(input, output) {
species_list <- reactiveValues(a = "setosa", b = "versicolor", c = "virginica")
species_function <- reactive({
species_table <- list()
for (i in reactiveValuesToList(species_list)){
local({
j <- i
species_table[[j]] <<- iris[iris$Species==j,]
})
}
#print(rv$species_table[[input$portfolio]])
return(list("setosa" = species_table[["setosa"]],"versicolor" = species_table[["versicolor"]],"virginica" = species_table[["virginica"]]))
})
output$result <- renderTable({
input$portfolio
species_table2 <- list()
for (p in reactiveValuesToList(species_list)){
local({
q <- p
species_table2[[reactive({q})()]] <<- species_function()[[reactive({q})()]][1:5, c("Sepal.Length", "Sepal.Width")]
})
}
print(species_table2[[input$portfolio]])
return(species_table2[[input$portfolio]])
})
}
shinyApp(ui = ui, server = server)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/465270.html
上一篇:在R中的for回圈內分組
