【问题标题】:RShiny Generating Dropdown Menu for Plotly Charts - using subelementsR Shiny为绘图图表生成下拉菜单 - 使用子元素
【发布时间】: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。非常感激。我添加了一个可重现的示例 - 这有助于解释问题吗?

标签: r shiny plotly r-plotly


【解决方案1】:

根据您的 MWE,此代码不会将绘图保存在任何地方,而是使用 observeEvent 根据选择进行绘图。

library(shiny)
library(plotly)



ui <-shinyUI(fluidPage(selectInput("selectPlot", "Choose desired plot", choices=paste0("p", 1:2)), plotlyOutput("plot")))

server <- shinyServer(function(input,output){      

  observeEvent(input$selectPlot,{
    if(input$selectPlot %in% 'p1') output$plot <- renderPlotly(plot_ly(mtcars, x=~cyl, y=~gear))
    else output$plot <- renderPlotly(plot_ly(mtcars, x=~hp, y=~am))

  })


})

shinyApp(ui,server)

【讨论】:

  • 感谢您的回复@amrrs,非常感谢。不幸的是仍然没有运气。如果我在上面使用您的示例(此示例stackoverflow.com/questions/45255613/…)-假设 p1 和 p2 包含在列表中-例如列表 = ((dataset1, p1), (dataset2, p2))。您将如何从工作区访问这些子元素,以便它们显示在下拉菜单中?更具体地说,您如何 1) 在 UI 'choice' 参数中引用它们,以及 2) 使用 input$selectPlot 在 Shiny 服务器中访问它们?如果还有什么不清楚的,请告诉我!
【解决方案2】:

这样的东西对你有用吗:

p1 <- plot_ly(mtcars, x=~cyl, y=~gear)
p2 <- plot_ly(iris, x=~Sepal.Width, y=~Petal.Length, color = "red")

l = list(mtcars = list(mtcars, p1), iris = list(iris, p2))
choice_data <- names(l)

ui <- shinyUI(fluidPage(selectInput("selectPlot", 
                                    "Choose desired country", 
                                    choices = choice_data), textOutput("selected_var"), uiOutput("test")))


server <- shinyServer(function(input,output){   

  output$test <- renderUI({
    l[[input$selectPlot]][2]

  })

})

shinyApp(ui,server)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-07
    • 2017-03-26
    • 2019-10-29
    • 1970-01-01
    • 2014-07-26
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    相关资源
    最近更新 更多