如何在 R / Shiny 中构建反应式数据框?
How do I build a reactive dataframe in R / Shiny?
我想使用反应式数据框来显示多个绘图和图形。我有一个数据集,我希望能够对其进行过滤。我找到了正确的过滤器设置,我想在许多不同的图上显示数据——如果过滤器设置发生变化,这些数据将会更新。
这可能解释了我正在尝试做的事情:
UI:
fluidPage(
sidebarLayout(
sidebarPanel(
checkboxGroupInput("checkGroups",
label = "Include", choices = list("1 star" = 1, "2 star" = 2,
"3 star" = 3, "4 star" = 4,
"5 star" = 5),
selected = list(1, 2, 3, 4, 5)),
checkboxInput("checkbox", "Include Replies"),
actionButton("Button", "Update")
),
mainPanel(
showOutput("plot", "nvd3"),
showOutput("pieplot", "nvd3")
)
)
)
服务器:
rating <- c(2, 3, 5, 4, 1, 5, 3, 1, 4)
date_time <- c("2015-05-14", "2015-05-07", "2015-05-06", "2015-04-11", "2015-01-07", "2014-12-06", "2014-04-11", "2014-01-07", "2013-12-06")
repliesOnly <- c(TRUE, FALSE, TRUE, TRUE, FALSE, TRUE, FALSE, FALSE, TRUE)
data <- data.frame(rating, date_time, repliesOnly)
function(input, output, session) {
load("data.Rdata")
newdata <- reactive({
filter_data <- data
filter_data <- filter_data %>% filter(rating %in% input$checkGroups)
filter_data <- filter_data %>% filter(repliesOnly %in% input$checkbox)
return(filter_data)
})
output$plot <- renderChart({
plot <- nPlot(rating ~ date_time, data = newdata,
type = "multiBarHorizontalChart", dom = 'plot')
return(plot)
})
output$pieplot <- renderChart({
pieplot <- nPlot(rating ~ date_time, data = newdata,
type = "pieChart", dom = 'pieplot')
return(pieplot)
})
}
可以吗?当然,我可以只为每个图形输出添加过滤器,但是我的数据集很大,过滤器也很复杂,所以如果它应该为每个图形计算它需要很长时间。
非常感谢所有帮助!
感谢您的帮助 - 这有效:
服务器:
library(shiny)
library(rCharts)
rating <- c(2, 3, 5, 4, 1, 5, 3, 1, 4)
date_time <- c("2015-05-14", "2015-05-07", "2015-05-06", "2015-04-11", "2015-01-07", "2014-12-06", "2014-04-11", "2014-01-07", "2013-12-06")
repliesOnly <- c(TRUE, FALSE, TRUE, TRUE, FALSE, TRUE, FALSE, FALSE, TRUE)
data <- data.frame(rating, date_time, repliesOnly)
data$rating <- as.character(data$rating)
function(input, output, session) {
load("data.Rdata")
datadata <- data
makeReactiveBinding("datadata")
newData <- reactive({
input$Button
isolate({
datadata <- data
datadata <- subset(datadata, rating %in% input$checkGroups)
})
})
output$plot <- renderChart({
datadata <- newData()
plot <- nPlot(rating ~ date_time, data = datadata,
type = "multiBarHorizontalChart", dom = 'plot')
return(plot)
})
output$pieplot <- renderChart({
datadata <- newData()
pieplot <- nPlot(rating ~ date_time, data = datadata,
type = "pieChart", dom = 'pieplot')
return(pieplot)
})
}
谢谢你提供这个例子;我还需要一个反应式数据帧过滤。战斗 2 小时后出现以下错误:
cannot coerce class ""reactive"" to a data.frame
你的post帮我解决了问题。我敢 post 你的例子的简化版本,更容易重现(没有 showOutput
或 readChart
调用),以供将来其他闪亮的用户参考。
N.B.: 我使用 single-file 应用程序。
library(shiny)
rating <- c(2, 3, 5, 4, 1, 5, 3, 1, 4)
date_time <- c("2015-05-14", "2015-05-07", "2015-05-06", "2015-04-11",
"2015-01-07", "2014-12-06", "2014-04-11", "2014-01-07", "2013-12-06")
repliesOnly <- c(TRUE, FALSE, TRUE, TRUE, FALSE, TRUE, FALSE, FALSE, TRUE)
data <- data.frame(rating, date_time, repliesOnly)
data$rating <- as.character(data$rating)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
checkboxGroupInput("checkGroups",
label = "Include", choices = c("1 star" = 1,
"2 star" = 2,
"3 star" = 3,
"4 star" = 4,
"5 star" = 5),
selected = list(1, 2, 3, 4, 5))
),
mainPanel(
tableOutput("view")
)
)
)
server <- function(input, output, session) {
newData <- reactive({
data <- subset(data, rating %in% input$checkGroups)
})
output$view <- renderTable({ newData() })
}
shinyApp(ui = ui, server = server)
我想使用反应式数据框来显示多个绘图和图形。我有一个数据集,我希望能够对其进行过滤。我找到了正确的过滤器设置,我想在许多不同的图上显示数据——如果过滤器设置发生变化,这些数据将会更新。
这可能解释了我正在尝试做的事情:
UI:
fluidPage(
sidebarLayout(
sidebarPanel(
checkboxGroupInput("checkGroups",
label = "Include", choices = list("1 star" = 1, "2 star" = 2,
"3 star" = 3, "4 star" = 4,
"5 star" = 5),
selected = list(1, 2, 3, 4, 5)),
checkboxInput("checkbox", "Include Replies"),
actionButton("Button", "Update")
),
mainPanel(
showOutput("plot", "nvd3"),
showOutput("pieplot", "nvd3")
)
)
)
服务器:
rating <- c(2, 3, 5, 4, 1, 5, 3, 1, 4)
date_time <- c("2015-05-14", "2015-05-07", "2015-05-06", "2015-04-11", "2015-01-07", "2014-12-06", "2014-04-11", "2014-01-07", "2013-12-06")
repliesOnly <- c(TRUE, FALSE, TRUE, TRUE, FALSE, TRUE, FALSE, FALSE, TRUE)
data <- data.frame(rating, date_time, repliesOnly)
function(input, output, session) {
load("data.Rdata")
newdata <- reactive({
filter_data <- data
filter_data <- filter_data %>% filter(rating %in% input$checkGroups)
filter_data <- filter_data %>% filter(repliesOnly %in% input$checkbox)
return(filter_data)
})
output$plot <- renderChart({
plot <- nPlot(rating ~ date_time, data = newdata,
type = "multiBarHorizontalChart", dom = 'plot')
return(plot)
})
output$pieplot <- renderChart({
pieplot <- nPlot(rating ~ date_time, data = newdata,
type = "pieChart", dom = 'pieplot')
return(pieplot)
})
}
可以吗?当然,我可以只为每个图形输出添加过滤器,但是我的数据集很大,过滤器也很复杂,所以如果它应该为每个图形计算它需要很长时间。
非常感谢所有帮助!
感谢您的帮助 - 这有效:
服务器:
library(shiny)
library(rCharts)
rating <- c(2, 3, 5, 4, 1, 5, 3, 1, 4)
date_time <- c("2015-05-14", "2015-05-07", "2015-05-06", "2015-04-11", "2015-01-07", "2014-12-06", "2014-04-11", "2014-01-07", "2013-12-06")
repliesOnly <- c(TRUE, FALSE, TRUE, TRUE, FALSE, TRUE, FALSE, FALSE, TRUE)
data <- data.frame(rating, date_time, repliesOnly)
data$rating <- as.character(data$rating)
function(input, output, session) {
load("data.Rdata")
datadata <- data
makeReactiveBinding("datadata")
newData <- reactive({
input$Button
isolate({
datadata <- data
datadata <- subset(datadata, rating %in% input$checkGroups)
})
})
output$plot <- renderChart({
datadata <- newData()
plot <- nPlot(rating ~ date_time, data = datadata,
type = "multiBarHorizontalChart", dom = 'plot')
return(plot)
})
output$pieplot <- renderChart({
datadata <- newData()
pieplot <- nPlot(rating ~ date_time, data = datadata,
type = "pieChart", dom = 'pieplot')
return(pieplot)
})
}
谢谢你提供这个例子;我还需要一个反应式数据帧过滤。战斗 2 小时后出现以下错误:
cannot coerce class ""reactive"" to a data.frame
你的post帮我解决了问题。我敢 post 你的例子的简化版本,更容易重现(没有 showOutput
或 readChart
调用),以供将来其他闪亮的用户参考。
N.B.: 我使用 single-file 应用程序。
library(shiny)
rating <- c(2, 3, 5, 4, 1, 5, 3, 1, 4)
date_time <- c("2015-05-14", "2015-05-07", "2015-05-06", "2015-04-11",
"2015-01-07", "2014-12-06", "2014-04-11", "2014-01-07", "2013-12-06")
repliesOnly <- c(TRUE, FALSE, TRUE, TRUE, FALSE, TRUE, FALSE, FALSE, TRUE)
data <- data.frame(rating, date_time, repliesOnly)
data$rating <- as.character(data$rating)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
checkboxGroupInput("checkGroups",
label = "Include", choices = c("1 star" = 1,
"2 star" = 2,
"3 star" = 3,
"4 star" = 4,
"5 star" = 5),
selected = list(1, 2, 3, 4, 5))
),
mainPanel(
tableOutput("view")
)
)
)
server <- function(input, output, session) {
newData <- reactive({
data <- subset(data, rating %in% input$checkGroups)
})
output$view <- renderTable({ newData() })
}
shinyApp(ui = ui, server = server)