在闪亮中更改变量的数据类型

时间:2018-08-22 05:55:24

标签: r shiny

我写了一个代码,用于选择CheckboxGroupInputShiny中的变量来上传文件,汇总数据和更改数据类型。但是我遇到了一些错误,例如:

  

警告:observeEventHandler中的错误:(列表)对象不能被强制键入“ double”。

有人可以帮我解决这个问题吗?

我的代码:

 library(shiny)
    library(readxl)
    library(summarytools)
    # install.packages("devtools")
    library(devtools)
    # install_github('dcomtois/summarytools', ref='dev-current')

    ui <- fluidPage(
      titlePanel("Data Visualization"),
      sidebarLayout(
        sidebarPanel(
          radioButtons(
            "fileType_Input",
            label = h4("Choose File type"),
            choices = c(".csv/txt" = 1, ".xls/.xlsx" = 2),
            selected = 1,
            inline = TRUE
          ),
          fileInput(inputId = 'file1', label = '1.Upload a CSV File',
                    accept=c('text/csv','text/comma-separated-values,text/plain','.csv'))),

        mainPanel(tabsetPanel(type = "tab",
                              tabPanel("Data",DT::dataTableOutput('contents')),
                              tabPanel("Summary",uiOutput("summaryTable")),
                              tabPanel("data_type",
                              box(checkboxGroupInput("class_var", "Variable", c("1"="1","2"="2")),
                                    width = 4, status = "primary"),
                              box(title = "Change Data Type", width = 3,  status = "primary",
                                  solidHeader = TRUE, collapsible = TRUE,
                              radioButtons("choose_class", label = "",choices = list(Numeric = "Numeric",Factor = "Factor", Character = "Character", Date = "Date")
                                               , selected = ""), 
                              actionButton("chg_class", "Change")))

                  ))


      )
    )

    options(shiny.maxRequestSize=30*1024^2)
    server <- function(input, output, session) {
      myData <- reactive({
        inFile <- input$file1
        if (input$fileType_Input == "1") {
          data <- read.csv(req(input$file1)$datapath,header = TRUE,stringsAsFactors = FALSE)
        } else {                      
          data <-read_excel(req(input$file1)$datapath,col_names = TRUE,sheet=1)
        }                        
      })   


      #Printing the data

      output$contents <- DT::renderDataTable({
        DT::datatable(myData())
      })

      #Getting summary of table

      output$summaryTable <- renderUI({

        out <- print(dfSummary( myData(),graph.magnif = 0.8), style = 'grid', omit.headings = TRUE, method = 'render',bootstrap.css = FALSE)
        out[[3]][[2]][[1]]
      })

      #Updating of column names in checkbox

      colnames <- reactive({ names(myData()) })

      observeEvent(myData(), {

        updateCheckboxGroupInput(session, "class_var",
                                 label = "Select Columns",
                                 choices = colnames(),
                                 selected = "") 

      })

      ## update when selection changes

      # storing the selected variable from the variables list table 
      table.sel <- reactive({
        myData()[,which(colnames(myData()) == col.name()[input$class_var,1])]
      })

      observeEvent(input$chg_class,{
        if( input$choose_class == "Numeric"){
          myData()[, input$class_var] <- as.numeric(myData()[, input$class_var])
        } else if(input$choose_class == "Factor"){
          myData()[, input$class_var] <- as.factor(myData()[, input$class_var])
        } else if( input$choose_class == "Character"){
          myData()[, input$class_var] <- as.character(myData()[, input$class_var])
        } else if( input$choose_class == "Date"){
          myData()[, input$class_var] <- as.Date(myData()[, input$class_var])
        }
        modified_dset
      })

    }

    shinyApp(ui, server)

1 个答案:

答案 0 :(得分:0)

在反应表达式中尝试一下()[,input $ class_var]

 library(shiny)
    library(readxl)
    library(summarytools)
    # install.packages("devtools")
    library(devtools)
    # install_github('dcomtois/summarytools', ref='dev-current')

    ui <- fluidPage(
      titlePanel("Data Visualization"),
      sidebarLayout(
        sidebarPanel(
          radioButtons(
            "fileType_Input",
            label = h4("Choose File type"),
            choices = c(".csv/txt" = 1, ".xls/.xlsx" = 2),
            selected = 1,
            inline = TRUE
          ),
          fileInput(inputId = 'file1', label = '1.Upload a CSV File',
                    accept=c('text/csv','text/comma-separated-values,text/plain','.csv'))),

        mainPanel(tabsetPanel(type = "tab",
                              tabPanel("Data",DT::dataTableOutput('contents')),
                              tabPanel("Summary",uiOutput("summaryTable")),
                              tabPanel("data_type",
                              box(checkboxGroupInput("class_var", "Variable", c("1"="1","2"="2")),
                                    width = 4, status = "primary"),
                              box(title = "Change Data Type", width = 3,  status = "primary",
                                  solidHeader = TRUE, collapsible = TRUE,
                              radioButtons("choose_class", label = "",choices = list(Numeric = "Numeric",Factor = "Factor", Character = "Character", Date = "Date")
                                               , selected = ""), 
                              actionButton("chg_class", "Change")))

                  ))


      )
    )

    options(shiny.maxRequestSize=30*1024^2)
    server <- function(input, output, session) {
      myData <- reactive({
        inFile <- input$file1
        if (input$fileType_Input == "1") {
          data <- read.csv(req(input$file1)$datapath,header = TRUE,stringsAsFactors = FALSE)
        } else {                      
          data <-read_excel(req(input$file1)$datapath,col_names = TRUE,sheet=1)
        }                        
      })   


      #Printing the data

      output$contents <- DT::renderDataTable({
        DT::datatable(myData())
      })

      #Getting summary of table

      output$summaryTable <- renderUI({

        out <- print(dfSummary( myData(),graph.magnif = 0.8), style = 'grid', omit.headings = TRUE, method = 'render',bootstrap.css = FALSE)
        out[[3]][[2]][[1]]
      })

      #Updating of column names in checkbox

      colnames <- reactive({ names(myData()) })

      observeEvent(myData(), {

        updateCheckboxGroupInput(session, "class_var",
                                 label = "Select Columns",
                                 choices = colnames(),
                                 selected = "") 

      })

      ## update when selection changes

      # storing the selected variable from the variables list table 
      table.sel <- reactive({
        myData()[,which(colnames(myData()) == col.name()[input$class_var,1])]
      })

      observeEvent(input$chg_class,{
        if( input$choose_class == "Numeric"){
          myData <- as.numeric(myData()[, input$class_var])
        } else if(input$choose_class == "Factor"){
          myData <- as.factor(myData()[, input$class_var])
        } else if( input$choose_class == "Character"){
          myData <- as.character(myData()[, input$class_var])
        } else if( input$choose_class == "Date"){
          myData <- as.Date(myData()[, input$class_var])
        }
        modified_dset
      })

    }

    shinyApp(ui, server)