I have the following piece of code in a shiny app. My goal is to generate the choices for the "cutFamily2" selectInput widget based on what the user chose for the "machine2" selectInput.
If I use corte2()
instead of eval(paste0("corte",2))
on the observerEvent
the app runs properly. The problem is that I want to use the paste0()
because the integer "2" in eval(paste0("corte",2))
will be an argument of a function (function(data,n)
) so I can easily generate corte1
, corte2
and so on.
When I run it using eval(paste0("corte",2))
I am getting the "error in $: $ operator is invalid for atomic vectors" and the app won't even run. I tried to use enframe()
to convert it to a tibble, then the app runs, but I get a "Unknown or uninitialised column: CutFamily
" error and the SelectInput choices will be empty. I also tried [[
instead, but nothing.
Any ideas on how to solve the problem?
library(shiny)
library(shinydashboard)
library(feather)
library(tibble)
library(tidyverse)
Cortes = as_tibble(read_feather("Cortes.feather"))
ui <- dashboardPage(
dashboardHeader(title="Setup"),
dashboardSidebar(
sidebarMenu(
menuItem("Procedimento",tabName = "task_wizard",icon = icon("th"))
)
),
dashboardBody(
tabItems(
tabItem(tabName = "task_wizard",
selectInput(paste0("machine",2),"Selecione o tipo de máquina: ",choices = unique(Cortes$Machine)),
selectInput(paste0("cutFamily",2),"Selecione a área de trabalho: ",choices = NULL)
)
)
)
)
server <- function(input, output) {
assign(paste0("corte",2),reactive({
filter(Cortes,Machine == input[[eval(paste0("machine",2))]])
}))
observeEvent(eval(paste0("corte",2)),{
choices <- unique(eval(paste0("corte",2))$CutFamily)
updateSelectInput(inputId = paste0("cutFamily",2),choices = choices)
})
}
shinyApp(ui = ui, server = server)
and here is the Cortes tibble
> Cortes
# A tibble: 4 x 3
Machine CutFamily CutFeature
<chr> <chr> <chr>
1 Torno Torneamento Externo Faceamento
2 Torno Torneamento Externo Torneamento
3 Torno Torneamento Externo Chanframento
4 Torno Torneamento Externo Canal radial
tks