【问题标题】:plot dygraph from a list in shiny从闪亮的列表中绘制 dygraph
【发布时间】:2021-01-31 19:57:44
【问题描述】:

我希望通过选择每个列表的名称从 xts 系列列表中绘制 xts。 我不理解反应性和列表选择的工作原理

library(zoo)
library(dygraphs)
library(xts)

d <- seq(as.Date("2020/01/01"), as.Date("2020/05/01"), "months")
xts1 <- xts(rnorm(5),order.by = d)
xts2 <- xts(rnorm(5),order.by = d)
xts3 <- xts(rnorm(5),order.by = d)
l <- list(xts1,xts2,xts3)
names(l) <- c("uno","dos","tres")

创建 xts 对象列表


library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(

    # Application title
    titlePanel("Old Faithful Geyser Data"),

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
            selectInput(names,names,names(l))
        ),

        # Show a plot of the generated distribution
        mainPanel(
            dygraphOutput("plot")
        )
    )
)

# Define server logic required to draw a histogram
server <- function(input, output) {
      
    #option 1 
        p <- reactive({
        input$names
    })
   output$plot <- renderDygraph({
        l[[p]]
})

# option 2

    output$plot <- renderDygraph({
        l[[input$names]]
    })
}

# Run the application 
shinyApp(ui = ui, server = server)

这两种方式都行不通。 欣赏:)

【问题讨论】:

    标签: r shiny dygraphs shiny-reactivity


    【解决方案1】:

    您的代码中有四处错误:

    • selectInput() 中,您必须为前两个参数使用引号,它们对应于inputIdname

    • 您不能在server 中使用两次output$plotplot 必须是一个唯一的 id,所以你可以有 output$plot1output$plot2 例如。这意味着您还需要在ui 部分中有两个dygraphOutput(或plotOutput,或...)。

    • 当你定义一个reactive()时,你必须在之后调用它时使用括号,例如p()而不是p

    • renderDygraph(或renderPlot,或...)中,您仍然需要输入代码来创建绘图,就好像它是在常规 R 中而不是 R Shiny 中一样。

    因此,您更正的代码是:

    library(zoo)
    library(dygraphs)
    library(xts)
    library(shiny)
    
    d <- seq(as.Date("2020/01/01"), as.Date("2020/05/01"), "months")
    xts1 <- xts(rnorm(5),order.by = d)
    xts2 <- xts(rnorm(5),order.by = d)
    xts3 <- xts(rnorm(5),order.by = d)
    l <- list(xts1,xts2,xts3)
    names(l) <- c("uno","dos","tres")
    
    
    # Define UI for application that draws a histogram
    ui <- fluidPage(
      
      # Application title
      titlePanel("Old Faithful Geyser Data"),
      
      # Sidebar with a slider input for number of bins 
      sidebarLayout(
        sidebarPanel(
          selectInput("names", "names", names(l))
        ),
        
        # Show a plot of the generated distribution
        mainPanel(
          dygraphOutput("plot1"),
          dygraphOutput("plot2")
        )
      )
    )
    
    # Define server logic required to draw a histogram
    server <- function(input, output) {
      
      p <- reactive({
        input$names
      })
      output$plot1 <- renderDygraph({
        dygraph(l[[p()]])
      })
    
      output$plot2 <- renderDygraph({
        dygraph(l[[input$names]])
      })
    }
    
    # Run the application 
    shinyApp(ui = ui, server = server)
    

    【讨论】:

      猜你喜欢
      • 2015-07-22
      • 2021-06-19
      • 2016-07-25
      • 2020-08-10
      • 2017-02-13
      • 1970-01-01
      • 2019-05-20
      • 2017-01-23
      • 1970-01-01
      相关资源
      最近更新 更多