【发布时间】:2018-02-15 11:14:54
【问题描述】:
尝试创建一个闪亮的应用程序,该应用程序具有一个下拉菜单,允许您选择作为保存到全局环境的列表的子元素的图。
图位于列表中每个元素的第二个子元素中
e.g. list = ([[dataframe1, plot1], [dataframe2, plot2], etc])
我正在尝试创建的应用程序由以下人员提供:
choices = paste0("list[[1]][[", 1:2, "]]")
ui <- shinyUI(fluidPage(selectInput("selectPlot",
"Choose desired country",
choices),
plotlyOutput("plot")))
server <- shinyServer(function(input,output){
output$plot <- renderPlotly({
return(get(input$selectPlot))
})
})
shinyApp(ui,server)
但是,没有显示任何图,并且我收到以下错误: **警告:获取错误:找不到对象'anom[[1]][[1]]'*
如果我将图单独保存到环境中,那么这种方法有效。但是,我正在尝试通过这个已经存在的列表来访问这些图!
添加可重现的示例:
ds1 <- data.frame(sample(1:10, 10), sample(11:20, 10))
ds2 <- data.frame(sample(1:10, 10), sample(11:20, 10))
p1 = plot_ly(x = ~ds1[[1]], y = ~ds1[[2]]) %>% add_markers()
p2 = plot_ly(x = ~ds2[[1]], y = ~ds2[[2]]) %>% add_markers()
l = list(list(ds1, p1), list(ds2, p2))
#want to access p1 and p2 from within the list to create drop down menu graph
choices.p = paste0("l[[1]][[", 1:2, "]]")
ui <- shinyUI(fluidPage(selectInput("selectPlot",
"Choose desired country",
choices.p),
plotlyOutput("plot")))
server <- shinyServer(function(input,output){
output$plot <- renderPlotly({
return(get(input$selectPlot))
})
})
shinyApp(ui,server)
【问题讨论】:
-
阴谋!这是一个可重现的示例 - (stackoverflow.com/questions/45255613/…) - 不是将绘图保存到全局环境中 - 有没有办法从已经保存到环境中的列表中访问它们?
-
@MikeTauber 即使在您的情况下,如果您已将其保存在列表中,不是吗?不保存到全局变量意味着,您只希望它仅在需要时不保存,对吗?
-
@MikeTauber 请检查答案和反馈
-
@amrrs 感谢您的 cmets。非常感激。我添加了一个可重现的示例 - 这有助于解释问题吗?