[英]Add user input column to Shiny
我正在嘗試收集給定數據集的用戶輸入。 我想插入一列,用戶可以確定他們是否要擁有mtdata
集中的一輛車。 這是完全主觀的,因為每個人的意見不同,所以我無法對此進行編程。是否可以添加一個額外的列,該列可以是復選框或下拉菜單,以標識用戶“想要擁有?”的汽車。
library(shiny)
shinyApp(ui = shinyUI(fluidPage(
titlePanel("Interesting Cars"),
sidebarLayout(
sidebarPanel(
helpText("This is a side bar")),
mainPanel(
tableOutput("view")
)
)
)),
server = function(input, output) {
output$view <- renderTable({
head(mtcars[, 1:4], n = 6)
})
})
怎么樣,您可以使用DT
庫。 通過添加filter
選項,用戶可以定義所需的不同組件,並查看將要行駛的汽車。
library(shiny)
library(DT)
shinyApp(ui = shinyUI(fluidPage(
titlePanel("Interesting Cars"),
sidebarLayout(
sidebarPanel(
helpText("This is a side bar")),
mainPanel(
DT::dataTableOutput("view")
)
)
)),
server = function(input, output) {
output$view <- DT::renderDataTable({
datatable(mtcars,
filter = "top"
)
})
})
編輯
如果真的很重要,那么添加另一列來表明它是否“有趣”,如果您打算讓用戶在不同的條件下分配它,那么將要編寫的代碼要多得多。 這只是mpg
的示例。 這里的基本思想是將數據分配給reactiveValues
函數。 然后可以根據需要進行修改。 顯然可以進一步改進(因為它將繼續添加列),但是它演示了這一概念。
shinyApp(ui = shinyUI(fluidPage(
titlePanel("Interesting Cars"),
sidebarLayout(
sidebarPanel(
helpText("This is a side bar"),
uiOutput("mpg"),
actionButton("add_label", "Mark Interesting")
),
mainPanel(
DT::dataTableOutput("view")
)
)
)),
server = function(input, output) {
values <- reactiveValues(
mydata = mtcars
)
output$mpg <- renderUI({
numericInput("mpg_input", "MPG Cutoff?",
value = 15
)
})
output$view <- DT::renderDataTable({
datatable(values$mydata
)
})
observeEvent(input$add_label, {
validate(
need(!is.null(input$mpg_input), "need mpg value")
)
values$mydata <- data.frame(values$mydata,
Interesting_Flag =
ifelse(values$mydata$mpg > input$mpg_input,
"Interesting",
"Not Interesting"))
})
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.