[英]Add new reactive column in Shiny
我打算在 R Shiny 中以反应方式添加新列以用于绘图。 即用户输入改变后,数据集再次被过滤,新列被重新计算。
这是我专门为您准备的代码示例供您测试。 所以,在这段代码中,我想显示的是每个花瓣长度计数的散点 plot 作为花瓣长度的 function,并且还根据我计算的自定义类别列为每个点着色并添加到 Z6A8064B53DF47945055704。
我不知道我在哪个部分犯了错误。 我收到错误:“尝试复制‘closure’类型的 object”
期待您的帮助。
library(shiny)
library(tidyr)
library(dplyr)
library(ggplot2)
#Prepare dataset
iriss <- iris
iriss$Petal.Length <- lapply(iris$Petal.Length, function(x) round(x, 0))
all_species <- unique(iriss$Species)
ui <- fixedPage(
titlePanel(h1(strong("Adding New Reactive Column - Test"), align = "center")),
checkboxGroupInput("species", "Species", choices=all_species, selected=all_species),
plotOutput("Petal_Count")
)
server <- function(input, output, session) {
#filter based on user input
iris_selected <- reactive(subset(iris, Species %in% input$species))
#groupby petal.length (count)
iris_grouped <- reactive(as.data.frame(iris_selected() %>% count(Petal.Length)))
#Add new category column reactively
iris_grouped2 <- reactive({
iris_grouped_new <- iris_grouped()
iris_grouped_new$Categ <- reactive({
ifelse(iris_grouped_new$Petal.Length >= 0 & iris_grouped_new$Petal.Length <= 1, '0-1',
ifelse(iris_grouped_new$Petal.Length >= 1 & iris_grouped_new$Petal.Length <= 2, '1-2',
ifelse(iris_grouped_new$Petal.Length >= 2 & iris_grouped_new$Petal.Length <= 3, '2-3',
ifelse(iris_grouped_new$Petal.Length >= 3 & iris_grouped_new$Petal.Length <= 4, '4',
ifelse(iris_grouped_new$Petal.Length >= 4, '4+', "")))))
return(iris_grouped_new)
})
})
#Plot scatter plot
output$Petal_Count <- renderPlot(
ggplot(iris_grouped2(), aes(x=Petal.Length, y=n)) +
geom_point(size = 4, alpha = 0.8, aes(colour=Categ)) +
geom_point(shape = 1, size = 4, colour = "black") +
theme_minimal() +
theme(plot.background = element_rect(color = "black", size = 1), legend.position = c(0.85, 0.60)) +
labs(x="Petal Length", y = "Count") +
guides(colour=guide_legend(title="Petal Length Group"))
)
}
shinyApp(ui, server)
终于找到了解决方案,类似于这个链接。
library(shiny)
library(tidyr)
library(dplyr)
library(ggplot2)
iriss <- iris
iriss$Petal.Length <- lapply(iriss$Petal.Length, function(x) round(x, 0))
all_species <- unique(iriss$Species)
ui <- fixedPage(
titlePanel(h1(strong("Adding New Reactive Column - Test"), align = "center")),
checkboxGroupInput("species", "Species", choices=all_species, selected=all_species),
plotOutput("Petal_Count")
)
server <- function(input, output, session) {
iris_selected <- reactive(subset(iriss, Species %in% input$species))
iris_grouped <- reactive(as.data.frame(iris_selected() %>% count(Petal.Length)))
newdf <- reactive({
cbind(
iris_grouped(),
Categ = newvar()
)
})
newvar <- reactive({
ifelse(iris_grouped()$Petal.Length >= 0 & iris_grouped()$Petal.Length <= 1, '0-1',
ifelse(iris_grouped()$Petal.Length >= 1 & iris_grouped()$Petal.Length <= 2, '1-2',
ifelse(iris_grouped()$Petal.Length >= 2 & iris_grouped()$Petal.Length <= 3, '2-3',
ifelse(iris_grouped()$Petal.Length >= 3 & iris_grouped()$Petal.Length <= 4, '3-4',
ifelse(iris_grouped()$Petal.Length >= 4, '4+', "")))))
})
output$Petal_Count <- renderPlot(
ggplot(newdf(), aes(x=as.numeric(Petal.Length), y=n)) +
geom_point(size = 4, alpha = 0.8, aes(colour=Categ)) +
geom_point(shape = 1, size = 4, colour = "black") +
theme_minimal() +
theme(plot.background = element_rect(color = "black", size = 1), legend.position = c(0.85, 0.60)) +
labs(x="Petal Length", y = "Count") +
guides(colour=guide_legend(title="Petal Length Group"))
)
}
shinyApp(ui, server)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.