我正在研究一个从雅虎请求股票数据的小应用程序。如果单击图,则应显示数据集中相应的价格/日期时间行。但不幸的是,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
有什么想法吗?
答案 0 :(得分:0)
似乎ALTER TABLE MYTABLE ALTER COLUMN MYCOLUMN SET DEFAULT 1 NOT NULL
操纵某事。仅使用NOT NULL
可以解决问题,并将print(p)
值正确显示为数字。
p
作品。