user46759
user46759

Reputation: 103

Access to a variable inside shiny r and reuse it

I have defined a global variable called y_kmeans in clustering plot as following : and I want to use this variable later in a table:

Server.R

  output$plot4<-renderPlot({
        df<-rawdata()
        #clustering of Related IP address and the number of movie downloads and number of user_id
        df<- na.omit(df)
        df5 <-df[3:4]
        #%>% dplyr::select(user_id,pa_content_name)
        #set.seed(12)
        split <- sample.split(df5, SplitRatio = 0.85)
        training_set <- subset(df5, split == TRUE)
        test_set <- subset(df5, split == FALSE)
        # Feature Scaling
        training_set = scale(training_set)
        test_set = scale(test_set)
        # Using the elbow method to find the optimal number of clusters
        # Fitting K-Means to the dataset
        set.seed(28)
        kmeans = kmeans(x = df5, centers = 3)
        y_kmeans <<- kmeans$cluster

      })

So To add this variable in a table I have used following codes:

#Demand of titles 
  GroupingVal <- reactive({
    df<-rawdata()
    #Grouping the Values based on clustering
    df$cluster<-kmeans$cluster
    df<-df %>% mutate(group=ifelse(cluster==1,"A",
                                   ifelse(cluster==2,"B",
                                          ifelse(cluster==3,"outlier","outlier"
                                          ))))
    df
  })

and the code related to renderDatatable is as follows:

 output$filteredtbl<-DT::renderDataTable({
    if(is.null(GroupingVal)){return()}
        DT::datatable(GroupingVal(),extensions="Responsive",options=list(pageLength=3),class='cell-border strip',selection='single')
      })

The error that I received is "object of type 'closure' is not subsettable" could you please let me know where is the problem.

Upvotes: 1

Views: 51

Answers (1)

user
user

Reputation: 592

Here is the way that you can get it:

k_means<-reactive({
    #clustering to find the groups of passionate IPS
    df<-rawdata()
    #clustering of Related IP address and the number of movie downloads and number of user_id
    df<- na.omit(df)
    df5 <-df[3:4]
    #%>% dplyr::select(user_id,pa_content_name)
    #set.seed(12)
    split <- sample.split(df5, SplitRatio = 0.85)
    training_set <- subset(df5, split == TRUE)
    test_set <- subset(df5, split == FALSE)
    # Feature Scaling
    training_set = scale(training_set)
    test_set = scale(test_set)
    # Using the elbow method to find the optimal number of clusters
    # Fitting K-Means to the dataset
    set.seed(28)
    kmeans = kmeans(x = df5, centers = 3)
    y_kmeans <<- kmeans$cluster
    y_kmeans
  })

And then call the k_means() in the GroupingVal reactive function:

GroupingVal <- reactive({
    df<-rawdata()
    df$cluster<-k_means()
    df<-df %>% mutate(group=ifelse(cluster==1,"A",
                                   ifelse(cluster==2,"B",
                                          ifelse(cluster==3,"outlier","outlier"
                                          ))))
    df
  })

And then call it in renderDatatable:

output$filteredtbl<-DT::renderDataTable({
    if(is.null(GroupingVal)){return()}
    DT::datatable(GroupingVal(),extensions="Responsive",options=list(pageLength=3),class='cell-border strip',selection='single')
  })

Upvotes: 1

Related Questions