简体   繁体   English

如何从.csv文件中读取特定列的数据?

[英]How to read a specific column of data from .csv file?

I am trying to make a Shiny App which can read a.csv file and display the specific data in the app.我正在尝试制作一个 Shiny 应用程序,它可以读取一个.csv 文件并在应用程序中显示特定数据。 For example, if I choose number 3 and type 2, "H" will be displayed in the app.例如,如果我选择数字 3 并键入 2,则应用程序中将显示“H”。

Below are the sample codes:以下是示例代码:

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)

And this is the sample data in the.csv file:这是.csv文件中的示例数据:

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

Appreciate any help!!感谢任何帮助! Thanks!谢谢!

If the data in the csv isn't being changed dynamically when the app is running then I would move the read.csv to the header so that it is loaded once into memory when the application is launched. If the data in the csv isn't being changed dynamically when the app is running then I would move the read.csv to the header so that it is loaded once into memory when the application is launched.

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

Your nested ifelse code is complicated - it would probably be easier to try and rename the data in the csv to make it easier to sub-set, eg您的嵌套 ifelse 代码很复杂 - 尝试重命名 csv 中的数据以使其更容易进行子集可能会更容易,例如

output <- df[input1, input2] 

...and pass the output of that subsetting operation to the UI. ...并将该子集操作的 output 传递给 UI。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM