R Shiny:为什么我不能在eventReactive上应用更改类函数

时间:2018-12-19 07:36:40

标签: r shiny

我想构建一个代码来更改闪亮应用程序中输入列的类。 下面的代码可以运行。 但是当我在df [,input $ col]中将更改类函数(例如.character,as.numeric)应用为df [,classFunction(input $ col)]时,它显示出奇怪的结果。

像这样奇怪的结果是什么

  • 当我在radioButtons中选择第一个选项时,结果将显示列的向量;

  • 当我在radioButtons中选择第三个(as.factor)时,它返回as.factor(first_col_in_df)的结果,但不返回我选择的列;

  • 当我选择其他功能时,它显示:错误:选择了未定义的列

我这样奇怪的代码:

    library(shiny)
    library(rio)
    library(anytime)

    options(shiny.maxRequestSize=500*1024^2,shiny.usecairo = FALSE)

    ui <- fluidPage(

        titlePanel("See the file table"),
        fluidRow(
            column(6,
                   fileInput("theFile","upload your file")
            ),
            column(6,
                   radioButtons("encode", "encoding way",
                                choices = c("Default" = "default",
                                            "UTF-8" = "utf_8"),selected = "default")
            ),
            column(8,
                   uiOutput("colToDesc")
            ),
            column(3,
                   radioButtons("class", "Which class is the variable",
                                choices = c("as.character" = "as.character",
                                            "as.numeric" = "as.numeric",
                                            "as.factor" = "as.factor",
                                            "as.Date" = "anydate",
                                            "as.datetime" = "anytime"),selected = "as.character")
            ),
            column(3,
                   actionButton("choice2", "Show variables Desc")
            ),

            column(12,
                   verbatimTextOutput("console")
            )
        )

    )
    server <- function(input,output, session){

        allData <- reactive({
            theFile <- input$theFile
            req(input$theFile)

            # Changes in read.table 
            if(input$encode == "default"){
                df <- import(theFile$datapath)  
            } else{
                df <- import(theFile$datapath,encoding = "UTF-8")
                return(df)
            }
        })


        output$colToDesc <- renderUI({
            cn <- colnames(allData())
            selectInput("colToDesc", "Select variable to Desc", 
                        choices  = cn,
                        size=10,
                        multiple=T, selectize=FALSE)
        }) 

        class <- eventReactive(input$choice2,{
            dat <- allData()
            class <- switch(
                input$class,
                as.character = as.character,
                as.numeric = as.numeric,
                as.factor =as.factor,
                anydate = anydate,
                anytime = anytime,
                as.character)
            class <- class(dat[,class(input$colToDesc), drop = FALSE])
        })



        output$console <-  renderPrint({

            print(class())
        })

    }

    shinyApp(ui, server)

0 个答案:

没有答案