我正在尝试使用shiny
创建R应用。它由sliderInput
组成,允许用户选择日期范围(最小和最大日期)来对数据进行子集化,这些数据将被绘制为ggplot条形图。
我面临着将输入值分配到data.frame的子集的问题,然后将其用于绘制条形图。我通过R控件获得了子集代码,并且它生成了正确的数据,但是当我尝试在应用程序上运行时,我尝试使用输入值作为限制因素会让我遇到问题。
我的代码如下:
ui.R
library(shiny)
#define shiny UI
shinyUI(fluidPage(
titlePanel("Life Span"),
sidebarLayout(
sidebarPanel(
sliderInput("days",
"Duration:",
min = start,
max = last,
value = c(start,last),
timeFormat = "%F %T")
),
mainPanel(
plotOutput("timeline")
)
)
))
server.R
library(shiny)
library(RMySQL)
library(quantmod)
library(ggplot2)
shinyServer(function(input, output) {
#here i create function to scope data
getDataSubSet <- function(dataFrame, column2Filter, rangeStart, rangeEnd){
rangeStart1<-strptime(rangeStart,format = "%Y-%m-%d %H:%M:%S")
rangeEnd1<-strptime(rangeEnd,format = "%Y-%m-%d %H:%M:%S")
dataFrame[dataFrame[column2Filter] >= rangeStart1 & dataFrame[column2Filter] <= rangeEnd1,]
}
output$timeline <- renderPlot({
#to create reactive data....
Candy_Sales_Working <- reactive({
getDataSubSet(Candy_Sales, "Sales_date", input$days[1], input$days[2])
})
ggplot(Candy_Sales_Working(), aes(Candy_Sales_Working()[candyType])) +
geom_bar(stat="count")
})
})
global.R
library(RMySQL)
con <- dbConnect(RMySQL::MySQL(), user = "user", password = "pw", dbname = "candy",
host = "candy.com", port = ....)
Candy_Sales = dbGetQuery(conn = con, statement = "Select * from candy_sales")
Candy_Sales <- transform(Tape_Span,
Sales_Date = as.POSIXlt(strptime(Sales_Date, "%Y-%m-%d %H:%M:%S")))
Candy_Sales_Working <- Candy_Sales
start = strptime(min(Candy_Sales$Sales_Date), format = "%Y-%m-%d %H:%M:%S")
last = strptime(max(Candy_Sales$Sales_Date), format = "%Y-%m-%d %H:%M:%S")
candy_types = unique(Candy_Sales$candyType)
错误陈述未实施这些类型的比较
答案 0 :(得分:0)
发现错误,原因是dataFrame[column2Filter]
应写为dataFrame[[column2Filter]]
而不是
dataFrame[dataFrame[[column2Filter]] >= rangeStart1 & dataFrame[[column2Filter]] <= rangeEnd1,]