【问题标题】:How to take userinput in flexdashboard shiny as number of cluster to run kmeans in r?如何将 flexdashboard 中的用户输入作为集群数量在 r 中运行 kmeans?
【发布时间】:2021-06-24 20:05:56
【问题描述】:

我是 shiny/flexdashboard 的新手,到目前为止,我已经能够在 req(input$user_input_value) 的帮助下使用来自 selectInput 的值来渲染图和过滤数据框。

问题:要运行 kmeans,我将 user input 获取 number of clusters,但我无法在 响应式格式并出现错误:闭包类型的对象不可子集

```{r setup, include=FALSE}
library(flexdashboard)
library(shiny)
library(tidyverse)
library(tidytext)
library(scales)
library(glue)
library(widyr)
library(factoextra)
```

df

                               1            2           3            4
Angola               -0.08260540  0.034325891 -0.02013353 -0.014063951
Armenia              -0.06613693 -0.044308626 -0.13230387 -0.024534033
Azerbaijan           -0.07562365 -0.003670707  0.05886792 -0.219660410
Bahrain              -0.08275891  0.035843793 -0.02280102 -0.008044934
Bangladesh           -0.08306371  0.032998297 -0.02634819 -0.017627316
Bosnia & Herzegovina -0.06303898 -0.050781511 -0.15183954  0.016794674

(注意:我已将 csv 文件放在 github 并在下面提到其链接。对于 kmeans,字符列应用作代表 country 的行名。)

更新的 df 创建步骤

svd_dimen_all_wide <- read.csv(url("https://raw.githubusercontent.com/johnsnow09/covid19-df_stack-code/main/svd_dimen_all_wide.csv"))

svd_dimen_all_wide <- as.data.frame(svd_dimen_all_wide)

rownames(svd_dimen_all_wide) <- svd_dimen_all_wide$X

svd_dimen_all_wide <- svd_dimen_all_wide[,2:ncol(svd_dimen_all_wide)]

flexdashboard

---
title: "UN Country Votes"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
    theme: space
runtime: shiny
---

```{r setup, include=FALSE}
library(flexdashboard)
library(shiny)
library(tidyverse)
library(tidytext)
library(scales)
library(glue)
library(widyr)
library(factoextra)
Page NAme 
=====================================



Inputs {.sidebar}
-----------------------------------------------------------------------


```{r}
  selectInput("number_of_clusters", label = h3("Number of Clusters"), 
      choices = c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15) , 
                selected = 6)
```


Column {data-width=1000}
-----------------------------------------------------------------------

```{r include=FALSE}
set.seed(123)

km.res <- reactive({
  
      # req(input$number_of_clusters)                
  
      kmeans(svd_dimen_all_wide, as.numeric(input$number_of_clusters), nstart = 25)
    })

      df_with_cluster <- cbind(svd_dimen_all_wide, cluster = km.res$cluster)

      df_with_cluster <- rownames_to_column(df_with_cluster, "country")

      df_with_cluster <- df_with_cluster %>% 
       select(country, cluster, everything())
```

更新尝试:


renderPrint({
  df_with_cluster <- cbind(svd_dimen_all_wide, cluster = km.res()$cluster)
  
  df_with_cluster <- rownames_to_column(df_with_cluster, "country")

  df_with_cluster <- df_with_cluster %>%
                      select(country, cluster, everything())
  
  head(df_with_cluster)
})
### Comparison of Countries on Yes% of Bi Words

```{r}
renderPlot({
  world_data %>%
  left_join((df_with_cluster %>%
               mutate(country_code = countrycode(country, "country.name", "iso2c"))
             ),
            by = c("country_code")) %>%
  filter(!is.na(cluster)) %>%
  ggplot(aes(x = long, y = lat, group = group,
             fill = as.factor(cluster))) +
  geom_polygon() +
  theme_map() +
  scale_fill_discrete() +
  labs(fill = "cluster",
       title = "World Clusters based on UN voting",
       caption = "created by ViSa") +
  theme(plot.title = element_text(face = "bold", size = 16))
}) 
```

【问题讨论】:

    标签: r shiny k-means flexdashboard


    【解决方案1】:

    问题出在反应性块中。反应式表达式 km.res 使用输入的集群数量,运行模型并保存输出。 (让我们在这里结束代码块)。

    接下来,决定你想对输出做什么?

    • 要打印结果,请使用 renderPrint
    • 要显示为绘图,请使用 renderPlot,
    • 显示为表格、用户渲染表格等。

    现在让我们使用 renderPrint() 打印模型的输出,可以通过调用表达式名称后跟括号来访问输出,例如 km.res()

       Column {data-width=1000}
        -----------------------------------------------------------------------
        
        ```{r include=FALSE}
        
        km.res <- reactive({
          
              req(input$number_of_clusters)
          
              set.seed(123)
          
              kmeans(svd_dimen_all_wide, as.numeric(input$number_of_clusters), nstart = 25)
            })
         ```
    
    
    
    ###
    
    ```{r model}
    renderPrint({
    
    df_with_cluster <- cbind(svd_dimen_all_wide, cluster = km.res()$cluster)
    head(df_with_cluster)
    
    })
    ```
    

    这是我与这个问题非常相关的博文https://towardsdatascience.com/build-an-interactive-machine-learning-model-with-shiny-and-flexdashboard-6d76f59a37f9?sk=922526470699966c3f47b24843404a15

    【讨论】:

    • 没有 flexdashboard 我会执行:km.res &lt;- kmeans(svd_dimen_all_wide, 6, nstart = 25)。这我将进一步用于数据准备,然后最终绘制它,但这会给出错误(用我尝试过的代码更新了帖子)
    • 我认为df_with_cluster &lt;- cbind(svd_dimen_all_wide, cluster = km.res()$cluster) 代码会产生问题。如果没有仪表板或用户输入,我会以df_with_cluster &lt;- cbind(svd_dimen_all_wide, cluster = km.res$cluster) 的形式执行此操作
    • 您是否在同一个响应式代码块中运行代码?创建响应式 km.res 后,打开一个新块,并在 renderPrint 函数中逐行添加代码。
    • 现在df_with_cluster 已经在renderPrint() 内部工作了,但是当我使用这个df_with_cluster 数据框来绘制它有或没有renderPlot() 时它不起作用。我需要做些什么才能在 renderPrint() 之外使用df_with_cluster 吗? (代码已更新)
    • 您更新的代码不可重现,但如果您想在不同的代码块中使用 df_with_cluster,您也应该使其具有响应性。例如:{r cluster reactive} df_with_cluster &lt;- reactive({cbind(svd_dimen_all_wide, cluster = km.res()$cluster) }) 然后### plot {r} renderPlot({ plot(df_with_cluster()$cluster) })
    猜你喜欢
    • 2023-03-06
    • 2019-10-31
    • 2018-01-03
    • 1970-01-01
    • 1970-01-01
    • 2014-06-24
    • 2017-01-30
    • 2017-06-01
    • 2019-01-04
    相关资源
    最近更新 更多