簡體   English   中英

R shiny 根據輸入向小標題插入一列

[英]R shiny insert a column to tibble based on input

我有一個簡單的小標題,有 2 列, Column1Column2 如果 selectInput s1的輸入為No ,我想添加第三列Column3 ,但如果 selectInput s1的輸入為Yes ,則應該只有 2 列。

library(shiny)
library(tidyverse)

ui <- fluidPage(
  selectInput("s1", label = "Select", choices = c("Yes","No")),
  dataTableOutput("table1")
)

server <- function(input, output, session) {
  df = reactive({
    tibble(Column1 = rep(1,10), Column2 = rep(2,10))
  })
  
  output$table1 = renderDataTable({
    df()
  })
}

shinyApp(ui, server)

使用 Stéphane Laurent 的建議,我試圖想出一個答案,如果有根本性的錯誤,請糾正它。

library(shiny)
library(tidyverse)

ui <- fluidPage(
  selectInput("s1", label = "Select", choices = c("Yes","No")),
  dataTableOutput("table1")
)

server <- function(input, output, session) {
  df = reactive({
    tibble(Column1 = rep(1,10), Column2 = rep(2,10))
  })
  
  s2 = reactive({add_column(df(),Column3=1)})
  
  output$table1 = renderDataTable({
    if(input$s1=="Yes"){
      df()
    } else {s2()}
  })
  

}

shinyApp(ui, server)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM