时间参数不再受支持

时间:2018-09-06 08:17:38

标签: r shiny gtrendsr

我使用gtrendsR软件包有问题。创建示例Shiny APP时,我总是会收到错误消息:

gtrends中的错误:未使用的参数(start_date = start_date(),end_date = end_date())

开始和结束日期在当前包中不再存在。当前,开始时间和结束时间仅需要指定如下:

两个日期之间的时间跨度(例如:“ 2010-01-01 2010-04-03”)

如何更改功能?

非常感谢您的帮助。

完整的代码如下:

server.R:

    # Load libraries ====
if(!require(shiny)){
    install.packages('shiny')
      }
if(!require(gtrendsR)){
    install.packages('gtrendsR')
      }
if(!require(reshape2)){
    install.packages('reshape2')
      }
if(!require(ggplot2)){
    install.packages('ggplot2')
      }

library(shiny)
library(gtrendsR)
library(reshape2)
library(ggplot2)

data(countries)

# Start shiny application

shinyServer(function(input, output) {



  out <- reactive({
    if(length(input$terms)>0){

      unlist(strsplit(input$terms,","))
    }
  })

  start_date<-reactive({

    if(input$period=="2004-present"){as.Date("2004-01-01")}

    else if (input$period=="Past90Days"){as.Date(Sys.time())-90}

    else if (input$period=="Past12Months"){
      m=as.POSIXlt(as.Date(Sys.time()))
      m$year=m$year-1
      m}

    else if (input$period=="2011"){as.Date("2011-01-01")}
    else if (input$period=="2012"){as.Date("2012-01-01")}
    else if (input$period=="2013"){as.Date("2013-01-01")}
    else if (input$period=="2014"){as.Date("2014-01-01")}
    else if (input$period=="2015"){as.Date("2015-01-01")}



  })


  end_date<-reactive({

    if(input$period %in% c("2004-present",
                           "Past90Days","Past12Months"))
                 {
      as.Date(Sys.time())}

    else if (input$period=="2011"){as.Date("2011-12-31")}
    else if (input$period=="2012"){as.Date("2012-12-31")}
    else if (input$period=="2013"){as.Date("2013-12-31")}
    else if (input$period=="2014"){as.Date("2014-12-31")}
    else if (input$period=="2015"){as.Date(Sys.time())} 

    })

geo<-reactive({
  if(input$geography=="Worldwide"){""}

  else{

    countries$CODE[countries$COUNTRY==input$geography]
  }

})

  data<-reactive({
    if(length(out()>0))
    {

      out2<-gtrends(query=out(),start_date=start_date(),end_date=end_date(),geo=geo())

    }

  })




  output$myplot <- renderPlot({
    if(length(out()>0)){
      z=data()
      trend=z$trend

      if("end"%in%names(trend)==T)
                           {
      trend=select(trend,-end)}

      trend <- melt(trend, id='start')

      ggplot(trend, aes(start,value, color=variable)) + geom_line()+ggtitle("Interest over time")+
        ylab("Relative Trend")+
        theme(plot.title = element_text(size = 18,colour="black"))+
        xlab('')+theme(axis.title.y = element_text(colour="#00007A",size=14,angle=90,hjust=.5,vjust=1),
                       axis.text.y = element_text(colour="darkred",size=14,angle=0,hjust=1,vjust=0),
                       axis.text.x = element_text(colour="darkred",size=14,angle=0,hjust=1,vjust=0))+
        theme(legend.title = element_text(colour="black", size=15, 
                                          face="bold"))+
        theme(legend.text = element_text(colour="blue", size=14, 
                                         face="bold"))

    }

  })


 corr<-reactive({

   if(input$corr==T & length(out()>1)){

     z=data()
     trend=z$trend
     trend=trend[,3:ncol(trend)]
     cor(trend)

   }
 }) 


 output$myplot3 <- renderPlot({
   if(length(corr()>0)){
     data=corr()

     qplot(x=Var1, y=Var2, data=melt(cor(data)), fill=value, geom="tile")+
       ggtitle('Correlation Matrix')+theme(axis.title.y =element_blank(),axis.title.x =element_blank(),
                                           axis.text.y = element_text(colour="darkred",size=14,angle=0,hjust=1,vjust=0),
                                           axis.text.x = element_text(colour="darkred",size=14,angle=0,hjust=1,vjust=0))+
       theme(legend.title=element_blank())+
       theme(legend.text = element_text(colour="black", size=14))+scale_fill_gradient2(limits=c(-1, 1),low="skyblue", high="blue")+
       theme(plot.title = element_text(size = 20,colour="black"))
   }
 })


  output$myplot2 <- renderPlot({
    if(length(out()>0)){
      data=data()


      z=data$searches
      rr=data$regions

      for (i in 1:length(z)){
        n=z[i]
        n=as.data.frame(n)
        names(n)=c("searches","hits")
        n$searches <- factor(n$searches, levels = n$searches[order(n$hits,decreasing =T)])

        colors=c("orange","skyblue","#999966")

        col=sample(c(1,2,3),1,replace=T)

        x11()

        print(ggplot(n, aes(searches,hits))+  
                geom_bar(stat='identity',fill=colors[col],color='black')+
                ggtitle(data$headers[2+2*length(z)+i])+ylab('Hits')+
                theme(plot.title = element_text(size = 18,colour="blue"))+
                theme(axis.title.x=element_blank(),axis.title.y = element_text(colour="blue",size=14),axis.text.x = element_text(colour="grey20",size=14,angle=60,hjust=.5,vjust=.5,face="plain"))


        )


        if(geo()=='')
        {
        x11()


        regions = as.data.frame(rr)[c(1,i+1)]

        names(regions)=c('region','hits')

        regions$region[regions$region=="United States"] = "USA"

        world_map = map_data("world")

        world_map =merge(world_map, regions, by="region",all.x = TRUE)

        world_map = world_map[order(world_map$group, world_map$order),]

        g=ggplot(world_map, aes(x=long, y=lat, group=group))+
          geom_polygon(aes(fill=hits), color="gray70") 

        print(g+theme(axis.text.y   = element_blank(),
                      axis.text.x   = element_blank(),
                      axis.title.y  = element_blank(),
                      axis.title.x  = element_blank(),
                      panel.background = element_blank(),
                      panel.grid.major = element_blank(), 
                      panel.grid.minor = element_blank())+
                scale_fill_gradient(low = "skyblue", high = "blue", guide = "colorbar",na.value="white")+ggtitle(data$headers[2+2*length(z)+i])+ylab('Hits')+
                theme(legend.key.size = unit(1, "cm"),
                      legend.title = element_text(size = 12, colour = "blue"),
                      legend.title.align=0.3,legend.text = element_text(size = 10))+
                theme(panel.border = element_rect(colour = "gray70", fill=NA, size=0.5))
        )
        }
      }

    }

  }) 

  }) 

