如何显示文件中的变量并更改数据类型

时间:2018-04-16 15:13:21

标签: r shiny shinydashboard

我正在创建一个应用程序,我上传文件并显示上传文件中的变量,并根据需要从用户更改其数据类型。

在大多数情况下,它们是数字/日期。

因此,我尝试了下面的代码。在上半部分,我创建了文件上传代码,在下半部分,我试图显示变量的复选框组输入。

我在下面的代码中遇到两个问题。

在选择变量中,它没有显示变量名称,而是找到了与复选框无关的内容。

另一方面,我希望它们在我的子菜单项标签“prep”中,我不知道为什么我在菜单项Load中找到它们。

下面是截图,可帮助您找出菜单项之间的内容。

Dashboard design

这是我的代码,到目前为止我已尝试过。

library(shiny)
library(shinydashboard)
library(ggplot2)
library(DT)

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")
                )
              )
      )
    ),
    tabItem(tabName = "prep",
            fluidPage(
              fluidRow(
                 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",
                                            label="Select Variables",
                                            choices = names(filedf))

   })
filedf_sel <- reactive({
  req(input$select_vars)
  filedf_sel<- data()%>% select(input$select_var)
})
})

shinyApp(ui,server)

任何人都可以帮助我如何避免这些错误,然后构建我如何改变他们的数据类型

1 个答案:

答案 0 :(得分:1)

#----- Data Preparation------
 output$Pre <- renderUI({checkboxGroupInput(inputId = "select_vars",
                                         label="Select Variables",
                                         choices = colnames(data())) #to retrive the data frame column names

 })
filedf_sel <- reactive({
  req(input$select_vars)
  filedf_sel<- data()%>% select(input$select_vars)  #possible typo
})

我不确定你想要什么,但我希望这在某种程度上有所帮助。

相关问题