更改类的操作按钮不起作用 - R shinydashboard

时间:2018-04-16 10:46:37

标签: r shiny shinydashboard

我想在R shinydashboard中更改所选变量的类。请查找附上我的代码。但是当我按下动作按钮时,所选变量的类不会改变。

UI

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

ui <- dashboardPage(skin = "black",
                    dashboardHeader(title = "Analytics Workbench 2.0", titleWidth = 250),

                    dashboardSidebar(
                      sidebarMenu(
                        fileInput("Table1", "Historical Data"),
                        radioButtons("sep", "Separator", choices = c(Comma = ",", Semicolon = ";", Tab = "\t", Pipe="|"), selected = ","),
                        menuItem("Data Overview", tabName = "data_view", icon = icon("table")))),

                    dashboardBody(
                      tabItems(
                        tabItem(tabName = "data_view",
                               fluidRow(
                                  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"))))

                      )))

服务器

当我按动作按钮更改所选变量的类类型时,变量的类不会改变。

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

  Train <- reactive({

    if (is.null(input$Table1)) return(NULL)

    read.table(input$Table1$datapath, fill = TRUE, header=T, sep=input$sep, na.strings = c(""," ",NA), stringsAsFactors = TRUE)

  })

  observe({
    cname <- names(Train())
    col_options <- list()
    col_options[ cname] <- cname

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


  eventReactive(input$chg_class,{

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

}

shinyApp(ui, server)  

谢谢, 巴拉吉

1 个答案:

答案 0 :(得分:0)

问题是变量Train是由reactive创建的,因此在其余代码中是只读的。这意味着,形式的行

Train()[, input$class_var] <- as.numeric(Train()[, input$class_var])

不要更改Train()返回的变量。您应该使用reactiveValreactiveValuesmakeReactiveBinding i代替。例如

## initialize
rv <- reactiveValues()

## update when dataset changes
observe({
  rv$Train <- read.table(req(input$Table1)$datapath, fill = TRUE, header=T, 
                         sep=input$sep, na.strings = c(""," ",NA), 
                         stringsAsFactors = TRUE)
}) 

## update when selection changes
observeEvent(input$chg_class,{
    if( input$choose_class == "Numeric"){
      rv$Train[, input$class_var] <- as.numeric(rv$Train[, input$class_var])
    } else if( input$choose_class == "Factor"){
      rv$Train[, input$class_var] <- as.factor(rv$Train[, input$class_var])
    } else if( input$choose_class == "Character"){
      rv$Train[, input$class_var] <- as.character(rv$Train[, input$class_var])
    } else if( input$choose_class == "Date"){
      rv$Train[, input$class_var] <- as.Date(rv$Train[, input$class_var])
    }
  })