如何在闪亮的多个输出中检索循环生成的变量
How to retrieve loop-generated variables across multiple outputs in shiny
使用 iris 数据,我 运行 在 r shiny 反应式表达式中循环生成下面的变量 species_table 我想访问的内容在随后的输出函数中。我在 this link 中关注了 guidance 但是当稍后调用 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)
您的 species_table
在其定义的反应式之外不可用。相反,return 一个命名列表,你会得到你想要的输出。试试这个
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)
使用 iris 数据,我 运行 在 r shiny 反应式表达式中循环生成下面的变量 species_table 我想访问的内容在随后的输出函数中。我在 this link 中关注了 guidance 但是当稍后调用 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)
您的 species_table
在其定义的反应式之外不可用。相反,return 一个命名列表,你会得到你想要的输出。试试这个
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)