r shiny 在列表中存储图和数据框
r shiny storing plot and data frame in list
我试图通过将函数的输出存储在列表中然后调用列表值来输出图形和数据框,但我的图形没有显示,也没有错误消息。我创建了一个虚拟示例来展示我的问题。
这是我的代码
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)
非常感谢您的帮助,谢谢
您的代码中存在一些错误 - 这是使用 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))
}
}
这是输出
我试图通过将函数的输出存储在列表中然后调用列表值来输出图形和数据框,但我的图形没有显示,也没有错误消息。我创建了一个虚拟示例来展示我的问题。
这是我的代码
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)
非常感谢您的帮助,谢谢
您的代码中存在一些错误 - 这是使用 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))
}
}