![](/img/trans.png)
[英]R shiny use input from textInput to be the column name in a tibble
[英]R shiny insert a column to tibble based on input
我有一個簡單的小標題,有 2 列, Column1
和Column2
。 如果 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.