【问题标题】:Shiny nearPoints not working when X Var is POSIXct当 X Var 为 POSIXct 时,闪亮的 nearPoints 不起作用
【发布时间】:2016-06-29 13:40:40
【问题描述】:

我正在开发一个小巧的应用程序,该应用程序从雅虎请求股票数据。如果单击图表,将显示数据集中的相应价格/日期时间行。但不幸的是,input$plot_click 似乎没有返回正确的 x,y - 值。

这是mwe:

library(shiny)
library(htmlwidgets)
library(ggplot2)
library(scales)
library(dplyr)
library(RCurl)
library(XML)
library(rvest)

server <- function(input, output, session) {
   Sys.setlocale("LC_TIME", "C")
   dataset <- data.frame()
   xml.url <- "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quote%20where%20symbol%20in%20(%22YHOO%22%2C%22AAPL%22%2C%22GOOG%22%2C%22MSFT%22)&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys"

   YahooObs <- function(xml.url){
      script  <- getURL(xml.url)
      doc     <- xmlParse(script)
      results <- doc %>% xml_nodes("results")
      dataset <- lapply(results, FUN=function(x){xmlToDataFrame(x, stringsAsFactors = F)})[[1]]
      dataset$LastTradePriceOnly <- as.numeric(dataset$LastTradePriceOnly)
      created <- doc %>% xml_node("query") %>% xml_attr("created")
      dataset$created <- as.POSIXct(strptime(created, format="%Y-%m-%dT%H:%M:%SZ", tz="UTC")+3600)
      return(dataset)
    }

  output$newsplot <- renderPlot({
     invalidateLater(10000, session)
     dataset <<- rbind(dataset, YahooObs(xml.url))
     p <- ggplot(data = dataset)
     p <- p + layer(mapping=aes(x=created, y=LastTradePriceOnly, color= Symbol),
               geom="point", stat="identity", position="identity")
     limit_down <- as.POSIXct(Sys.time()-input$timeslider*60)
     attributes(limit_down)$tzone <- input$timezone
     limit_up <- as.POSIXct(Sys.time())
     attributes(limit_up)$tzone <- input$timezone
     p <- p + scale_x_datetime(breaks = date_breaks("200 sec"), labels = date_format("%H:%M:%S"),
                          limits=c(min(dataset$created-1800), max(dataset$created))) +
  theme(axis.text.x = element_text(angle = 90), panel.grid.major=element_blank(), panel.grid.minor=element_blank(),
        panel.background = element_blank()) + coord_cartesian()
     print(p)
  })

    output$plot_click <- renderPrint({
       paste(str(input$plot_click))
    })

   output$newstable <- renderDataTable({
      ##Transforming the created column to numeric was a hint on stackoverflow but it didn't work out
      #dataset$created <- as.numeric(dataset$created)
      nearPoints(dataset, input$plot_click, xvar="created",yvar="LastTradeDatePrice", threshold = 100, maxpoints = 10,
           addDist = TRUE)
})} #the server

ui_2 <- shinyUI(fluidPage(
    #header
    titlePanel(tags$h1("Share Prices")),
    #horizontal line
    sidebarLayout(
       sidebarPanel(
          sliderInput("timeslider", label = "Choose Timespan in minutes", min = 1, max = 60, value = 30, step = 1),
  width=3
      ),
      mainPanel(
         tabsetPanel(type="tabs",
              tabPanel("News Plot", plotOutput("newsplot", click="plot_click"),dataTableOutput("newstable"), textOutput("plot_click")),
              tabPanel("Settings",  selectInput("timezone", label="Choose your Timezone", choices=c("UTC")))
  )
)
)))# the user interface

shinyApp(ui = ui_2, server = server) # this launches your app

有什么想法吗?

【问题讨论】:

标签: r ggplot2 shiny


【解决方案1】:

print(p) 似乎在操纵某些东西。仅使用 p 可以解决问题,并将 POSIXct 值正确显示为数字。

p <- ggplot(data = dataset)
p <- p + layer(mapping=aes(x=created, y=LastTradePriceOnly, color= Symbol),
           geom="point", stat="identity", position="identity")limit_down <- as.POSIXct(Sys.time()-input$timeslider*60)
attributes(limit_down)$tzone <- input$timezone
limit_up <- as.POSIXct(Sys.time())
attributes(limit_up)$tzone <- input$timezone
p <- p + scale_x_datetime(breaks = date_breaks("200 sec"), labels = date_format("%H:%M:%S"),
                      limits=c(min(dataset$created-1800), max(dataset$created))) +
theme(axis.text.x = element_text(angle = 90), panel.grid.major=element_blank(), panel.grid.minor=element_blank(),
    panel.background = element_blank()) + coord_cartesian()
p

有效。

【讨论】:

    猜你喜欢
    • 2018-03-07
    • 2017-04-09
    • 2018-09-25
    • 1970-01-01
    • 2017-09-10
    • 2018-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多