【问题标题】:How to read a specific column of data from .csv file?如何从 .csv 文件中读取特定列的数据?
【发布时间】:2020-08-28 13:53:12
【问题描述】:

我正在尝试制作一个闪亮的应用程序,它可以读取 .csv 文件并在应用程序中显示特定数据。例如,如果我选择数字 3 并键入 2,则应用程序中将显示“H”。

以下是示例代码:

library(shiny)
library(shinydashboard)
library(shinyWidgets)

ui = dashboardPage(
     dashboardHeader(title = "Selection"),
     dashboardSidebar(disable = TRUE), 
     dashboardBody(uiOutput("body")))



server = function (input, output, session) {

  output$body <- renderUI({ 
    box(numericInput('number', "Choose a number:", value = NULL, min = 1, max = 5),
        br(),
        selectInput('type', "Choose a type:", choices = list("Type 1", "Type 2")),
        br(),
        textOutput("my_selection"),
        br()

    )})


  selection_table <- reactive({read.csv("selection.csv", header = T, sep = "," , check.names = FALSE, col.names = TRUE)})


  output$my_selection = reactive({
    selection = ifelse(input$number == 1 & input$type == "Type 1", selection_table()$selection_1[1], 
                       ifelse(input$number == 2 & input$type == "Type 1", selection_table()$selection_1[2],
                              ifelse(input$number == 3 & input$type == "Type 1", selection_table()$selection_1[3],
                                     ifelse(input$number == 4 & input$type == "Type 1", selection_table()$selection_1[4],
                                            ifelse(input$number == 5 & input$type == "Type 1", selection_table()$selection_1[5],
                                                   ifelse(input$number == 1 & input$type == "Type 2", selection_table()$selection_2[1], 
                                                          ifelse(input$number == 2 & input$type == "Type 2", selection_table()$selection_2[2],
                                                                 ifelse(input$number == 3 & input$type == "Type 2", selection_table()$selection_2[3],
                                                                        ifelse(input$number == 4 & input$type == "Type 2", selection_table()$selection_2[4],
                                                                               ifelse(input$number == 5 & input$type == "Type 2", selection_table()$selection_2[5]))))))))))




    selection
  })

}
shinyApp(ui,server)

这是 .csv 文件中的示例数据:

age selection_1   selection_2
1    A             F
2    B             G
3    C             H
4    D             I
5    E             J

感谢任何帮助!谢谢!

【问题讨论】:

    标签: r csv shiny


    【解决方案1】:

    如果 csv 中的数据在应用程序运行时没有动态更改,那么我会将 read.csv 移动到标题中,以便在应用程序启动时将其加载到内存中。

    library(shiny)
    library(shinydashboard)
    library(shinyWidgets)
    
    ui = dashboardPage(
         dashboardHeader(title = "Selection"),
         dashboardSidebar(disable = TRUE), 
         dashboardBody(uiOutput("body")))
    
    df <- read.csv("selection.csv", header = T, sep = "," , check.names = FALSE, col.names = TRUE)
    
    server = function (input, output, session) { #### ...rest of your app code
    
    

    您的嵌套 ifelse 代码很复杂 - 尝试重命名 csv 中的数据以使其更容易进行子集可能会更容易,例如

    output <- df[input1, input2] 
    

    ...并将该子集操作的输出传递给 UI。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-23
      • 1970-01-01
      相关资源
      最近更新 更多