r闪亮使用数据框列进行选择

时间:2019-01-29 06:07:06

标签: r shiny

我真的是R的新手,所以这可能是一个简单而明显的解决方法。

我正在尝试创建一个函数,我可以调用该函数来使用数据框中列的名称来创建selectInput框,选择为

数据集是数据框的名称,特征是数据框内的列的名称。我试图将特征名称用作输入框的标题,将列中的值用作下拉框选项,并将输入存储在名为I(characteristic)的变量中。

NewCharacterBox <- function(Characteristic, Dataset)
  fluidRow(
    column(3,
           selectInput(paste("I", toString(Characteristic), sep = ""), h5(toString(Characteristic)),
                       choices = Dataset$Characteristic 

1 个答案:

答案 0 :(得分:0)

如果我理解正确,那么您可能不需要新的用户定义函数,而只需使用names(dataset)。下面是一个例子。

library(shiny)

data = iris

ui = fluidPage(

  selectInput("choice","List of Columns", 
              choices = names(data),
              selected = NULL),
  textOutput("selected"),
  tableOutput("result")
)


server = function(input, output) {

  output$selected <- renderText({
    paste("You have selected", input$choice)
  })

  output$result <- renderTable({
    data[,input$choice]
  })
}




shinyApp(ui,server)