只想为数据表而不是传单数据计算按选定日期范围分组的数字变量的平均值和中位数。传单地图可以正常工作(只是需要缩小以查看伪造的长/平图,但现在不必担心)。
我创建了第二个数据帧df10
,用于数据的数据表中位数/均值求和。
到目前为止,尝试更改输入函数以为均值创建单独的变量,但发现它很麻烦,并且对于我的需求不是必需的。
在此处Shiny calculate the mean of columns in dataframe尝试使用colMeans(dataset()[,which(sapply(dataset(), class) != "Date")])
错误为"invalid 'x' type in 'x && y"
。这是与海豚有关的
### Generate a dataset ###
start_date <- as.Date('2018-01-01')
end_date <- as.Date('2019-05-10')
set.seed(1984)
date1 <- as.Date(sample( as.numeric(start_date): as.numeric(end_date), 988,
replace = T), origin = '1970-01-01')
group <- rep(letters[1:26], each = 38)
x1 <- runif(n = 988, min = 3.26, max = 10)
x2 <- runif(n = 988, min = 3.26, max = 10)
x3 <- runif(n = 988, min = 3.26, max = 10)
x4 <- runif(n = 988, min = 3.26, max = 10)
x5 <- runif(n = 988, min = 3.26, max = 10)
latitude <- runif(988,40.75042,50.75042)
longitude <- runif(988,-73.98928,-63.98928)
dataframe <- cbind(data.frame(date1,group,x1,x2,x3,x4,x5,latitude,longitude))
df10 <- cbind(data.frame(date1,group,x1,x2,x3,x4,x5))
library(lubridate)
dataframe$date <- ymd(dataframe$date1)
df10$date <- ymd(df10$date1)
library(shiny)
library(leaflet)
library(DT)
dataframe$defectrateLvl <- cut(dataframe$x1,
c(3.26,6,100), include.lowest = T,
labels = c('3.26-6x','6x+'))
beatCol <- colorFactor(palette = c('yellow', 'red'), dataframe$defectrateLvl)
ui <- fluidPage(
dateInput(inputId = "date", label="Select a date", value = "2019-03-01", min = "2018-01-01", max = "2019-05-10",
format = "yyyy-mm-dd", startview = "month",
language = "en", width = NULL),
leafletOutput("map"),
fluidRow(
dateRangeInput("daterange","Date range:",start=Sys.Date()-10, end=Sys.Date() -1),
DT::dataTableOutput("tbl")
)
)
server <- shinyServer(function (input, output,session) {
dailyData <- reactive(dataframe[dataframe$date == format(input$date, '%Y/%m/%d'), ] )
output$map <- renderLeaflet({
dataframe <- dailyData() # Added this in attempt to integrate
dataframe %>% leaflet() %>%
setView(lng = -73.98928, lat = 40.75042, zoom = 10) %>%
addProviderTiles("CartoDB.Positron", options = providerTileOptions(noWrap = TRUE)) %>%
addCircleMarkers(
lng=~dataframe$longitude, # Longitude coordinates
lat=~dataframe$latitude, # Latitude coordinates
#radius=~defectrateLvl, # Total count
popup =~ dataframe$group,
color = ~beatCol(dataframe$defectrateLvl),
fillOpacity=0.5 # Circle Fill Opacity
)
})
output$tbl<-DT::renderDataTable({
dataset <- reactive({df10 })
dataset() %>% group_by(group) %>%
filter(date > input$daterange[1],
date < input$daterange[2])
#sapply(Filter(is.numeric, df6), mean)
colMeans(dataset()[,which(sapply(dataset(), class) !="date","date1","group")])
})
})
shinyApp(ui, server)
我希望数值变量可以按均值进行汇总,如果可能的话可以按中位数进行汇总,但这在当前并不重要。任何帮助将不胜感激。
答案 0 :(得分:0)
该错误是由上一个功能引起的。
colMeans(df[,which(sapply(df, class) !="date","date1","group")])
此代码会将函数应用于所有不属于xy类的列。 "date"
或"group"
是列名。
ColMeans
还会产生一个数值向量,这将导致错误,因为DT
只能显示矩阵或data.frame。我为您提供了创建数据框的代码。但是在Generell中,我会考虑使用dplyr
来创建您的结果。这要容易得多。
这是一个可行的解决方案,但是您必须更改日期输入,因为预定义的选择将创建一个具有0行的data.frame。
library(lubridate)
library(shiny)
library(leaflet)
library(DT)
library(dplyr)
### Generate a dataset ###
start_date <- as.Date('2018-01-01')
end_date <- as.Date('2019-05-10')
set.seed(1984)
date1 <- as.Date(sample( as.numeric(start_date): as.numeric(end_date), 988,
replace = T), origin = '1970-01-01')
group <- rep(letters[1:26], each = 38)
x1 <- runif(n = 988, min = 3.26, max = 10)
x2 <- runif(n = 988, min = 3.26, max = 10)
x3 <- runif(n = 988, min = 3.26, max = 10)
x4 <- runif(n = 988, min = 3.26, max = 10)
x5 <- runif(n = 988, min = 3.26, max = 10)
latitude <- runif(988,40.75042,50.75042)
longitude <- runif(988,-73.98928,-63.98928)
dataframe <- cbind(data.frame(date1,group,x1,x2,x3,x4,x5,latitude,longitude))
df10 <- cbind(data.frame(date1,group,x1,x2,x3,x4,x5))
dataframe$date <- ymd(dataframe$date1)
df10$date <- ymd(df10$date1)
dataframe$defectrateLvl <- cut(dataframe$x1,
c(3.26,6,100), include.lowest = T,
labels = c('3.26-6x','6x+'))
beatCol <- colorFactor(palette = c('yellow', 'red'), dataframe$defectrateLvl)
ui <- fluidPage(
dateInput(inputId = "date", label="Select a date", value = "2019-03-01", min = "2018-01-01", max = "2019-05-10",
format = "yyyy-mm-dd", startview = "month",
language = "en", width = NULL),
leafletOutput("map"),
fluidRow(
dateRangeInput("daterange","Date range:",start=Sys.Date()-10, end=Sys.Date() -1),
DT::dataTableOutput("tbl")
)
)
server <- shinyServer(function (input, output,session) {
dailyData <- reactive(dataframe[dataframe$date == format(input$date, '%Y/%m/%d'), ] )
output$map <- renderLeaflet({
dataframe <- dailyData() # Added this in attempt to integrate
dataframe %>% leaflet() %>%
setView(lng = -73.98928, lat = 40.75042, zoom = 10) %>%
addProviderTiles("CartoDB.Positron", options = providerTileOptions(noWrap = TRUE)) %>%
addCircleMarkers(
lng=~dataframe$longitude, # Longitude coordinates
lat=~dataframe$latitude, # Latitude coordinates
#radius=~defectrateLvl, # Total count
popup =~ dataframe$group,
color = ~beatCol(dataframe$defectrateLvl),
fillOpacity=0.5 # Circle Fill Opacity
)
})
dataset <- reactive({df10 })
output$tbl <-DT::renderDataTable({
df <- dataset()
df <- df %>%
group_by(group) %>%
filter(date > input$daterange[1],
date < input$daterange[2])
#sapply(Filter(is.numeric, df6), mean)
result <- data.frame(colMeans(df[which(sapply(df, class)=="numeric")]))
colnames(result)[1] <- "Result"
result
#colMeans(df[,which(sapply(df, class) !="date","date1","group")])
})
})
shinyApp(ui, server)