【问题标题】:In shiny / flexdashboards how can I change the values of columns through an input where multiple=TRUE?在闪亮/ flexdashboards中,如何通过multiple = TRUE的输入更改列的值?
【发布时间】:2019-12-20 00:54:55
【问题描述】:

当multiple = TRUE 时,我似乎无法弄清楚如何在 flexdashboard/shiny 中更改数据框中列的值。在下面的示例中,我可以通过单独选择 x 和 y 来更改它们。我想要完成的是当我同时选择 x 和 y 时,df$x 变为 3,df$y 变为 2。

但是当两者都被选中时,只有第一个被选中的会被改变。

---
title: "Test"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    social: menu
    source_code: embed
    runtime: shiny
---

```
library(flexdashboard)
library(shiny)
library(DT)

df <- data.frame(
  x = 1:10,
  y = 1:10)

selectInput("var", label = "Change:", 
        choices =  list("no change", "x","y"), 
        multiple =TRUE,
        selected = "no change"
        )

renderDataTable({
  if(input$var == "no change")  {df <- df} 
  if(input$var %in% "x")  {df$x <- 3}  
  if(input$var %in% "y")  {df$y <- 2} 
  return(df)
   })
```

任何帮助将不胜感激。

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    对于上下文,下面是上面代码的输出快照,上面没有应用程序输出:

    图片 0


    您几乎就在那里——只是在最后两个if 之前缺少else,以及相应的换行符(类似地,see this)。

    ---
    title: "Please remember to make [reprex posts](https://stackoverflow.com/help/minimal-reproducible-example) :)"
    output: html_document
    ---
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = TRUE)
    library(flexdashboard)
    library(shiny)
    library(DT)
    library(tidyverse)
    ```
    
    ## R Markdown
    
    This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
    
    When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
    
    ```{r now-including-else}
    
    df <- data.frame(
      x = 1:10,
      y = 1:10)
    
    selectInput("var", label = "Change:", 
                choices =  list("no change", "x","y"), 
                multiple =TRUE,
                selected = "no change"
                )
    
    renderDataTable({
      if(input$var == "no change")  {
        df <- df
      }
      else if(input$var %in% "x")  {
        df$x <- 3
      }
      else if(input$var %in% "y")  {df$y <- 2} 
      return(df)
    })
    ```
    

    附: Reprex posts.


    建议的代码输出的一些快照:

    图片 1

    图片 2

    图 3

    【讨论】:

    • 我已在序言中添加,但遗憾的是,当同时选择 x 和 y 时,您的代码似乎无法解决问题。
    • 能否再详细说明一下?
    • 我想要完成的是当我同时选择 x 和 y 时,df$x 变为 3,df$y 变为 2。
    猜你喜欢
    • 2020-02-05
    • 1970-01-01
    • 2019-10-23
    • 2018-02-09
    • 2020-08-02
    • 2021-04-18
    • 2016-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多