gpt4 book ai didi

r - 使用有光泽的 dygraph

转载 作者:行者123 更新时间:2023-12-02 21:16:49 25 4
gpt4 key购买 nike

我最近开始使用 dygraph,到目前为止我非常喜欢它。我曾尝试使用它来 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

使用品种和计数变量,我想绘制价格随时间变化的图表。

这是我的 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 中的 renderUI({...})ui.R 中的 uiOutput(...)

# 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"))
)))

现在可以了 in shinyapps.io

关于r - 使用有光泽的 dygraph,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30176303/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com