r - Unable to append cluster membership from kmeans to the raw data in Shiny -
i trying small shiny kmeans exercise download csv file , run kmeans on (ignoring required preprocessing steps)---after getting cluster, want append these cluster numbers original data , output in interactive datatable(from dt package)......but running error....code below....
library(shiny) # loading required packages pacman::p_load(amelia,broom,caret,cluster,clustertend,clvalid,corrplot,dbscan,dplyr,dt,data.table,forecast,fpc,fpdclustering,fpp,ggally,ggfortify,ggraph,ggplot2,ggrepel,ggthemes,gmodels,googlevis,gridextra,igraph,knitr,mice,missforest,nbclust,optcluster,pacman,plyr,purrr,qcc,randomforest,rcharts,reshape2,tibble,tidyr,tidyverse,tsa,tseries,vegan,vim,zoo) # add 'caret',`iipr`,'ggthemes','ggraph',igraph,vim,missforest list when using script in spark envir #comparegroups library(markdown) library(imputets) # define ui application ui <- navbarpage( # application title titlepanel("shinyapp "), # sidebar layout input , output definitions ---- sidebarlayout( # sidebar panel inputs ---- sidebarpanel( # input: select file ---- fileinput("dataset", "choose csv file", multiple = true, accept = c("text/csv", "text/comma-separated-values,text/plain", ".csv")), # include clarifying text ---- helptext("note: first select dataset of csv format app give insight!!"), # horizontal line ---- tags$hr(), # input: checkbox if file has header ---- checkboxinput("header", "header", true), # input: select separator ---- radiobuttons("sep", "separator", choices = c(comma = ",", semicolon = ";", tab = "\t"), selected = ","), # horizontal line ---- tags$hr(), # input: actionbutton() defer rendering of output ---- # until user explicitly clicks button (rather # doing when inputs change). useful if # computations required render output inordinately # time-consuming. actionbutton("update", "update button", class = "btn-primary"), tags$hr() ), mainpanel( tabsetpanel( navbarmenu("kmeans", tabpanel("raw data cluster membership", # output: interactive dt table ---- h4("cluster table"), dt::datatableoutput("cluster_table") ) ), tabpanel("random forest", "this panel intentionally left blank") ) ) ) ) # define server logic server <- function(input, output) { datasetinput <- eventreactive(input$update, { read.csv(input$dataset$datapath, header = input$header, sep = input$sep) }, ignorenull = false) #selecting numeric variables ms.num<- reactive({sapply(datasetinput(), is.numeric)}) ms.data.in.num <- reactive({datasetinput()[ , ms.num()]}) # imputing nas zeros df<- reactive({imputets::na.replace(ms.data.in.num(), 0)}) # keeping sample of 10k modeling sample_data <-reactive({df()[1:10000,]}) #### kmeans opt.cluster=9 set.seed(115) ms.data.kmeans.mdl <- reactive({kmeans(scale(sample_data()),opt.cluster,nstart=25)}) # appending clusters raw sample data ms.data_kmeans<-reactive({ x<-ms.data.kmeans.mdl()$cluster sample_data()$cluster.kmeans <-x }) output$cluster_table <- renderdatatable({ dt::datatable(ms.data_kmeans()) }) } # run application shinyapp(ui = ui, server = server)
i getting following error:
error in <-: invalid (null) left side of assignment stack trace (innermost first): 96: <reactive:ms.data_kmeans> [c:\users\admin\documents\shiny_test/app.r#124] 85: ms.data_kmeans 84: base::rownames 83: dt::datatable 82: exprfunc [c:\users\admin\documents\shiny_test/app.r#128] 81: widgetfunc 80: func 79: origrenderfunc 78: renderfunc 77: origrenderfunc 76: output$cluster_table 1: runapp
dont know doing wrong??
found solution...
appending clusters raw sample data
x<-reactive({ cluster<-ms.data.kmeans.mdl()$cluster cluster }) output$x1 <- renderprint({ dataset <- x() table(dataset) }) add_to_df <- reactive({ sample_data1<-cbind(sample_data(),x()) sample_data1 }) output$cluster_table <- renderdatatable({ dt::datatable(add_to_df()) })
just had use cbind() here....
Comments
Post a Comment