My conditional panel is exhibiting weird behavior. I just want it to show respective table based on choice as it supposed to, however it only shows first table twice irrespective of choice and only first time it reaches correct node of if else statement. Afterwards it always end up in wrong if statement which concerns the part when rhandsontable inputs are changed.. I would highly appreciate your advice how to solve it. Thank you!
library(shiny)
library(rhandsontable)
get_pct_table<-function(){
rev_pct <- data.frame(matrix(rnorm(0), ncol = 5, nrow = 3))
col_names <- c("2015", "2016", "2017", "2018", "2019")
row_names <- c("Worst", "Base", "Best")
rownames(rev_pct) <- row_names
colnames(rev_pct) <-col_names
return(rev_pct)
}
get_pm_table<-function() {pm <- data.frame(matrix(rnorm(0), ncol = 5, nrow = 3))
col_names <- c("Product","Quantity", "Avg. Price", "Avg. Cost", "Year")
colnames(pm) <- col_names
return(pm)
}
ui <- fluidPage("Lm Assumptions",
sidebarLayout(
sidebarPanel(
radioButtons("lm_forecast_choice", "Lm Forecasting Method",
c("Percentage Change"="pc", "Product Mix"="pm")
)
),
mainPanel(
conditionalPanel(condition = "input.lm_forecast_choice == 'pc'",
rHandsontableOutput("lm_forecast_c")
),
conditionalPanel(condition = "input.lm_forecast_choice == 'pm'",
rHandsontableOutput("lm_forecast_m")
)
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
v = reactiveValues()
rev.choice <- reactive ({input$lm_forecast_choice})
observe({
if(!is.null(rev.choice())) {
if(rev.choice()=="pc"){
if (!is.null(input$lm_forecast_c)) {
print("at pc changed")
v$lm_forecast <- hot_to_r(input$lm_forecast_c)
} else {
print("at pc new")
v$lm_forecast <- get_pct_table()
}
}
else if(rev.choice() == "pm"){
if (!is.null(input$lm_forecast_m)) {
print("at pm changed")
v$lm_forecast <- hot_to_r(input$lm_forecast_m)
} else {
print("at pm new")
v$lm_forecast <- get_pm_table()
}
}
}
})
output$lm_forecast_c <- renderRHandsontable({
rhandsontable(v$lm_forecast, rowHeaderWidth = 100) %>%
hot_col(colnames(v$lm_forecast)[1], format = "0%") %>%
hot_col(colnames(v$lm_forecast)[2], format = "0%") %>%
hot_col(colnames(v$lm_forecast)[3], format = "0%") %>%
hot_col(colnames(v$lm_forecast)[4], format = "0%") %>%
hot_col(colnames(v$lm_forecast)[5], format = "0%") %>%
hot_cols(colWidths = 90) %>%
hot_rows(rowHeights = 30)
})
output$lm_forecast_m <- renderRHandsontable({
rhandsontable(v$lm_forecast, rowHeaderWidth = 100) %>%
hot_col(colnames(v$lm_forecast)[1], format = "$0,0.00") %>%
hot_col(colnames(v$lm_forecast)[2], format = "$0,0.00") %>%
hot_cols(colWidths = 90) %>%
hot_rows(rowHeights = 30)
})
}
# Run the application
shinyApp(ui = ui, server = server)