R 添加新的反应性色谱柱
我打算在R中以反应式的方式添加新列,以用于绘图。也就是说,在用户更改输入后,将再次过滤数据集,并以反应方式再次计算新列 下面是我专门为您编写的代码示例,供您测试。因此,在这段代码中,我想显示的是每个花瓣长度的计数的散点图,作为花瓣长度的函数,并且根据我计算的自定义类别列对每个点进行着色,并以反应方式添加到dataframe 我不知道我在哪一部分出错了。我遇到错误:“尝试复制“closure”类型的对象” 期待您的帮助R 添加新的反应性色谱柱,r,ggplot2,shiny,R,Ggplot2,Shiny,我打算在R中以反应式的方式添加新列,以用于绘图。也就是说,在用户更改输入后,将再次过滤数据集,并以反应方式再次计算新列 下面是我专门为您编写的代码示例,供您测试。因此,在这段代码中,我想显示的是每个花瓣长度的计数的散点图,作为花瓣长度的函数,并且根据我计算的自定义类别列对每个点进行着色,并以反应方式添加到dataframe 我不知道我在哪一部分出错了。我遇到错误:“尝试复制“closure”类型的对象” 期待您的帮助 library(shiny) library(tidyr) library(d
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)
库(闪亮)
图书馆(tidyr)
图书馆(dplyr)
图书馆(GG2)
#准备数据集
IRIS最终找到了一个类似于此的解决方案
库(闪亮)
图书馆(tidyr)
图书馆(dplyr)
图书馆(GG2)
Iris你在嵌套反应式表达式。这不是个好主意。我不擅长闪亮;我只是在其他帖子中发现了这个想法:\期待一个解决方案。谢谢
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)