java实现用户分组,根据用户指定的组分组数据

该博客介绍了如何创建一个R Shiny应用程序,该程序允许用户通过交互式界面选择过滤条件,对Iris数据集进行分组,并根据用户指定的属性计算平均值。当用户选择'GroupDataforMeanAnalysis'时,程序会按所选物种对数据进行分组,并用平均值绘制结果。文章中提供了UI和SERVER部分的代码实现,包括使用ggvis进行数据可视化。
摘要由CSDN通过智能技术生成

我有一个闪亮的应用程序,接受用户输入,其中x和y变量绘制,并能够根据用户输入过滤它 .

36a0038c-8b3b-4f6f-9dee-ee23e39b1daa.png

我希望能够做的是根据用户指定的组对数据进行分组(样本中只列出了物种,但我的实际代码将使用许多不同的输入进行分组,多个= FALSE),并且具有数据调整,以便它取每个唯一用户指定组的平均值并绘制结果 . 因此,在此示例中,仅显示3个数据点,一个用于虹膜数据集中的每个物种 . 具体使用假设的用户输入x = Petal.Length和y = Petal.Width的setosa物种,数据点将绘制在x = setosa的平均值Petal.Lengths和y = setosa Petal.Widths的平均值 . 我使用基本的R plot函数运行它,这就是我希望它看起来像ggvis的结果:

3042ff79-8f2f-4831-a0f3-d7ca6688ce6d.png

这是通过以下代码在单独的脚本中完成的:

alldata

splitDataX

meanXvar

splitDataY

meanYvar

plot(meanXvar, meanYvar)

正如我现在所做的那样,当我尝试通过每个x和y变量的平均值对物种数据进行分组时,应用程序崩溃了 . 如果它是独立的话,else部分按预期工作 .

# GLOBAL

#Check packages to use in library

library('shiny') #allows for the shiny app to be used

library('stringr') #string opperator

library('ggvis') #allows for interactive ploting

library('dplyr')

alldata

#Establish options for drop down menus

specieschoices

petalwchoices

petallchoices

sepallchoices

sepalwchoices

# UI

ui

titlePanel("Explorer"),

fluidRow(

column(4,

wellPanel(

h4("Apply Filters"),

selectInput(inputId = "species", label="Select a Species:", choices = sort(specieschoices), selected=specieschoices, multiple = TRUE, selectize = TRUE),

selectInput(inputId = "petalw", label="Select Petal Width:", choices = sort(petalwchoices), selected=petalwchoices, multiple = TRUE, selectize = FALSE),

selectInput(inputId = "petall", label="Select Petal Length", choices = sort(petallchoices), selected=petallchoices, multiple = TRUE, selectize = FALSE),

selectInput(inputId = "sepall", label="Select Sepal Length", choices = sort(sepallchoices), selected=sepallchoices, multiple = TRUE, selectize = FALSE),

selectInput(inputId = "sepalw", label="Select Sepal Width", choices = sort(sepalwchoices), selected=sepalwchoices, multiple = TRUE, selectize = FALSE),

checkboxInput(inputId = "groupdata", label="Group Data for Mean Analysis", value = FALSE),

conditionalPanel(

condition = "input.groupdata == true",

wellPanel(

selectInput(inputId = "group", label = "Select Filter to Group By", choices = as.character("Species"), selected = "Species", multiple = FALSE, selectize = TRUE)

))

)),

column(8,

ggvisOutput("plot1")

),

column(4,

wellPanel(

h4("Data Variables"),

selectInput(inputId = "x", label="Select x-axis Variable:", choices=as.character(names(alldata[,1:4])),selected='Petal.Length', multiple = FALSE),

selectInput(inputId = "y", label="Select y-axis Variable:", choices=as.character(names(alldata[,1:4])),selected='Petal.Width', multiple = FALSE)

))

))

#SERVER

server

{

#Set up reactive variables

filteredData

#Apply filters

m % filter(

`Species` %in% input$species,

`Petal.Width` %in% input$petalw,

`Petal.Length` %in% input$petall,

`Sepal.Width` %in% input$sepalw,

`Sepal.Length` %in% input$sepall

)

m

m

})

vis

################## THIS IS THE SECTION THAT I NEED HELP WITH #################

if (input$groupdata == TRUE) {

splitDataX % filteredData(), input$group %>% alldata, drop = FALSE)

meanXvar

xvar

splitDataY % filteredData(), input$group %>% alldata, drop = FALSE)

meanYvar

yvar

}

else {

xvar

yvar

}

##############################################################################

#Plot Data with Visualization Customization

p1 = filteredData() %>%

ggvis(x = xvar, y = yvar) %>%

layer_points(size.hover := 200,

fillOpacity:= 0.5, fillOpacity.hover := 1,

fill = ~Species

) %>%

#Specifies the size of the plot

set_options(width = 800, height = 450, duration = 0)

})

#Actually plots the data

vis %>% bind_shiny("plot1")

}

#Run the Shiny App to Display Webpage

shinyApp(ui=ui, server=server)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值