从上传的文件中渲染变量

时间:2018-04-16 12:47:42

标签: r shiny

我正在尝试开发一个应用程序,用户可以上传文件并准备文件。

因此,我在仪表板上设计了2个子菜单​​项,称为“加载和准备”,如下所示。

在第一个标签中,我正在尝试由用户上传文件。

在第二个选项卡中,准备我想显示用户选择的文件中的列名。例如,如果我的数据框有列名(ID,Date,Accepeted,Rejected),那么我希望它们列在prep选项卡中。

列出后,用户应该能够选择他想要的数据类型。

以下是我创建的信息中心的快照。

Idea is to display all the column names from the file user has uploaded  in "select variable" and modify the variable as numeric or charchter or in date format

这是UI代码:

ui<-dashboardPage(
  dashboardHeader(title = "Model"),
  dashboardSidebar(
    sidebarMenu(id="tabs",
                menuItem("Data", tabName = "data", icon = icon("table"),startExpanded = TRUE,
                         menuSubItem("Load", tabName = "data1"),
                         menuSubItem("Prep", tabName = "prep")
                ),
                menuItem("Visualisation",icon=icon("bar-chart-o"), tabName = "vis"),
                menuItem("Result", icon=icon("cog"), tabName = "result")
    )
  ),
  dashboardBody(
    tags$style(type="text/css",
               ".shiny-output-error { visibility: hidden; }",
               ".shiny-output-error:before { visibility: hidden; }"
    ),
    tabItems(
      tabItem(tabName = "data1",
              fluidPage(
                fluidRow(
                  fileInput("file1","Choose CSV File",
                            accept = c("text/csv",
                                       "text/comma-seperated-values, text/plain",
                                       ".csv")
                  ),
                  tags$hr(),
                  checkboxInput("header", "Header", TRUE),
                  radioButtons("sep","Separator",
                               choices=c(Comma=",",
                                         semicolon=";",
                                         Tab="\t"),
                               selected = ";")
                ),
                mainPanel(
                  uiOutput("tb")
                )

                )),
    #--------Sub menu Item 2-----------
    tabItem(tabName = "prep",
            h1("Preprocessing"),
            fluidPage(
              fluidRow(
                uiOutput("loaded_tb"),
                selectInput('data_option','Select Option',
                            label="Select the Variable",
                            list(
                              "Variable Attributes"="var_attr",
                              "Data Summary ='data_summary"
                            ))
              ),
              radioButtons("class_selection", label="Variables Modeification",
                           choices = list(Numeric="numeric",Factor="factor",
                                          Character ="character",
                                          Date="date"),
                           selected = "numeric"),
              selectInput('date_format', "Select the Date Format",
                          list(
                            YMD ="ymd",
                            YDM ="ydm",
                            MYD ="myd",
                            DMY ="dmy",
                            DYM ="dym"
                          )),
              tags$h5("Date Preview"),
              verbatimTextOutput('date_preview'),
              actionButton("var_modify", "Modify")
              ),
                 mainPanel(
                   uiOutput("Pre")

              )
            ))
  )
) 

这是服务器代码:

server <- shinyServer(function(input,output){
  data <- reactive({
    file1 <- input$file1
    if(is.null(file1)){return()}
    read.csv(file = file1$datapath, sep=input$sep)
  })

  output$filedf <- renderTable({
    if(is.null(data())){return()}
    input$file1
  })
  output$sum <- renderTable({
    if(is.null(data())){return()}
    summary(data())
  })
  output$table <- renderTable({
    if(is.null(data())){return()}
    data()
  })
  output$tb <- renderUI({
    if(is.null(data())){return()}
    tabsetPanel(tabPanel("About file", tableOutput("filedf")),tabPanel("Data", tableOutput("table")),tabPanel("Summary", tableOutput("sum")))

  })

  #----- Data Preparation------

  output$Pre <- renderUI({checkboxGroupInput(inputId = "select_vars",
                                             choices = names(data))
  })
  data_sel <- reactive({
    req(input$select_vars)
    data_sel<- data()%>% select(input$select_var)
  })
})

shinyApp(ui,server)

1 个答案:

答案 0 :(得分:1)

这不是一个答案,因为我们不了解你真正想做的事情。但是,这里有一些技巧可以帮助你继续前进。

data()

不要将data用作变量名称。检查data()是否为空或使用names(data())可能会导致意外错误。

将数据框存储为reactiveValues

根据@Florian的建议,如果您想直接编辑数据,可以使用reactiveValues。已发布一个很好的答案here,下面是您案例的相关代码段。

values <- reactiveValues(df_data = NULL)

observeEvent(input$file1, {
  values$df_data <- read.csv(input$file1$datapath, sep = input$sep)
}) 

现在,您不必使用data()来检索数据框,而是必须使用values$df_data

从上传的文件

中渲染变量

您充分利用了renderUI,因为您希望输入选择器依赖于您的数据变量,但您的uiOutput永远不会链接到您上传的文件变量。这是一种为变量选择器编辑uiOutput的方法。

output$Pre <- renderUI({
  checkboxGroupInput(inputId = "select_vars",
                     label = "foo",
                     selected = names(values$df_data),
                     choices = names(values$df_data))
})

output$ui_data_option <- renderUI({
  selectInput('data_option',
              choices = names(values$df_data),
              label = "Select the Variable")
})

希望这对你有所帮助。

相关问题