簡體   English   中英

R,Shiny:使用selectInput()選擇創建數據框

[英]R,Shiny: Using selectInput() selection to create data frame

看來應該沒什么大不了的,但是我顯然缺少了一些東西。 我想從selectInput()進行選擇以創建數據框。 selectInput()產生的字符輸出似乎不能用作創建數據框的輸入。 我的ui.R和server.R代碼如下。 df<-data.frame(input$select, header=TRUE)行未產生預期的輸出。 提前感謝你的幫助。

ui.R

library(shiny)

shinyUI(

  # Use a fluid Bootstrap layout
  fluidPage(    

    # Give the page a title
    titlePanel("Select Test Dataset"),

    # Generate a row with a sidebar
    sidebarLayout(      

      # Define the sidebar with one input
      sidebarPanel(
        selectInput("select", "Test Dataset", 
                    choices= list("Model Year 2011" = 'cars2011', "Model Year 2012" = 'cars2012'),selected='cars2011')

      ),

      # mainPanel Output
      mainPanel(
        verbatimTextOutput("value"),
        verbatimTextOutput("type"),
        dataTableOutput(outputId="data")
      )

    )
  )
)

server.R

library(AppliedPredictiveModeling)
data(FuelEconomy)



shinyServer(function(input, output) {

  output$value <- renderPrint({ input$select })
  output$type <- renderPrint({class(input$select)})

  output$data <- reactive({
    df <- data.frame(input$select, header=TRUE)
    head(df)
  })




})

定義output$data時,應使用shiny::renderDataTable而不是shiny::renderDataTable shiny::reactive

output$data <- renderDataTable({
    df <- data.frame(input$select, header=TRUE)
    head(df)
})

使用switch語句將字符串轉換為數據框名稱似乎可行。

在server.R中插入:

數據集<-反應性({switch(input $ select,“ cars2011” = cars2011,“ cars2012” =汽車2012)})

然后使用head(dataset())輸出數據可提供所需的結果。

暫無
暫無

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

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