小编Naj*_*j S的帖子

R闪亮; 如何使用selectInput中的多个输入传递到dplyr中的"select"选项?

我有一个应用程序,其中我希望在"ui"文件中获取用户输入并使用该信息更新"服务器"文件中的数据帧.以下是代码的简化版本:

Dataframe <- readRDS(Dataframe.rds)
Table <- readRDS(Table.rds)    

ui <- fluidPage(
     selectInput("Location","Location",
              unique(as.character(Table$Locations)), multiple = TRUE)
                )

server <- function(input,output) {
 Dataframe2 <- Dataframe %>% select(get(input$Location))
                                 }
Run Code Online (Sandbox Code Playgroud)

如果我没有对selectInput使用"multiple = TRUE"选项,则上述代码有效,这意味着Dataframe2对象仅选择与用户选择的单个输入匹配的列.但是,我不知道如何为多个输入做同样的事情,当选择可能从selectInput传递的最多1个项目到总共10个项目.

r shiny dplyr

10
推荐指数
1
解决办法
1万
查看次数

如何使用dplyr语法在R中编写循环"for"循环

我有一个广泛的代码块,我在R中使用dplyr语法编写.但是,我试图将该代码放在一个循环中,这样我最终可以创建多个输出文件而不是一个.不幸的是,我似乎无法这样做.

为了说明我的问题,让我们参考R中常用的"虹膜"数据集:

      > data("iris")
      > str(iris)
      'data.frame': 150 obs. of  5 variables:
      $ Sepal.Length: num  
      $ Sepal.Width : num  
      $ Petal.Length: num  
      $ Petal.Width : num  
      $ Species     : Factor w/ 3 levels "setosa","versicolor","virginica"
Run Code Online (Sandbox Code Playgroud)

让我们说我想保存物种"杂色"的平均Petal.Length.dplyr代码可能如下所示:

    MeanLength2 <- iris %>% filter(Species=="versicolor")
                       %>% summarize(mean(Petal.Length)) %>% print()
Run Code Online (Sandbox Code Playgroud)

哪个会给出以下价值:

      mean(Petal.Length)
    1               4.26
Run Code Online (Sandbox Code Playgroud)

让我们尝试创建一个循环来获得所有物种的平均花瓣长度.

从我对循环的了解很少,我想做这样的事情:

     for (i in unique(iris$Species))
      {
       iris %>% filter(iris$Species==unique(iris$Species)[i]) %>%
        summarize(mean(iris$Petal.Length)) %>% print()
        print(i) 
       }
Run Code Online (Sandbox Code Playgroud)

出于某种原因,我必须在循环内指定数据框和列,在使用dplyr的管道功能时通常不是这种情况.我假设这表明问题所在.

无论如何,上面的代码给出了以下输出:

          mean(iris$Petal.Length)
     1                   3.758
     [1] "setosa"
          mean(iris$Petal.Length)
     1                   3.758
     [1] "versicolor"
          mean(iris$Petal.Length)
     1                   3.758
     [1] …
Run Code Online (Sandbox Code Playgroud)

loops r dplyr

6
推荐指数
2
解决办法
1万
查看次数

在 flexdashboard 中,是否可以单击 valueBox 来更新文本框,例如 actionButton?

我正在尝试利用valueBoxinflexdashboard来显示标题数字。但是,我也希望valueBox可以像 一样actionButton,因为单击valueBox应该会触发仪表板中其他位置的操作。

在检查flexdashboard文档时,我看到以下相关位valueBox

关联值框

valueBox(42, icon = "fa-pencil", href="#details")
Run Code Online (Sandbox Code Playgroud)

其中单击valueBox将用户导航到具有“#details”锚点的不同页面。但是,没有任何迹象表明单击valueBox可用于其他操作。

下面是一个最小的相关 flexdashboard 代码

---
title: "valueBox Links"
output: 
  flexdashboard::flex_dashboard:
runtime: shiny
---

```{r setup, include=FALSE}
library(flexdashboard)

    ```

Tab 1 - Test
======================================================================

Column 
-------------------------------------

#### Three valueBoxes

### valueBox 1
```{r}
valueBox(1)
    ```

### valueBox 2
```{r}
valueBox(2)
    ```

### valueBox 3
```{r}
valueBox(3)
    ```

Column
-------------------------------------

### Text output
This is …
Run Code Online (Sandbox Code Playgroud)

r shiny flexdashboard

3
推荐指数
1
解决办法
2355
查看次数

标签 统计

r ×3

dplyr ×2

shiny ×2

flexdashboard ×1

loops ×1