[英]Action Button for change class is not working - R shinydashboard
I want to change class of the selected variable in R shinydashboard. 我想在R Shinydashboard中更改所选变量的类。 Please find attached my code.
请找到附件我的代码。 But when I press action button, class of the variable selected is not getting changed.
但是,当我按下操作按钮时,所选变量的类别没有改变。
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 服务器
when i press the action button for changing the class type of the selected variable, the class of the variable is not getting changed. 当我按下用于更改所选变量的类类型的操作按钮时,该变量的类未更改。
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)
Thanks, Balaji 谢谢,巴拉吉
The issue is that the variable Train
was created by reactive
and is therefore read-only in the rest of the code. 问题是变量
Train
是由reactive
创建的,因此在其余代码中为只读。 This means, that the lines of the form 这意味着表格的行
Train()[, input$class_var] <- as.numeric(Train()[, input$class_var])
do not alter the variable returned by Train()
. 不要更改
Train()
返回的变量。 You should use reactiveVal
, reactiveValues
or makeReactiveBinding
i instead. 您应该使用
reactiveVal
, reactiveValues
或makeReactiveBinding
我来代替。 For example 例如
## 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])
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.