Доступ к переменной внутри блестящего г и ее повторное использование - PullRequest
0 голосов
/ 29 января 2019

Я определил глобальную переменную y_kmeans на графике кластеризации следующим образом: и я хочу использовать эту переменную позже в таблице:

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

      })

Итак, чтобы добавитьэта переменная в таблице я использовал следующие коды:

#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
  })

и код, связанный с 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')
      })

Я получил ошибку "объект типа"замыкание «не подмножество», пожалуйста, дайте мне знать, в чем проблема.

1 Ответ

0 голосов
/ 30 января 2019

Вот способ, которым вы можете его получить:

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
  })

И затем вызвать k_means () в реактивной функции GroupingVal:

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
  })

И затем вызвать его в 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')
  })
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...