I want to create a number of boxes that increases when the user click an action button.
I tried to create a counter, initialized it on 1 and linked it to an action button. When I try to use the counter it gives me error
library(shiny)
library(DT)
library(readr)
library(shinythemes)
library(shinydashboard)
library(sortable)
testdataset <- data.frame(A = runif(n=4, min=1, max=6),B = runif(n=4, min=1, max=6),C = runif(n=4, min=1, max=6))
ui <- dashboardPage(
dashboardHeader(title = "ShinySem"),
dashboardSidebar(
sidebarMenu(
menuItem("Insert data", tabName = "data"),
menuItem("Choosing variables", tabName = "drag")
)
),
dashboardBody(
tabItems(
tabItem(tabName = "data",
fileInput("infile","Insert your dataset in csv",accept = c(".csv","text/csv","text/comma-separated-values")),
radioButtons("separator","Separator: ",choices = c(";",",",":"), selected=";",inline=TRUE),
dataTableOutput("userfile")),
tabItem(tabName = "drag",
actionButton("addex","Add Latent Exogenous variable"),
actionButton("adden","Add Latent Endogenous variable"),
textOutput("ex"),
uiOutput("bucket"))
)
)
)
server <- function(input,output,session){
#Rendering data
df_uploads <- reactive({
data <- input$infile
if (is.null(data))
return(testdataset)
df <- read.csv(data$datapath, header = TRUE,sep = input$separator)
return(df)
})
output$userfile<- renderDataTable({
df <- df_uploads()
datatable(df)
})
#Render Bucket list and drag and drop
output$bucket <- renderUI(
do.call("bucket_list",args = c(
list(header = "Drag and drop",
group_name = "bucketlistgroup",
orientation = "horizontal",
add_rank_list(
text = "dataset you have insert",
labels = colnames(df_uploads()),
input_id = "starting_variables")),
rank_list_exogenous
)))
#Reactive action button exogenous
counter <- reactiveVal(value = 1) #initializing
observeEvent(input$addex, {
newcounter <- counter() + 1 #adding one by clicking
counter(newcounter)
})
#Reactive ranklist exogneous
rank_list_exogenous <- lapply(1:counter,function(i){
add_rank_list(
text = "Latent Exogenous variable",
labels = NULL,
input_id = "Latent Exogenous variable"
)})
}
shinyApp(ui, server)
if I replace "counter" in the reactive ranklist exogenous block with a specific number it works for good so I assume the problem is the counter
I solved the problem by modifying the creation of the reactive rank list in order to use the counter in a reactive block
server <- function(input,output,session){
#Rendering data
df_uploads <- reactive({
data <- input$infile
if (is.null(data))
return(testdataset)
df <- read.csv(data$datapath, header = TRUE,sep = input$separator)
return(df)
})
output$userfile<- renderDataTable({
df <- df_uploads()
datatable(df)
})
#Render Bucket list and drag and drop
output$bucket <- renderUI(
do.call("bucket_list",args = c(
list(header = "Drag and drop",
group_name = "bucketlistgroup",
orientation = "horizontal",
add_rank_list(
text = "dataset you have insert",
labels = colnames(df_uploads()),
input_id = "starting_variables")),
lapply(1:counterex(), function(i) {
add_rank_list(
text = paste("Latent Exogenous variable", i),
labels = NULL,
input_id = paste0("Latent_Exogenous_variable", i)
)
}),
lapply(1:counteren(), function(i){
add_rank_list(
text = paste("latent Endogenous variable", i),
labels = NULL,
input_id = paste0("Latent_Endogenous_variable", i)
)
})
)))
#Reactive action button exogenous
counterex <- reactiveVal(value = 1) #initializing
observeEvent(input$addex, {
newcounterex <- counterex() + 1 #adding one by clicking
counterex(newcounterex)
})
#Reactive action button endogenous
counteren <- reactiveVal(value = 1) #initializing
observeEvent(input$adden, {
newcounteren <- counteren() + 1
counteren(newcounteren)
})
}
shinyApp(ui, server)