[英]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.