【发布时间】:2022-01-22 20:29:44
【问题描述】:
我试图通过将函数的输出存储在列表中然后调用列表值来输出图表和数据框,但我的图表没有显示并且没有错误消息。我创建了一个虚拟示例来展示我的问题。
这是我的代码
library(shiny)
ui <- fluidPage(
titlePanel("title"),
sidebarLayout(
sidebarPanel(
checkboxInput("EF", "Efficient Frontier"),
actionButton("Go", "Go", style="color: #fff; background-color: #337ab7; border-color: #2e6da4; margin: auto")
),
mainPanel(
fluidRow(
align = "center",
plotOutput("GraphEF")),
tableOutput("EFWeightsTable")
)
)
)
server <- function(input, output) {
OPw <- reactiveValues()
observeEvent(input$Go, {
if(input$EF){
showModal(modalDialog("Loading... Please Wait", footer=NULL))
OPw$LIST1 <- X(5,10,20)
}
removeModal()
})
output$GraphEF <- renderPlot({
OPw$LIST1[[1]]
},height = 550, width = 700)
output$EFWeightsTable <- renderTable({
OPw$LIST1[[2]]}, colnames = TRUE
)
#Function
X <- function(a,b,c){
PLOT1 <- plot(c(1,2),c(3,4), type="l")
DF1 <- data.frame(c(1,2),c(3,4))
return(list(c(PLOT1,DF1)))
}
}
shinyApp(ui = ui, server = server)
非常感谢您的帮助,谢谢
【问题讨论】:
-
plot没有返回值。return(list(PLOT1,DF1))。您不需要在两者之间使用c,您将获得NULL用于plot -
我的意思是,如果你创建一个对象
out <- plot(1, 1); out NULL,plot 是没有返回值的,所以它是NULL。我之前的评论是关于以正确的方式创建列表,即list(PLOT1, DF1)而不是c(PLOT1, DF1)- (这导致将'DF1'的结构更改为列列表而不是data.frame -
您可以使用
ggplot并将其存储为对象,而不是基本 R 图 -
啊,我明白了。是的,我会尝试使用 ggplot 来代替。我认为也可以存储常规地块。谢谢