【问题标题】:Rendering the variables from the uploaded file从上传的文件中渲染变量
【发布时间】:2018-04-19 21:19:45
【问题描述】:

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

因此,我设计了带有 2 个子菜单项的仪表板,称为加载和准备,如下所示。

在第一个选项卡中,我正在尝试由用户上传文件。

在第二个选项卡中,准备我想显示用户选择的文件中的列名。例如,如果我的数据框有列名(ID、日期、接受、拒绝),那么我希望它们列在准备选项卡中。

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

下面是我创建的仪表板的快照。

这是用户界面代码:

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)

【问题讨论】:

  • 谁能帮我解决这个问题?
  • 请剥离您的可重现示例以使其尽可能小,有关提示,请参阅here。另外,我不清楚您的问题或目标是什么。
  • @Florian 我已经用我的仪表板设计图像和 UI 、服务器代码完全编辑了帖子。
  • 我觉得你应该看看reactiveValues
  • 是的,所以你可以反复读写来修改列。

标签: r shiny


【解决方案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")
})

希望对你有所帮助。

【讨论】:

    猜你喜欢
    • 2019-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-08
    • 2017-02-09
    • 1970-01-01
    • 1970-01-01
    • 2012-01-12
    相关资源
    最近更新 更多