- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我最近一直在使用 gapminder 数据集探索 Shiny 和 Shiny 的仪表板,并遇到了我的最新问题,试图输出使用基于用户选择的响应变量和预测变量创建的 react 模型的摘要。目的是同时输出 3 个模型的 EDA 图和摘要(基数、对数和逆),这就是为什么 var_EDA_plots()
有点乱。正在返回的错误是:
# Error: argument 1 (type 'list') cannot be handled by 'cat'
与简单线性回归建模相关的代码是:
# LM Page Content
tabItem(tabName = "lm",
h2("Simple Linear Regression"),
h3(wellPanel(fluidRow(
column(2,"Response Variable:"),
column(2, selectInput("response", "", colnames(data)[4:6],
multiple = FALSE, selected = "lifeExp")),
column(2, "Predictor Variable: "),
column(2, selectInput("predictor", "", colnames(data)[4:6],
multiple = FALSE, selected = "gdpPercap"))))
),
tabsetPanel(type = "tabs",
tabPanel("Plot",
splitLayout(
plotOutput("EDAplotBase"),
plotOutput("EDAplotLog"),
plotOutput("EDAplotInv"))),
tabPanel("Summary", verbatimTextOutput("base.lm")),
tabPanel("Table", tableOutput("EDAtable"))
))
服务器
# LINEAR MODELLING ----
# Format Predictor and Response col names
var_EDA_plots <- reactive({
str_col_predictor <- as.character(input$predictor)
str_col_response <- as.character(input$response)
transformed_data <- data %>% select(str_col_response, str_col_predictor, continent, country, year)
transformed_data$predictor_base <- unlist(transformed_data[2])
transformed_data$predictor_log <- unlist(log(transformed_data[2]))
transformed_data$predictor_inv <- unlist(1/transformed_data[2])
transformed_data$response <- unlist(transformed_data[1])
transformed_data %>% select(continent, country, year, response, predictor_base, predictor_log, predictor_inv)
})
# Build linear models reactively
var_lm_base <- reactive({
fml <- as.formula("response ~ predictor_base")
lm(fml, data = var_EDA_plots())
})
# Base LM Model
output$base.lm <- renderText({
summary(var_lm_base())
})
output$EDAtable <- renderTable({var_EDA_plots()})
下面我将粘贴
全码对于 Shiny 的仪表板,如果整体调试和测试更容易(线性建模部分位于服务器底部):
library(shiny)
library(tidyverse)
library(shinydashboard)
library(gapminder)
# LOAD DATA ----
data <- gapminder %>% as_tibble() %>% arrange(country, year)
# LINEAR MODELLING ----
set.seed(117)
train <- data %>% slice_sample(prop = 0.8)
test <- data %>% slice_sample(prop = 0.2)
# UI ----
ui <- dashboardPage(
dashboardHeader(title = "Gapminder Dashboard"),
dashboardSidebar(
sidebarMenu(id = "tabs",
menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard"),
menuSubItem("Life Expectancy", tabName = "life"),
menuSubItem("GDP Per Capita", tabName = "gdp")),
menuItem("Linear Modelling", icon = icon("th"), tabName = "lm", badgeLabel = "new", badgeColor = "green"),
fluidPage(
selectInput("dateStart", "Start date:", distinct(data, year),
multiple = FALSE, selected = 1952),
selectInput("dateEnd", "End date:", distinct(data, year),
multiple = FALSE, selected = 2007),
selectInput("country1", "Select primary country:", distinct(data, country),
multiple = FALSE, selected = "Australia"),
selectInput("country2", "Select secondary country:", distinct(data, country),
multiple = FALSE, selected = "Greece")
))),
dashboardBody(
tabItems(
# Life Expectancy Page Content
tabItem(tabName = "life",
# Top 5 KPIs
splitLayout(
valueBoxOutput("kpi.top5.life1", width = NULL),
valueBoxOutput("kpi.top5.life2", width = NULL),
valueBoxOutput("kpi.top5.life3", width = NULL),
valueBoxOutput("kpi.top5.life4", width = NULL),
valueBoxOutput("kpi.top5.life5", width = NULL)
),
# Life Expectancy Histogram
fluidPage(
plotOutput("histLifeExp")
),
# Life Expectancy & Population Plots
splitLayout(
plotOutput("lineplotLife"),
plotOutput("lineplotPopn")
),
# Bottom 5 KPIs
splitLayout(
valueBoxOutput("kpi.btm5.life1", width = NULL),
valueBoxOutput("kpi.btm5.life2", width = NULL),
valueBoxOutput("kpi.btm5.life3", width = NULL),
valueBoxOutput("kpi.btm5.life4", width = NULL),
valueBoxOutput("kpi.btm5.life5", width = NULL)
)),
# GDP Page Content
tabItem(tabName = "gdp",
# Top 5 KPIs
splitLayout(
valueBoxOutput("kpi.top5.gdp1", width = NULL),
valueBoxOutput("kpi.top5.gdp2", width = NULL),
valueBoxOutput("kpi.top5.gdp3", width = NULL),
valueBoxOutput("kpi.top5.gdp4", width = NULL),
valueBoxOutput("kpi.top5.gdp5", width = NULL)
),
# GDP Histogram
fluidPage(
plotOutput("histGDP")
),
# GDP & Population Plots
splitLayout(
plotOutput("lineplotgdp"),
plotOutput("lineplotPopn2")
),
# Bottom 5 KPIs
splitLayout(
valueBoxOutput("kpi.btm5.gdp1", width = NULL),
valueBoxOutput("kpi.btm5.gdp2", width = NULL),
valueBoxOutput("kpi.btm5.gdp3", width = NULL),
valueBoxOutput("kpi.btm5.gdp4", width = NULL),
valueBoxOutput("kpi.btm5.gdp5", width = NULL)
)),
# LM Page Content
tabItem(tabName = "lm",
h2("Simple Linear Regression"),
h3(wellPanel(fluidRow(
column(2,"Response Variable:"),
column(2, selectInput("response", "", colnames(data)[4:6],
multiple = FALSE, selected = "lifeExp")),
column(2, "Predictor Variable: "),
column(2, selectInput("predictor", "", colnames(data)[4:6],
multiple = FALSE, selected = "gdpPercap"))))
),
tabsetPanel(type = "tabs",
tabPanel("Plot",
splitLayout(
plotOutput("EDAplotBase"),
plotOutput("EDAplotLog"),
plotOutput("EDAplotInv"))),
tabPanel("Summary", verbatimTextOutput("base.lm")),
tabPanel("Table", tableOutput("EDAtable"))
))
)
)
)
# SERVER ----
server <- function(input, output) {
# REACTIVE DATA FILTERING ----
# Top 5 Life Exp KPIs - date filtering
var_maxDate_kpi_top5_life <- reactive({
val <- as.integer(input$dateEnd)
data %>% filter(year == val) %>% slice_max(n = 5, order_by = lifeExp)
})
# Bottom 5 Life Exp KPIs - date filtering
var_maxDate_kpi_btm5_life <- reactive({
val <- as.integer(input$dateEnd)
data %>% filter(year == val) %>% slice_min(n = 5, order_by = lifeExp)
})
# Top 5 GDP KPIs - date filtering
var_maxDate_kpi_top5_gdp <- reactive({
val <- as.integer(input$dateEnd)
data %>% filter(year == val) %>% slice_max(n = 5, order_by = gdpPercap)
})
# Bottom 5 GDP KPIs - date filtering
var_maxDate_kpi_btm5_gdp <- reactive({
val <- as.integer(input$dateEnd)
data %>% filter(year == val) %>% slice_min(n = 5, order_by = gdpPercap)
})
# General Life Expectancy Reactive Filtering
var_date_and_country <- reactive({
startVal <- as.integer(input$dateStart)
endVal <- as.integer(input$dateEnd)
country1 <- as.character(input$country1)
country2 <- as.character(input$country2)
data %>% filter(year >= startVal & year <= endVal & country %in% c(country1, country2))
})
# LIFE EXPECTANCY ----
# Value Boxes - Top 5 KPIs | Life Expectancy
output$kpi.top5.life1 <- renderValueBox({
valueBox(paste(round(var_maxDate_kpi_top5_life()$lifeExp[1],1), "years"),
paste(var_maxDate_kpi_top5_life()$country[1], " (", input$dateEnd, ")", sep = ""), icon = icon("heart"), color = "green")
})
output$kpi.top5.life2 <- renderValueBox({
valueBox(paste(round(var_maxDate_kpi_top5_life()$lifeExp[2],1), "years"),
paste(var_maxDate_kpi_top5_life()$country[2], " (", input$dateEnd, ")", sep = ""), icon = icon("heart"), color = "green")
})
output$kpi.top5.life3 <- renderValueBox({
valueBox(paste(round(var_maxDate_kpi_top5_life()$lifeExp[3],1), "years"),
paste(var_maxDate_kpi_top5_life()$country[3], " (", input$dateEnd, ")", sep = ""), icon = icon("heart"), color = "green")
})
output$kpi.top5.life4 <- renderValueBox({
valueBox(paste(round(var_maxDate_kpi_top5_life()$lifeExp[4],1), "years"),
paste(var_maxDate_kpi_top5_life()$country[4], " (", input$dateEnd, ")", sep = ""), icon = icon("heart"), color = "green")
})
output$kpi.top5.life5 <- renderValueBox({
valueBox(paste(round(var_maxDate_kpi_top5_life()$lifeExp[5],1), "years"),
paste(var_maxDate_kpi_top5_life()$country[5], " (", input$dateEnd, ")", sep = ""), icon = icon("heart"), color = "green")
})
# GGPLOT - Life Expectancy
output$lineplotLife <- renderPlot({
ggplot(var_date_and_country(), aes(x = year, y = lifeExp, color = country)) +
geom_line(lwd = 1.5) +
theme_grey() +
labs(x = "Year",
y = "Life Expectancy",
title = paste("Life Expectancy Trend over time (", input$country1, " v ", input$country2, ")", sep = ""))
})
output$lineplotPopn <- renderPlot({
ggplot(var_date_and_country(), aes(x = year, y = (pop/10^6), color = country)) +
geom_line(lwd = 1.5) +
theme_grey() +
labs(x = "Year",
y = "Population (Millions)",
title = paste("Country Population in millions over time (", input$country1, " v ", input$country2, ")", sep = ""))
})
output$histLifeExp <- renderPlot({
ggplot(data, aes(x = lifeExp, color = continent, fill = continent)) +
geom_histogram() +
theme_grey() +
labs(x = "Life Expectancy",
y = "Frequency",
title = "Life Expectancy distribution by Continent")
})
# Value Boxes - Bottom 5 KPIs | Life Expectancy
output$kpi.btm5.life1 <- renderValueBox({
valueBox(paste(round(var_maxDate_kpi_btm5_life()$lifeExp[1],1), "years"),
paste(var_maxDate_kpi_btm5_life()$country[1], " (", input$dateEnd, ")", sep = ""), icon = icon("heart"), color = "red")
})
output$kpi.btm5.life2 <- renderValueBox({
valueBox(paste(round(var_maxDate_kpi_btm5_life()$lifeExp[2],1), "years"),
paste(var_maxDate_kpi_btm5_life()$country[2], " (", input$dateEnd, ")", sep = ""), icon = icon("heart"), color = "red")
})
output$kpi.btm5.life3 <- renderValueBox({
valueBox(paste(round(var_maxDate_kpi_btm5_life()$lifeExp[3],1), "years"),
paste(var_maxDate_kpi_btm5_life()$country[3], " (", input$dateEnd, ")", sep = ""), icon = icon("heart"), color = "red")
})
output$kpi.btm5.life4 <- renderValueBox({
valueBox(paste(round(var_maxDate_kpi_btm5_life()$lifeExp[4],1), "years"),
paste(var_maxDate_kpi_btm5_life()$country[4], " (", input$dateEnd, ")", sep = ""), icon = icon("heart"), color = "red")
})
output$kpi.btm5.life5 <- renderValueBox({
valueBox(paste(round(var_maxDate_kpi_btm5_life()$lifeExp[5],1), "years"),
paste(var_maxDate_kpi_btm5_life()$country[5], " (", input$dateEnd, ")", sep = ""), icon = icon("heart"), color = "red")
})
# GDP PER CAPITA ----
# Value Boxes - Top 5 KPIs | GDP Per Capita
output$kpi.top5.gdp1 <- renderValueBox({
valueBox(paste("$", round(var_maxDate_kpi_top5_gdp()$gdpPercap[1]/1000,1), "k"),
paste(var_maxDate_kpi_top5_gdp()$country[1], " (", input$dateEnd, ")", sep = ""), icon = icon("dollar-sign"), color = "green")
})
output$kpi.top5.gdp2 <- renderValueBox({
valueBox(paste("$", round(var_maxDate_kpi_top5_gdp()$gdpPercap[2]/1000,1), "k"),
paste(var_maxDate_kpi_top5_gdp()$country[2], " (", input$dateEnd, ")", sep = ""), icon = icon("dollar-sign"), color = "green")
})
output$kpi.top5.gdp3 <- renderValueBox({
valueBox(paste("$", round(var_maxDate_kpi_top5_gdp()$gdpPercap[3]/1000,1), "k"),
paste(var_maxDate_kpi_top5_gdp()$country[3], " (", input$dateEnd, ")", sep = ""), icon = icon("dollar-sign"), color = "green")
})
output$kpi.top5.gdp4 <- renderValueBox({
valueBox(paste("$", round(var_maxDate_kpi_top5_gdp()$gdpPercap[4]/1000,1), "k"),
paste(var_maxDate_kpi_top5_gdp()$country[4], " (", input$dateEnd, ")", sep = ""), icon = icon("dollar-sign"), color = "green")
})
output$kpi.top5.gdp5 <- renderValueBox({
valueBox(paste("$", round(var_maxDate_kpi_top5_gdp()$gdpPercap[5]/1000,1), "k"),
paste(var_maxDate_kpi_top5_gdp()$country[5], " (", input$dateEnd, ")", sep = ""), icon = icon("dollar-sign"), color = "green")
})
# GGPLOT - GDP Per Capita
output$lineplotgdp <- renderPlot({
ggplot(var_date_and_country(), aes(x = year, y = gdpPercap/1000, color = country)) +
geom_line(lwd = 1.5) +
theme_grey() +
labs(x = "Year",
y = "GDP Per Capita (000's)",
title = paste("GDP Per Capita Trend over time (", input$country1, " v ", input$country2, ")", sep = ""))
})
output$lineplotPopn2 <- renderPlot({
ggplot(var_date_and_country(), aes(x = year, y = (pop/10^6), color = country)) +
geom_line(lwd = 1.5) +
theme_grey() +
labs(x = "Year",
y = "Population (Millions)",
title = paste("Country Population in millions over time (", input$country1, " v ", input$country2, ")", sep = ""))
})
output$histGDP <- renderPlot({
ggplot(data, aes(x = gdpPercap, color = continent, fill = continent)) +
geom_histogram() +
theme_grey() +
labs(x = "GDP Per Capita",
y = "Frequency",
title = "GDP Per Capita distribution by Continent")
})
# Value Boxes - Bottom 5 KPIs | GDP Per Capita
output$kpi.btm5.gdp1 <- renderValueBox({
valueBox(paste("$", round(var_maxDate_kpi_btm5_gdp()$gdpPercap[1]/1000,1), "k"),
paste(var_maxDate_kpi_btm5_gdp()$country[1], " (", input$dateEnd, ")", sep = ""), icon = icon("dollar-sign"), color = "red")
})
output$kpi.btm5.gdp2 <- renderValueBox({
valueBox(paste("$", round(var_maxDate_kpi_btm5_gdp()$gdpPercap[2]/1000,1), "k"),
paste(var_maxDate_kpi_btm5_gdp()$country[2], " (", input$dateEnd, ")", sep = ""), icon = icon("dollar-sign"), color = "red")
})
output$kpi.btm5.gdp3 <- renderValueBox({
valueBox(paste("$", round(var_maxDate_kpi_btm5_gdp()$gdpPercap[3]/1000,1), "k"),
paste(var_maxDate_kpi_btm5_gdp()$country[3], " (", input$dateEnd, ")", sep = ""), icon = icon("dollar-sign"), color = "red")
})
output$kpi.btm5.gdp4 <- renderValueBox({
valueBox(paste("$", round(var_maxDate_kpi_btm5_gdp()$gdpPercap[4]/1000,1), "k"),
paste(var_maxDate_kpi_btm5_gdp()$country[4], " (", input$dateEnd, ")", sep = ""), icon = icon("dollar-sign"), color = "red")
})
output$kpi.btm5.gdp5 <- renderValueBox({
valueBox(paste("$", round(var_maxDate_kpi_btm5_gdp()$gdpPercap[5]/1000,1), "k"),
paste(var_maxDate_kpi_btm5_gdp()$country[5], " (", input$dateEnd, ")", sep = ""), icon = icon("dollar-sign"), color = "red")
})
# LINEAR MODELLING ----
# Format Predictor and Response col names
var_EDA_plots <- reactive({
str_col_predictor <- as.character(input$predictor)
str_col_response <- as.character(input$response)
transformed_data <- data %>% select(str_col_response, str_col_predictor, continent, country, year)
transformed_data$predictor_base <- unlist(transformed_data[2])
transformed_data$predictor_log <- unlist(log(transformed_data[2]))
transformed_data$predictor_inv <- unlist(1/transformed_data[2])
transformed_data$response <- unlist(transformed_data[1])
transformed_data %>% select(continent, country, year, response, predictor_base, predictor_log, predictor_inv)
})
# Build linear models reactively
var_lm_base <- reactive({
fml <- as.formula("response ~ predictor_base")
lm(fml, data = var_EDA_plots())
})
# Base EDA Scatter plot
output$EDAplotBase <- renderPlot({
ggplot(var_EDA_plots(), aes(x = predictor_base, y = response)) +
geom_point(aes(color = continent)) +
geom_smooth(method = "lm", se = TRUE) +
theme_grey() +
labs(x = paste(input$predictor, "(Predictor)"),
y = paste(input$response, "(Response)"),
title = paste(input$predictor, "v", input$response))
})
# Log EDA Scatter plot
output$EDAplotLog <- renderPlot({
ggplot(var_EDA_plots(), aes(x = predictor_log, y = response)) +
geom_point(aes(color = continent)) +
geom_smooth(method = "lm", se = TRUE) +
theme_grey() +
labs(x = paste("log(", input$predictor, ") (Predictor)", sep = ""),
y = paste(input$response, "(Response)"),
title = paste("log(", input$predictor, ") v ", input$response, sep = ""))
})
# Inv EDA Scatter plot
output$EDAplotInv <- renderPlot({
ggplot(var_EDA_plots(), aes(x = predictor_inv, y = response)) +
geom_point(aes(color = continent)) +
geom_smooth(method = "lm", se = TRUE) +
theme_grey() +
labs(x = paste("1/", input$predictor, " (Predictor)", sep = ""),
y = paste(input$response, "(Response)"),
title = paste("1/", input$predictor, " v ", input$response, sep = ""))
})
# Base LM Model
output$base.lm <- renderText({
summary(var_lm_base())
})
output$EDAtable <- renderTable({var_EDA_plots()})
}
shinyApp(ui, server)
任何帮助将不胜感激,谢谢:)
最佳答案
renderText()
无法处理列表。使用 summary
使用 lm 对象将产生长度为 11 的列表。修复它的最简单方法是将渲染函数更改为 renderPrint()
反而。
# Base LM Model
output$base.lm <- renderPrint({
summary(var_lm_base())
})
关于R [ Shiny ] : Error trying to output reactive model summary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68241763/
类型‘AbstractControl’上不存在属性‘Controls’。
主要是我很好奇。 我们有一个名为 Unit 的对象在我们的代码库中 - 代表桥梁或道路的组件。在我们的例子中,看到带有 Unit 的 ReactiveUI 命令可能会模棱两可。作为声明中的泛型之一。
我一直听说六边形架构必须与任何框架无关,并使用接口(interface) (SPI) 来委托(delegate)不属于业务层的每个代码部分。 但是如何在不使用额外框架的情况下通过六边形架构创建一个响应
我读了 Reactive Manifesto . 但我无法理解 event driven architectures 之间的核心差异和 message driven architectures .结果
申请要求: 订阅两个事件流 A 和 B 对于每个 A 事件,一段时间后应该有相应的 B 事件 如果没有相应的 B 到达(及时),应用程序会监视 A 事件并发出警报 B 事件可以以与 A 事件不同的顺序
Closed. This question is opinion-based。它当前不接受答案。 想改善这个问题吗?更新问题,以便editing this post用事实和引用来回答。 4年前关闭。
我有一个 ViewModel,它在其初始化程序中有一个输入 init(sliderEvents: Reactive) { 在测试中我想做类似的事情 slider.send(.touchDownInsi
经典实时搜索示例: var searchResults = from input in textBoxChanged from results in GetDa
我有一个响应式(Reactive)管道来处理传入的请求。对于每个请求,我需要调用一个与业务相关的函数 ( doSomeRelevantProcessing )。 完成后,我需要将发生的事情通知一些外部
是否可以为响应式扩展实现基于硬件计时器的自定义调度程序?我该如何开始,有什么好的例子吗? 我有一个硬件可以每毫秒向我发送一个准确的中断。我想利用它来创建更精确的 RX 调度程序。 更新 感谢 Asti
我正在通过网络浏览 Rx 框架 Material ,我发现了很多。 现在,每当我为此在 google 上搜索时,我还会在 wikipedia 链接中找到“响应式(Reactive)编程”。 由于响应式
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 可以用事实和引用来回答它. 6年前关闭。 Improve this
SignalR 与响应式扩展是同一回事吗?你能解释一下为什么或为什么不吗? 最佳答案 不,它们绝对不是同一件事。 Reactive Extensions 是一个用于创建和组合可观察的数据流或事件流的库
我知道有一种简单的方法可以做到这一点 - 但今晚它打败了我...... 我想知道两个事件是否在 300 毫秒内发生,就像双击一样。 在 300 毫秒内单击两次左键鼠标 - 我知道这是构建响应式(Rea
我们的应用程序使用 Reactive Extensions (Rx)。这些通常通过 Microsoft 的可下载包安装。但是,当我们发布应用程序时,我们会提供 dll 的副本(即 System.Cor
我想了解 Reactive 和 ReactiveStreams 之间的区别,特别是在 RxJava 的上下文中? 我能想到的最多的是 Reactive Streams 在规范中有一些背压的概念,但它已
我想探索来自 Quarkus 的响应式 REST 客户端的慢速后端,并在他们建议的样本 (https://github.com/quarkusio/quarkus-quickstarts/tree/m
假设我有一个存储桶,我需要从中获取日期早于现在的文档。 该文档如下所示: { id: "1", date: "Some date", otherObjectKEY: "key1" } 对于每个文档,我
我有一个 RIA 服务数据服务,它有几个函数调用,如下所示: public InvokeOperation SomeFunc( SomeData data, Action> callb
我一直在使用 Rx 在单个应用程序中创建事件总线(想想 CQRS/ES),它似乎工作得很好。然而,在调查了一堆不同的事件溯源框架之后,我还没有看到使用过一次 Rx。与基于反射/容器的调度程序相比,它似
我是一名优秀的程序员,十分优秀!