和ui.R:

library(shiny)
library(shinydashboard)

dashboardPage(
  dashboardHeader(title="By Fish"),

  dashboardSidebar(
    br(),


    h6(" Search Term(s)",style="text-align:center;color:#FFA319;font-size:150%"),

    helpText("Give one or more terms that you want R to retrieve data from the Google Trends API.
             Use comma to separate terms", style="text-align:center"),

    textInput('terms',''),


    selectInput("geography", 
label = tags$h4(strong(em("Geography")),style="text-align:center;color:#FFA319;font-size:150%"),
                choices = c("Worldwide"),
                selected = "Worldwide"),           
            selectInput("period", 
                label = tags$h4(strong(em("Time Period")),style="text-align:center;color:#FFA319;font-size:150%"),
                choices = c("2004-present",
                            "Past30Days",
                            "Past90Days",
                            "Past12Months",
                            "2011",
                            "2012",
                            "2013",
                            "2014",
                            "2015"
                ),
                selected = "2004-present"),

    checkboxInput("corr", 
                  label = strong("Correlation",style="text-align:center;color:#FFA319;font-size:150%")),
    br(),

    tags$h1(submitButton("Update!"),style="text-align:center"),
    helpText("To get results, click the 'Update!' button",style="text-align:center"),

    br(),
    br(),
    br(),
    br(),
    br(),
    br()



    ),


  #####
  ##  Main Panel
  #### help ====        
  dashboardBody(    
    fluidRow(
      br(),
      h5(em(strong("Google Trends Analytics", style="color:darkblue;font-size:210%")),align = "center"),

      plotOutput("myplot"),
      br(),
      plotOutput("myplot3"),
      plotOutput("myplot2")


    )
  ))

1 个答案:

答案 0 :(得分:0)

您的问题的答案是,您没有以正确的方式使用gtrends(),可能是为较旧版本的软件包编写的代码。

请这样更改您的代码。

out2<- gtrends(keyword = out(), time = paste0(start_date()," ",end_date()), geo=geo())

即使在代码未成功运行后,也请注意,因为代码中处理函数结果的方式与实际返回的方式有所不同,因此需要对其进行修复。