I am writing a shiny app to realize the following effects:
Whenever I choose variable included by categoryname, the web will generate the slider (here I use conditional panel) which provides a divider. It divides the selected variable into 2 groups and form a new column added to the original data set.
The web page can be generated now. My problem is:
The slider should be hided when I am not choosing the variable in
categoryname, but it always appears.Whenever I choose the variable in
categoryname, the page will exit.
The error shows:
Warning in max(MT_EG$id_arm) :
no non-missing arguments to max; returning -Inf
Warning in input$divider$max <- max(MT_EG$id_arm) :
Coercing LHS to a list
Warning: Error in $<-.reactivevalues: Attempted to assign value to a read-only reactivevalues object
75: stop
74: $<-.reactivevalues
72: observeEventHandler [/opt/bee_tools/shiny/3.5.1/users/denga2/teal.modules.km/testapp/app.R#75]
1: runApp
Well the attempt to change the max an min of the slider isn't the only cause. When I set it to be fixed, the page also exits.
Here in the code I simply use mtcars dataset so that all of you can get access to.
library(shiny)
categoryname = c("mpg_group", "disp_group")
MT_EG = mtcars[,1:5]
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Mtcars Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
selectInput(inputId = "arm",
label = "ARM VARIABLE",
choices = c("mpg_group", "cyl", "disp_group", "hp", "drat"),
selected = "cyl"),
conditionalPanel(
condition = "categoryname.includes(input.arm)",
#condition = "categoryname == input.arm",
#optionalSliderInputValMinMax("divider", "divide slider", c(50,0,100), ticks = FALSE)
sliderInput("divider", "divide slider", 0, 100, 50)
)
),
# Show a plot of the generated distribution
mainPanel(
uiOutput("data")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output, session) {
observeEvent(
input$arm,
{
if (input$arm %in% categoryname){
# start over and remove the former column if exists
MT_EG = MT_EG[, !(colnames(MT_EG) %in% input$arm)]
id_arm_var <- input$arm
id_arm <- unlist(str_split(id_arm_var,'_'))[1]
# change the range of the slider
input$divider$max = max(MT_EG$id_arm)
input$divider$min = min(MT_EG$id_arm)
# generate a new column and bind
divi <- data.frame(id_arm_var = MT_EG$id_arm>input$divider)
divi$id_arm_var[divi$id_arm_var==TRUE] <- paste0(id_arm_var, " Larger")
divi$id_arm_var[divi$id_arm_var==FALSE] <- paste0(id_arm_var, " Smaller")
MT_EG <- cbind(MT_EG,divi)
}
output$data=renderTable(MT_EG)
})
}
# Run the application
shinyApp(ui = ui, server = server)
Any ideas? Thank you guys!