【问题标题】:selectInput based on columns of uploading csv fileselectInput 基于上传 csv 文件的列
【发布时间】:2018-04-30 14:25:05
【问题描述】:

我正在尝试构建一个闪亮的应用程序,我可以在其中上传 csv 文件并基于列名,来自 selectInput - 应用程序显示基本统计数据(图表、描述性统计平均值、sd、var .... 和一些统计测试)。 应用程序中已有的数据集没有问题。统计也没有问题。 问题是如何使 selectInput 使用新上传的 csv 文件中的列名。

这是我的精简版代码:

用户界面:

library(shiny)
shinyUI(fluidPage(
    titlePanel("xxx"),

    sidebarLayout(
        sidebarPanel(
            fileInput("csvFile", "Drag cars.csv over here!"),
            selectInput("var", "choose variable:", choices= names(data()))
        ),

        mainPanel(
            textOutput("average"),
            plotOutput("plot")
        )
    )
))

服务器:

shinyServer(function(input, output, session) {
    data <- reactive({
        file1 <- input$csvFile
        if (is.null(file1)) { 
            return() 
        } 
        data = read.csv(file=file1$datapath)
        data
    })
    output$average <- renderText({
        paste("average is: ", mean(data()[,input$var]))
    })
    output$plot <- renderPlot({
        boxplot(data()[,input$var])
    })
})

【问题讨论】:

  • 看看updateSelectInput()。此外,避免将任何名称命名为 "data",您可能会受益。尽管它可以工作,但由于名为 data() 的内置函数,它可能会令人困惑

标签: r shiny


【解决方案1】:

您将需要使用 uiOutput() 和 renderUI() 函数。

library(shiny)

ui <- fluidPage(
  titlePanel("xxx"),

  sidebarLayout(
    sidebarPanel(
      fileInput("csvFile", "Drag cars.csv over here!"),
      uiOutput("var_ui")
    ),

    mainPanel(
      textOutput("average"),
      plotOutput("plot")
    )
  )
)

server <- function(input, output, session) {
  data <- reactive({
    file1 <- input$csvFile
    if (is.null(file1)) { 
      return() 
    } 
    data = read.csv(file=file1$datapath)
    data
  })
  output$average <- renderText({
    paste("average is: ", mean(data()[,input$var]))
  })
  output$plot <- renderPlot({
    boxplot(data()[,input$var])
  })

  output$var_ui <- renderUI({
    selectInput("var", "choose variable:", choices= names(data()))
  })
}

shinyApp(ui, server)

【讨论】:

    【解决方案2】:

    您也可以在服务器中使用observeobserve 不返回任何内容。与reactive 不同,它会立即响应(而不是懒惰)。它最适合用于 ip/op 操作。

    observe({
      updateSelectInput(
        session,
        "var",
        choices = names(data())
      )
    )}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-12
      • 1970-01-01
      • 1970-01-01
      • 2022-01-15
      • 1970-01-01
      相关资源
      最近更新 更多