我想从R shinydashboard中的数据集中删除选定的变量。请查找附上我的代码。但是当我尝试选择变量并删除时,
if(input$imputation == "Delete Variable"){
rv$Train <- rv$Train[,-(input$miss_var)]
return(rv$Train)
}
我收到以下错误,
警告:错误输入 - :一元运算符的参数无效
UI
请找到代码的ui部分,
library(shiny)
library(shinydashboard)
library(missForest)
ui <- dashboardPage(skin = "black",
dashboardHeader(title = "Analytics Workbench 2.0", titleWidth = 250, dropdownMenuOutput("msgs")),
dashboardSidebar(
sidebarMenu(
fileInput("Table1", "Historical Data"),
radioButtons("sep", "Separator", choices = c(Comma = ",", Semicolon = ";", Tab = "\t", Pipe="|"), selected = ","),
menuItem("Variable Transformation", tabName = "transformation",icon = icon("exchange")))),
dashboardBody(
tabItems(
tabItem(tabName = "transformation",
fluidRow(
box(selectInput("miss_var", "Variable", c("1"="1","2"="2")), width = 2, status = "primary"),
box(title = "Missing Value Imputation", width = 4, status = "primary", solidHeader = TRUE, collapsible = TRUE,
radioButtons("imputation", label = "",
choices = list(Delete_Variable = "Delete Variable", Impute = "Impute"), selected = ""),
actionButton("miss_imp", "Select"))))
)
)
)
服务器
请找到代码的服务器部分,
server <- function(input, output, session) {
## 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)
})
observe({
is.miss <- sapply(rv$Train, function(y) sum(length(which(is.na(y)))))
dname3 <- names(rv$Train[, is.miss > 0])
col_options3 <- list()
col_options3[dname3] <- dname3
updateSelectInput(session, "miss_var",
label = "Missing Variables",
choices = col_options3,
selected = "")
})
observeEvent(input$miss_imp,{
if(input$imputation == "Delete Variable"){
rv$Train <- rv$Train[,-(input$miss_var)]
return(rv$Train)
} else if(input$imputation == "Impute"){
missfor <- missForest(rv$Train)
rv$Train <- data.frame(missfor$ximp)
return(rv$Train)
}
})
}
shinyApp(ui, server)
谢谢, 巴拉吉