我試圖通過將函式的輸出存盤在串列中然后呼叫串列值來輸出圖形和資料框,但我的圖形沒有顯示并且沒有錯誤訊息。我創建了一個虛擬示例來展示我的問題。
這是我的代碼
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)
非常感謝您的幫助,謝謝
uj5u.com熱心網友回復:
您的代碼中有一些錯誤 - 這是 Base R Plot 的解決方案。代碼包括詳細注釋,需要解釋正在發生的事情。
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(
{
# Here it seem OP use the wrong reference [[1]] where it should be [[2]]
OPw$LIST1[[2]]
}, colnames = TRUE
)
#Function
X <- function(a,b,c){
# Base R plot doesn't return a value but will always output to panel plot
plot(c(1,2),c(3,4), type="l")
# To save it to object for later use recordPlot() function after plot function
PLOT1 <- recordPlot()
DF1 <- data.frame(c(1,2),c(3,4))
# Here only use list(...) instead of list(c(...))
# c(...) convert all objects passed to it into vector - you don't want that.
return(list(PLOT1,DF1))
}
}
這是輸出

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/420230.html
標籤:
上一篇:如何使原始串列通過函式并進行修改,但不回傳新串列而是添加到舊串列中?
下一篇:解釋輸出會是什么
