Reactive DateRangeInput为Shiny [英] Reactive DateRangeInput for Shiny

查看:232
本文介绍了Reactive DateRangeInput为Shiny的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在教自己Shiny,而且我被卡在ggplot2图表上,无法使用被反应的dateRangeInput作为我的x轴。我有几个问题:


  1. 有没有办法使用我的数据框来获取日期范围输入的最小值,最大值而不是必须对它们进行硬编码,以便在向数据框中添加更多推文时,我不必每次都对这些值进行硬编码? 我得到错误:美学必须是长度为1或与数据(33108)相同:x,y 当我尝试使用输入$ date作为我的aes(x =输入$ date ...

library(shiny)library(tidyr)library ggplot2)tweets< - read.csv(file.choose())colnames(tweets)[1]< - Contenttweets< - separate(tweets,created_at,c(Date,Time (日期范围,日期范围,sep =)tweets $ Date <-as.Date(tweets $ Date,%m /%d /%Y)ui < - fluidPage(dateRangeInput(inputId =date ) start =2009-05-04,end =2018-02-28,min =2009-05-04,max =2018-02-28),plotOutput(Graph))server< ;函数(输入,输出){output $ Graph< - renderPlot({ggplot(tweets,aes(x = input $ date,y = count))+ geom_bar(stat =identity,position =stack) + #scale_y_continuous(name =Retweet Count,limits = c(0,370000),breaks = seq(0,370000,10000))+ theme(panel.background = element_rect(fill =white,color =grey50 ))})} shinyApp(ui = ui,server = server)

解决方案

@ Pete900的答案总结了 updateDateRangeInput 的用法,有关更多信息,可以参考这部分的闪亮文档。



关于你的第二个问题: input $ date 将返回一个长度为2的向量,第一个元素第二个是选定范围的上半部分。您很可能不会直接将此用作x美学,而是将您的数据与此集合进行分组,然后绘制新的子集数据。你可以例如写

  library(dpylr)#alternatevly library(tidyverse)
newtweets< - reactive({
过滤器(在(日期,输入$ date [1],输入$ date [2]))
})

$ b $之间的推文b

然后,在你的ggplot中,使用 newtweets()作为你的数据。



更新
()之间的函数过滤器因为x大于...小于那么...)来自包 dplyr ,这对于处理数据框和处理包的集合的一部分非常有用与 tidyverse 相互很好,(请参阅此处 )。



当你引用新创建的反应对象 newtweets()时,请确保不要忘记该名词因为它现在是一个函数调用,它可以在输入发生变化时使闪烁更新数据框。

Upd ate

一个完整的工作示例,我创建了一些人工数据:

  library(shiny)
library(tidyverse)
library(lubridate)

tweets< - read.csv(file.choose())

st < - ymd(2009-05-01)
en< - ymd(2018-02-28)
日期< - seq.Date (from = st,to = en,by = 1)
tweets< - tibble(date = dates,count = rnorm(length(dates),mean = 5,sd = 3))


ui< - fluidPage(
dateRangeInput(inputId =date,
strong(Date Range),
start =2009-05-04 ,end =2018-02-28,
min =2009-05-04,max =2018-02-28),
plotOutput(Graph)


server < - function(input,output){

newtweets < - reactive({
filter(tweets,between(date,input $ date输入$ date [2]))
})

输出$ Graph< - renderPlot({
ggplot(newtweets(),aes(x = date,y = count))+
geom_bar(stat =identity,position =stack)+
#scale_y_continuous(name =Retweet Count,limits = c(0,370000) ,break = seq(0,370000,10000))+
theme(panel.background = element_rect(fill =white,color =grey50))
})
}

shinyApp(ui = ui,server = server)


I'm teaching myself Shiny and I am stuck on my ggplot2 graph not being able to use the reactive dateRangeInput as my x-axis. I have a few questions:

  1. Is there a way to use my data frame to grab the min, max values for date range input instead of having to hardcode them in so that when I add more tweets to the data frame I don't have to hardcode the values each time?
  2. I am getting the error: Aesthetics must be either length 1 or the same as the data (33108): x, y when I try to use input$date as my aes(x = input$date...

library(shiny)
library(tidyr)
library(ggplot2)

tweets <- read.csv(file.choose())
colnames(tweets)[1] <- "Content"
tweets <- separate(tweets, created_at, c("Date", "Time"), sep = " ")
tweets$Date <-as.Date(tweets$Date, "%m/%d/%Y")

ui <- fluidPage(
  dateRangeInput(inputId = "date", 
              strong("Date Range"),
              start = "2009-05-04", end = "2018-02-28",
              min = "2009-05-04", max ="2018-02-28" ),
  plotOutput("Graph")
)

server <- function(input, output) {
  output$Graph <- renderPlot({
    ggplot(tweets, aes(x = input$date, y = count)) + 
      geom_bar(stat = "identity", position = "stack") +
      #scale_y_continuous(name = "Retweet Count", limits = c(0,370000), breaks=seq(0,370000,10000)) +
      theme(panel.background = element_rect(fill = "white", colour = "grey50")) 
  })
}

shinyApp(ui = ui, server = server)

解决方案

@Pete900's answer summarizes the use of updateDateRangeInput well, for further information you can refer to this part of the shiny documentation.

About your second problem: input$date will return a vector of length 2 with the first element beeing the lower and the second being the upper part of the selected range. You will most likely not use this directly as x-aesthetics but rather subset your data with this and then plot the newly subsettet data. You can e.g. write

library(dpylr) # alternatevly library(tidyverse)
newtweets <- reactive({
filter(tweets, between(date ,input$date[1], input$date[2]))
})

then, in your ggplot, use newtweets() as your data.

Update The functions filter and between() (which is a shortcut for x is greater than ... and lesser then ...) come fromt the package dplyr, which is great for working with dataframes and part of a collection of packages that play very nicely with each other called tidyverse (see here).

When you refer to the newly created reactive object newtweets(), make sure to not forget the paranthesis because it is now a function call, that enables shiny to update the dataframe should the input change.

Update

A full working example in which I create some artificial data:

library(shiny)
library(tidyverse)
library(lubridate)

# tweets <- read.csv(file.choose())

st <- ymd("2009-05-01")
en <- ymd("2018-02-28")
dates <- seq.Date(from = st, to = en, by = 1)
tweets <- tibble(date = dates, count = rnorm(length(dates), mean = 5, sd = 3))


ui <- fluidPage(
    dateRangeInput(inputId = "date",
                   strong("Date Range"),
                   start = "2009-05-04", end = "2018-02-28",
                   min = "2009-05-04", max ="2018-02-28" ),
    plotOutput("Graph")
)

server <- function(input, output) {

    newtweets <- reactive({
        filter(tweets, between(date ,input$date[1], input$date[2]))
    })

    output$Graph <- renderPlot({
        ggplot(newtweets(), aes(x = date, y = count)) +
            geom_bar(stat = "identity", position = "stack") +
            #scale_y_continuous(name = "Retweet Count", limits = c(0,370000), breaks=seq(0,370000,10000)) +
            theme(panel.background = element_rect(fill = "white", colour = "grey50"))
    })
}

shinyApp(ui = ui, server = server)

这篇关于Reactive DateRangeInput为Shiny的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