In a nutshell, I believe I need to nest uiOutputs together and cannot figure a great way to do so.
The app is large but for this part, I would like to create a survey that renders sub-surveys (new panels) based on a slider input (I've accomplished that much). These panels will all be standard and so they can be created with a loop.
However, answers within these panels should generate more UI within the panel from which they were generated and therein lies the problem... the nesting of uiOutputs. I've tried to provide the shortest example possible below, with comments - and note that the second uiOutput call works if I specify a panel for which it should work ("oh_lawd_1" in this case).
Please let me know what you think! Have been looking at this in my spare time for at least 4 days. (also I realize that this is not an ideal use of shiny).
library(shiny)
library(shinyWidgets)
ui <- fluidPage( #UI
column(6, offset = 3,
sliderInput(inputId = "my_slider", # slider to choose number of panels
label = "Choose Panels to be Displayed",
min = 0, max = 5, value = 1),
uiOutput(outputId = "update_panels") # ui output to create panels
)
)
server <- function(input, output, session) { #Server
output$update_panels <- renderUI({ # rendering all the panels called for by user
panels <- input$my_slider
if(panels == 0){
return("No panels being displayed")# returning 0 if none selected
} else {
our_ui <- list() # creating a list to store a standard panel
for(i in 1:panels){
button_id <- paste("button_id", i, sep = "_") # a unique id for each panel's radiobuttons
oh_lawd <- paste("oh_lawd", i, sep = "_") # a unique id for each panel's uiOutput
update <- wellPanel(paste("Well Panel #", i), # "update" is what each panel should START OFF looking like
radioButtons(inputId = button_id,
label = "Choose a pill",
choices = c("Red Pill", "Blue Pill")),
uiOutput(oh_lawd)) # this part is the issue - I would like to update individual panels with a
# radio button selection specific to a choice in each panel... a nested uiOutput
our_ui <- list(our_ui, update)
}}
our_ui})
output$oh_lawd_1 <- renderUI({ # this works for the first... but I need to somehow create one of these for each based on
# number of panels and the choice in each panel
if(input$button_id_1 == "Red Pill"){
radioButtons("first_output", "Next Choices", choices = c("I'm a brave boi", "Knowledge schmoledge"))
} else {
radioButtons("first_output", "Next Choices", choices = c("Gimme dat ignorance", "Mhmm yea") )
}
})
}
# Run the application
shinyApp(ui = ui, server = server)
Is it what you want? I'm not sure.
library(shiny)
library(shinyWidgets)
ui <- fluidPage( #UI
column(6, offset = 3,
sliderInput(inputId = "my_slider", # slider to choose number of panels
label = "Choose Panels to be Displayed",
min = 0, max = 5, value = 1),
uiOutput(outputId = "update_panels") # ui output to create panels
)
)
server <- function(input, output, session) { #Server
output$update_panels <- renderUI({ # rendering all the panels called for by user
panels <- input$my_slider
if(panels == 0){
return("No panels being displayed")# returning 0 if none selected
} else {
our_ui <- list() # creating a list to store a standard panel
for(i in 1:panels){
button_id <- paste("button_id", i, sep = "_") # a unique id for each panel's radiobuttons
oh_lawd <- paste("oh_lawd", i, sep = "_") # a unique id for each panel's uiOutput
update <- wellPanel(paste("Well Panel #", i), # "update" is what each panel should START OFF looking like
radioButtons(inputId = button_id,
label = "Choose a pill",
choices = c("Red Pill", "Blue Pill")),
uiOutput(oh_lawd)) # this part is the issue - I would like to update individual panels with a
# radio button selection specific to a choice in each panel... a nested uiOutput
our_ui <- list(our_ui, update)
}}
our_ui})
observeEvent(input$my_slider, {
lapply(seq_len(input$my_slider), function(i){
uiID <- paste0("oh_lawd_", i)
buttonID <- paste0("button_id_", i)
radioID <- paste0("radio_id_", i)
output[[uiID]] <- renderUI({
if(input[[buttonID]] == "Red Pill"){
choices <- c("I'm a brave boi", "Knowledge schmoledge")
}else{
choices <- c("Gimme dat ignorance", "Mhmm yea")
}
radioButtons(radioID, "Next Choices", choices = choices)
})
})
})
}
# Run the application
shinyApp(ui = ui, server = server)
Related
I am trying to design a search feature where you can search via a text input and through check boxes (I am using shinyWidgets), except for some reason, when you hit enter inside the text input it is activating my "ALL/NONE" button.
The goal is that when the ALL/NONE button is hit that it alternates between selecting all of the check boxes and selecting none of them. The issue is that hitting enter in the text box also seems to activate the observe, even when it should only be activating by the button.
library(shiny)
library(shinyWidgets)
Habitat <- c("grass", "water", "stone")
ID <- c(1, 2, 3)
data <- data.frame(ID, Habitat)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
width = 2,
textInput("keyword_search", label = "Search by Keyword"),
uiOutput("h_button"),
uiOutput("habitat_filter")
),
mainPanel(width = 10
))
)
server <- function(input, output, session) {
output$habitat_filter <- renderUI({
habitat_choices <- checkboxGroupInput(inputId = "habitat", label = "",
choices = unique(data$Habitat[!is.na(data$Habitat)]),
selected = unique(data$Habitat[!is.na(data$Habitat)]))
})
output$h_button <- renderUI({
habitat_button <- checkboxGroupButtons(
inputId = "habitat_switch",
choices = "ALL / NONE",
size = "sm",
selected = "ALL / NONE")
})
observe({ #all/none button for habitats
x <- input$habitat_switch
if (!is.null(x)) {
x <- unique(data$Habitat[!is.na(data$Habitat)])
}
else {
x <- character(0)
}
updateCheckboxGroupInput(
session,
"habitat",
label = NULL,
choices = unique(data$Habitat[!is.na(data$Habitat)]),
selected = x
)
})
}
shinyApp(ui = ui, server = server)
Weirdly, this problem seems to go away if it is coded outside of the sidebarLayout. i.e. if the ui side looks like this:
ui <- fluidPage(
textInput("keyword_search", label = "Search by Keyword", width = '100%', placeholder = "Type here to search the archive..."),
uiOutput("h_button"),
uiOutput("habitat_filter")
)
Unfortunately, I need the sidebar so removing it isn't an option for fixing the problem. Does anyone have a solution to prevent these features from being connected? Or an explanation for why this happening?
Replacing my observe for the button with this seems to avoid the problem as suggested here: Select/Deselect All Button for shiny variable selection
observe({ #all/none button for habitats
x <- unique(data$Habitat[!is.na(data$Habitat)])
if (!is.null(input$habitat_switch) && input$habitat_switch >= 0) {
if (input$habitat_switch %% 2 == 0) {
x <- unique(data$Habitat[!is.na(data$Habitat)])
}
else {
x <- character(0)
}
}
updateCheckboxGroupInput(
session,
"habitat",
label = NULL,
choices = sort(unique(data$Habitat[!is.na(data$Habitat)])),
selected = x
)
})
Still no idea what caused this issue initially, but this work around seems good enough
Probably very basic question - but can't translate similar posts I've found to my exact issue.
Within an R Shiny app, I have a first drop-down menu that is populated by a vector produced on the server - this allows me to make one set of choices.
I want to have a tick box that then introduces a second drop down - but I want that drop down to disappear if I un-tick the tick box.
I've had a go - see MWE below - the graph is just there to keep to the structure of my original code (obviously I'm aware my drop-downs do nothing but that's not the case in the original but wanted the MWE to be as 'M' as possible).
If I remove the removeUI() line then ticking the tick-box does create a new drop down as required - but then un-ticking the tick box fails to remove it.
I'm obviously missing something; any help much appreciated as I totally suck at R Shiny but really want to get better!
library(shiny)
library(shinyMobile)
# define UI elements
ui <- f7Page(
f7SingleLayout(
navbar = f7Navbar(
),
f7Card(htmlOutput("initial_drop_down"), #first drop down
f7checkBox(inputId = "switch", label = "Introduce second choice", FALSE), #tick box for second drop down if required
htmlOutput("reactive_drop_down") #second drop down
),
f7Shadow(
intensity = 16,
f7Card(
plotOutput("distPlot", height = "800px") # plot - originally linked to drop down choices but an arbitrary graph here for simplicity
)
)
)
)
# server calculations
server <- function(input, output) {
library(ggplot2)
# generate first drop down - done on server side since usually choices vector is comprised of information read in from files
output$initial_drop_down = renderUI({
selectInput(inputId = "initial_choice",
label = "First choice:",
choices = c("Choice 1", "Choice 2", "Choice 3"))
})
observeEvent(input$initial_choice, {
# trying to add second drop down based on action in switch - not convinced my use of observeEvent is quite right - issue likely sits in here.
observeEvent(input$switch, {
if(input$switch == T){
output$reactive_drop_down = renderUI({
selectInput(inputId = "second_choice",
label = "Second (dynamic) choice:",
choices = c(1,2,3))
})
}else{
removeUI(selector ="#reactive_drop_down")
}
})
output$distPlot <- renderPlot({
ggplot(data = cars) + geom_line(aes(x=speed, y=dist))
})
})
}
# Run the application
shinyApp(ui = ui, server = server)
Could you use conditionalPanel? Put your htmlOutput for your second input there in your ui. I would avoid using nested observeEvent and output.
library(shiny)
library(shinyMobile)
library(ggplot2)
# define UI elements
ui <- f7Page(
f7SingleLayout(
navbar = f7Navbar(
),
f7Card(htmlOutput("initial_drop_down"), #first drop down
f7checkBox(inputId = "switch", label = "Introduce second choice", FALSE), #tick box for second drop down if required
conditionalPanel(
condition = "input.switch==1",
htmlOutput("reactive_drop_down") #second drop down
)
),
f7Shadow(
intensity = 16,
f7Card(
plotOutput("distPlot", height = "800px") # plot - originally linked to drop down choices but an arbitrary graph here for simplicity
)
)
)
)
# server calculations
server <- function(input, output) {
# generate first drop down - done on server side since usually choices vector is comprised of information read in from files
output$initial_drop_down = renderUI({
selectInput(inputId = "initial_choice",
label = "First choice:",
choices = c("Choice 1", "Choice 2", "Choice 3"))
})
output$reactive_drop_down = renderUI({
selectInput(inputId = "second_choice",
label = "Second (dynamic) choice:",
choices = c(1,2,3))
})
output$distPlot <- renderPlot({
ggplot(data = cars) + geom_line(aes(x=speed, y=dist))
})
}
# Run the application
shinyApp(ui = ui, server = server)
I am trying to create a shiny code that is able to filter a table non pre-determined number of times. When the user uploads a different (new) table, unfortunately the code breaks as I need to restart a lapply loop somehow, throwing out the previously stored column names.
I would like to create an non pre-defined filtering options for a table within Shiny. The user can select a column and filter a table choosing different categorical variables within that column. It is possible to add additional selection fields by pressing the 'Add' button.
the UI:
library(shiny)
library(shinydashboard)
library(dplyr)
ui <- shinyUI(
pageWithSidebar(
headerPanel("testing of dynamic number of selection"),
sidebarPanel(
uiOutput("buttons")),
mainPanel(
uiOutput("drops")
,tableOutput("table")
)
))
The server:
A table (test.csv) is automatically stored in a reactive values and a first searching field appears with 3 buttons (Add = to add a new searching field by reading in the colnames and a multiselect that stores the unique variables from that columns. The filtering function is activated by the Calculate button)
server<-function(input, output, session) {
###### read in test file
values<-reactiveValues(number = 1,
upload = NULL,
input = NULL)
values$upload<-read.csv("test.csv")
#just the "add" button, in this instance it shouldn't be a uiOutput
output$buttons <- renderUI({
div(
actionButton(inputId = "add", label = "Add"), actionButton(inputId = "calc", label = "Calculate"),
actionButton(inputId = "new", label = "new table")
)
})
#pressing the add button
observeEvent(input$add, {
cat("i adding a new record\n")
values$number <- values$number + 1L })
daStuff <- function(i){
inputName<-paste0("drop", i)
inputName2<-paste0("select", i)
inputText<-if(values$number>0){input[[paste0("drop",i)]]}else{F} # previously selected value for dropdown
inputSelect <- if(values$number>1){input[[paste0("select",i)]]}else{F} # previously selected value for dropdown
fluidRow(
column(6,selectInput(inputName, inputName, c(colnames(values$upload)), selected = inputText)),
column(6,selectInput(inputName2, inputName2,
na.omit(unique(as.vector(values$upload[,input[[paste0("drop",i)]]]))),
multiple=TRUE, selectize=TRUE, selected=inputSelect)) )}
output$drops<- renderUI({
lapply(seq_len(values$number), daStuff)})
By pressing the Calculate button, the uploaded table is subjected to filtering, depending on the selected unique values and shown in the output$table
observeEvent(input$calc, {
values$input<-NULL
for (i in 1:values$number){
if(!is.null(input[[paste0("select",i)]])){
if(is.null(values$input)){
values$input<- filter(values$upload,values$upload[,input[[paste0("drop",i)]]] %in% input[[paste0("select",i)]])}
else{
values$input<- filter(values$input,values$input[,input[[paste0("drop",i)]]] %in% input[[paste0("select",i)]])}
} }
if (is.null(values$input)){values$input<-values$upload}
output$table <- renderTable({values$input})
})
My problem is when I upload a new table (test2.csv), I don't know how to erase the previously stored selections (drop* and select* values) and gives back an error message.
observeEvent(input$new,{
values$upload<-read.csv("test2.csv")
})
}
shinyApp(ui=ui, server = server)
I suppose I should stop somehow the lapply loop and restart it over, so the previously stored values are replaced depending on the new selection, but I am a bit stuck on how I could achieve that.
Just in case you might still be looking for solutions, I wanted to share something that was similar and could potentially be adapted for your needs.
This uses observeEvent for all select inputs. If it detects any changes, it will update all inputs, including the possibilities for select based on drop.
In addition, when a new file is read, the selectInput for drop and select are reset to first value.
Edit: I forgot to keep selected = input[[paste0("drop",i)]] in place for the dropdown (see revised code). It seems to keep the values now when new filters are added - let me know if this is what you had in mind.
library(shiny)
library(shinydashboard)
library(dplyr)
myDataFrame <- read.csv("test.csv")
ui <- shinyUI(
pageWithSidebar(
headerPanel("Testing of dynamic number of selection"),
sidebarPanel(
fileInput("file1", "Choose file to upload", accept = ".csv"),
uiOutput("buttons")
),
mainPanel(
uiOutput("inputs"),
tableOutput("table")
)
)
)
server <- function(input, output, session) {
myInputs <- reactiveValues(rendered = c(1))
myData <- reactive({
inFile <- input$file1
if (is.null(inFile)) {
d <- myDataFrame
} else {
d <- read.csv(inFile$datapath)
}
d
})
observeEvent(lapply(paste0("drop", myInputs$rendered), function(x) input[[x]]), {
for (i in myInputs$rendered) {
updateSelectInput(session,
paste0('select', i),
choices = myData()[input[[paste0('drop', i)]]],
selected = input[[paste0("select",i)]])
}
})
output$buttons <- renderUI({
div(
actionButton(inputId = "add", label = "Add"),
actionButton(inputId = "calc", label = "Calculate")
)
})
observeEvent(input$add, {
myInputs$rendered <- c(myInputs$rendered, max(myInputs$rendered)+1)
})
observeEvent(input$calc, {
showData <- NULL
for (i in 1:length(myInputs$rendered)) {
if(!is.null(input[[paste0("select",i)]])) {
if(is.null(showData)) {
showData <- filter(myData(), myData()[,input[[paste0("drop",i)]]] %in% input[[paste0("select",i)]])
}
else {
showData <- filter(showData, showData[,input[[paste0("drop",i)]]] %in% input[[paste0("select",i)]])
}
}
}
if (is.null(showData)) { showData <- myData() }
output$table <- renderTable({showData})
})
observe({
output$inputs <- renderUI({
rows <- lapply(myInputs$rendered, function(i){
fluidRow(
column(6, selectInput(paste0('drop',i),
label = "",
choices = colnames(myData()),
selected = input[[paste0("drop",i)]])),
column(6, selectInput(paste0('select',i),
label = "",
choices = myData()[1],
multiple = TRUE,
selectize = TRUE))
)
})
do.call(shiny::tagList, rows)
})
})
}
shinyApp(ui, server)
I have used the below code to create checkbox from my data.I would like to create slider input for each checkbox I select from the list.For example if the checkbox has 4 variables like "sky","earth","water","fire" and if I select sky, it should dynamically open a slider input for sky and if I select water it should open up one more slider input for water. I tried conditionalPanel,but I have more than 50 variables in my checkbox,so i cannot write condition for all the 50 variables. Is there any generalized method available in shiny?
server
output$choosedigital=renderUI({
if(is.null(bk$variables))
return()
checkboxGroupInput("choosemedia", "Choose digital",
choices = bk$variables,
selected = bk$variables)
})
output$test <- renderUI({
LL <- list(rep(0,length(input$choosedigital)))
for(i in 0:(length(input$choosedigital))) {
LL[i] <- list(sliderInput(inputId = paste(input$choosedigital,i)
, label = paste(input$choosedigital,i),
min=0,max=25,value = 5))
}
return(LL)
})
You want to put your sliderInputs inside a conditionalPanel in the UI and set the condition so that when the relevant checkbox is clicked the the condition equates to TRUE.
e.g.
library(shiny)
myData = c("One", "Two", "Three")
ui <- fluidPage(
checkboxGroupInput("choosemedia", "Choose digital",
choices = myData,
selected = myData),
textOutput("myText"),
conditionalPanel(condition = "input.choosemedia.includes('One')",
sliderInput("sliderOne", "Choose your value", min=0, max=100, value=50)
),
conditionalPanel(condition = "input.choosemedia.includes('Two')",
sliderInput("sliderTwo", "Choose your other value",
min=0, max=50, value=25))
)
# Define server logic
server <- function(input, output) {
output$myText <- renderText({input$choosemedia})
}
# Run the application
shinyApp(ui = ui, server = server)
If long as you know what the content of bk$variables is you can hardcode them, otherwise you'll have to generate these on the fly.
Hope this is enough info to get you going.
My shiny app begins with a checkboxGroupInput which contains the names of three companies: A, B and C. It also has 3 hidden numeric inputs, each corresponding to a company. Potential investors may select the name of the company they wish to invest in and specifiy the amount they are willing to invest. When the name of a company is checked, the corresponding numeric input shows up. Also, when the company name is unchecked, the numeric input disappears.
The checkboxGroupInput is called company. The 3 numericInput fields are respectively called amountA, amountB and amountC and are all generated inside a uiOutput. They are hidden with the hidden function of shinyjs.
library(shiny)
library(shinyjs)
library(magrittr)
ui <- fluidPage(
useShinyjs(),
checkboxGroupInput(inputId = "company", label = "Select a company", choices = LETTERS[1:3]),
uiOutput(outputId = "amounts")
)
server <- function(input, output){
company_names <- LETTERS[1:3]
num_ids <- paste0("amount", LETTERS[1:3])
output$amounts <- renderUI({
num_inputs <- lapply(1:3, function(i){
numericInput(inputId = num_ids[i], label = paste0("Investment in ", company_names[i]), value = 0, min = 0, max = 5000)
}) %>% tagList
shinyjs::hidden(num_inputs)
})
observeEvent(eventExpr = input$company, handlerExpr = {
if(length(input$company) == 0){
for(i in num_ids){
shinyjs::hide(id = i)
}
} else {
for(i in input$company){
shinyjs::toggle(id = paste0("amount", i), condition = input$company)
}
}
})
}
shinyApp(ui = ui, server = server)
The problem with my app is that the intended dynamics between the checkboxGroupInput and the numericInput fields are not working as intended. For instance, once a numericInput is shown, it cannot be hidden anymore by unchecking the boxes. How can I handle this?
The code pasted above is fully functional. Thank you very much.
I fixed your code by explicitly show/hide the numericInput when the corresponding check box is selected/unselected. Also I change the observeEvent with observe to make sure that the observer reacts when none of the check boxes are selected.
library(shiny)
library(shinyjs)
library(magrittr)
ui <- fluidPage(
useShinyjs(),
checkboxGroupInput(inputId = "company", label = "Select a company", choices = LETTERS[1:3]),
uiOutput(outputId = "amounts")
)
server <- function(input, output){
company_names <- LETTERS[1:3]
num_ids <- paste0("amount", LETTERS[1:3])
output$amounts <- renderUI({
num_inputs <- lapply(1:3, function(i){
numericInput(inputId = num_ids[i], label = paste0("Investment in ", company_names[i]), value = 0, min = 0, max = 5000)
}) %>% tagList
shinyjs::hidden(num_inputs)
})
observe({
for(i in company_names){
if (i %in% input$company) {
shinyjs::show(id = paste0("amount", i))
} else {
shinyjs::hide(id = paste0("amount", i))
}
}
})
}
shinyApp(ui = ui, server = server)