簡體   English   中英

使用帶光筆

[英]using dygraph with shiny

我最近開始使用音筆,到目前為止我非常喜歡。 我曾嘗試將其用於光澤效果不佳的產品。 盡管我的腳本不會產生任何錯誤,但也不會產生任何圖形!
您是否有機會向正確的方向指引我?

這是我的數據樣本:

> head(df2)
        date     Variety   Count Price Value Quantity TotalKg
1 2014-11-06 CRIPPS PINK   80-90   204  3670       18     333
2 2014-11-06 CRIPPS PINK 120-135   181 10150       56    1036
3 2014-11-06  CRIPPS RED   80-90   221 26910      122    2257
4 2014-11-06  CRIPPS RED 100-110   205 22910      112    2072
5 2014-11-06  CRIPPS RED 120-135   193 58950      306    5661
6 2014-11-06      TOPRED   80-90   167  7350       44     814

使用Variety和Count變量,我想繪制一段時間內的價格。

這是我的ui.R

library(dygraphs)
library(shiny)

shinyUI(fluidPage(
  titlePanel("Apples Prices"),

  sidebarLayout(
          sidebarPanel(
            selectInput("productname", "Select your product",
                        choices = levels(df2$Variety)),
            selectInput("count",  "Select your size",
                        choices = levels(df2$Count))),

            mainPanel(
              dygraphOutput("applesgraph"))
          )))

和服務器端:

library(shiny)
library(dygraphs)
library(dplyr)
library(xts)

shinyServer(function(input, output) {

  dfa <- reactive({df2 %>% filter(Variety == input$productname & 
                                    Count == input$count )})

#the first graph which is price over time (input: variety, count, date)
  output$applesgraph <- renderDygraph({
   xts(dfa()$Price, order.by = dfa()$date) %>% dygraph()
  })
})

我可以感覺到我對dplyr和時間序列對象使用了錯誤的方法...那么我什么時候應該過濾呢? 我嘗試了許多組合,但是后來總會出現諸如“不可子集化”的錯誤。

預先感謝您能給我的任何指導。

由於您需要在server.R (用於圖形)和ui.R (用於輸入列表)中輸入數據, server.R我在server.RuiOutput(...)添加了renderUI({...}) uiOutput(...)ui.R

# server.R
library(shiny)
library(dygraphs)
library(dplyr)
library(xts)

shinyServer(function(input, output) {
  data <- read.csv("cleanApples.csv") %>%
    filter(Quantity > 10)

  #the first graph which is price over time (input: variety, count, date)
  output$applesgraph <- renderDygraph({
    if (is.null(input$productname) || is.null(input$count)) return(NULL)
    filtered <- filter(data,
                       Variety == input$productname,
                       Count == input$count )
    xts(filtered$Price, as.Date(filtered$date, format = "%Y-%m-%d")) %>%
      dygraph()
  })

  output$productnames <- renderUI({
    selectInput("productname", "Select your product",
                choices = levels(data$Variety))
  })

  output$counts <- renderUI({
    selectInput("count",  "Select your size",
                choices = levels(data$Count))
  })
})

# ui.R
library(dygraphs)
library(shiny)

shinyUI(fluidPage(
  titlePanel("Apples Prices"),

  sidebarLayout(
    sidebarPanel(
      uiOutput("productnames"),
      uiOutput("counts")
    ),

    mainPanel(
      dygraphOutput("applesgraph"))
  )))

現在可以在shinyapps.io中使用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM