Shiny 从列表中绘制动态图

Shiny 从列表中绘制动态图,shiny,dygraphs,shiny-reactivity,Shiny,Dygraphs,Shiny Reactivity,我希望通过选择每个列表的名称,从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(rn

我希望通过选择每个列表的名称,从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)

d您的代码中有四个错误:

  • selectInput()
    中,必须对前两个参数使用引号,这两个参数对应于
    inputId
    name

  • 您不能在
    服务器中两次使用
    输出$plot
    <代码>绘图
    必须是唯一的id,因此您可以有
    output$plot1
    output$plot2
    。这意味着您还需要在
    ui
    部分中有两个
    dygraphhoutput
    (或
    plotOutput
    ,或…)

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

  • renderDygraph
    (或
    renderPlot
    ,或…)中,仍然需要放置代码来创建绘图,就像它是在常规R中而不是R中一样

因此,您更正的代码为:

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)
图书馆(动物园)
图书馆(动态图)
图书馆(xts)
图书馆(闪亮)

d您的代码中有四个错误:

  • selectInput()
    中,必须对前两个参数使用引号,这两个参数对应于
    inputId
    name

  • 您不能在
    服务器中两次使用
    输出$plot
    <代码>绘图
    必须是唯一的id,因此您可以有
    output$plot1
    output$plot2
    。这意味着您还需要在
    ui
    部分中有两个
    dygraphhoutput
    (或
    plotOutput
    ,或…)

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

  • renderDygraph
    (或
    renderPlot
    ,或…)中,仍然需要放置代码来创建绘图,就像它是在常规R中而不是R中一样

因此,您更正的代码为:

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)
图书馆(动物园)
图书馆(动态图)
图书馆(xts)
图书馆(闪亮)
D