change data for next input dynamically - r

i have two select input in my
UI.R
library(shiny)
ContextElements = c("choice1", "choice2", "choice3")
shinyUI(pageWithSidebar(
headerPanel(""),
sidebarPanel(
h4("test for selectize input"),
selectizeInput(inputId = "firstAxe", label = h4("First Axe"),
choices=ContextElements, selected = NULL, multiple = FALSE,
options = NULL),
hr(),
selectizeInput(inputId = "twoAxe", label = h4("Second Axe"),
choices=NULL, selected = NULL, multiple = FALSE,
options = NULL)
),
mainPanel(
)
))
i want that the next selectizeinput choices ("twoAxe") to be filled dynamically with the remained choices drom the first one, that's mean if i choosed the first choice if the first one, the second choices will be choice2 and choice3
Thanks

You can do this in server.r using updateSelectizeInput . Lets assume that ContextElements is available to the server - either defined in global.r or in both server.r and ui.r. Then this should work:
observe({
input$firstAxe # makes the second selectize change when the first one does
updateSelectizeInput({session, inputId="twoAxe",
choices=as.list( ContextElements[!ContextElements %in% input$firstAxe] ) })
})
As you change you choice in firstAxe you should see the choices in twoAxe respond.

Related

Show selectInput in rshiny based on condition (conditionalPanel)

I want to create an app that allows you to select one variable based on a condition.
So I have create a switchInput with conditions Yes, and No, and as you can see, a stratify SelectInput should appear in case Yes is marked.
However, no new SelectInput is displayed:
# Shiny
library(shiny)
library(shinyWidgets)
library(shinyjqui)
# Data
library(readxl)
library(dplyr)
# Plots
library(ggplot2)
not_sel <- "Not Selected"
# User interface
ui <- navbarPage(
main_page <- tabPanel(
title = "",
titlePanel(""),
sidebarLayout(
sidebarPanel(
title = "Inputs",
fileInput("xlsx_input", "Select XLSX file to import", accept = c(".xlsx")),
selectInput("num_var_1", "Variable X axis", choices = c(not_sel)),
selectInput("num_var_2", "Variable Y axis", choices = c(not_sel)),
switchInput(
inputId = "Id013",
onLabel = "Yes",
offLabel = "No"
),
conditionalPanel(
condition = "Id013 == 'Yes'", selectInput("Stratify", "Stratify", choices = c(not_sel)), #uiOutput("factor"),
),
actionButton("run_button", "Run Analysis", icon = icon("play"))
),
mainPanel(
tabsetPanel(
tabPanel(
title = "Plot",
br(),
plotOutput("sel_graph")
)
)
)
)
)
)
# Server
server <- function(input, output){
# Dynamic selection of the data. We allow the user to input the data that they want
data_input <- reactive({
#req(input$xlsx_input)
#inFile <- input$xlsx_input
#read_excel(inFile$datapath, 1)
iris
})
}
# Connection for the shinyApp
shinyApp(ui = ui, server = server)
I understand, based on the conditionalPanel function:
Creates a panel that is visible or not, depending on the value of a JavaScript expression. The JS expression is evaluated once at startup and whenever Shiny detects a relevant change in input/output.
That the change on the switchInput value should be enough to generate this changes in the UI interface.
As said in the docs of conditionalPanel():
For example, if you have an input with an id of foo, then you can use input.foo to read its value.
So you need to use input.Id013 instead of Id013 in the condition. Also, even if the labels of the switch are "Yes" or "No", it returns a value TRUE/FALSE (which are written "true" or "false" in Javascript). So the condition you need to use is:
condition = "input.Id013 == true"

How can I create a conditional selectInput widget in R Markdown?

The purpose is to choose a county from a state. I first create a selectInput widget for choosing a state. Then I create a selectInput widget for choosing a county from the selected state. In an R Markdown, the code is as follows:
inputPanel(
selectInput(inputId = "State", label = "Choose a state:", choices = state.name),
selectInput(inputId = "County", label = "Choose a county:", choices = input.State)
)
I guess the use of input.State is problematic, but I don't have any other idea.
Thanks for your time!
There are a number of ways to create conditional/dynamic UI in Shiny (see here). The most straightforward is usually renderUI. See below for a possible solution for you. Note that this requires Shiny so if you’re using R Markdown make sure to specify runtime: shiny in the YAML header.
library(shiny)
# I don't have a list of all counties, so creating an example:
county.name = lapply(
1:length(state.name),
function(i) {
sprintf("%s-County-%i",state.abb[i],1:5)
}
)
names(county.name) = state.name
shinyApp(
# --- User Interface --- #
ui = fluidPage(
sidebarPanel(
selectInput(inputId = "state", label = "Choose a state:", choices = state.name),
uiOutput("county")
),
mainPanel(
textOutput("choice")
)
),
# --- Server logic --- #
server = function(input, output) {
output$county = renderUI({
req(input$state) # this makes sure Shiny waits until input$state has been supplied. Avoids nasty error messages
selectInput(
inputId = "county", label = "Choose a county:", choices = county.name[[input$state]] # condition on the state
)
})
output$choice = renderText({
req(input$state, input$county)
sprintf("You've chosen %s in %s",
input$county,
input$state)
})
}
)
Hope this helps!

Show pickerInput depending on checkBoxInput result R shiny

I am trying to make a dynamic UI for my shiny dashboard. Here, I want to show a pickerInput field only when the input in a checkboxGroup is a specific value. For example, when the input from the checkboxGroup field is A, I want to show the pickerInput field, otherwise I want to show a different input field.
Currently, the part of my code looks, using conditionalPanel, like the following:
output$UI_selection <- renderUI({
tagList(
p(tags$i("Define the network")),
checkboxGroupInput(inputId = "choice1",
label = "Make a choice",
choices = list("A", "B")
),
conditionalPanel(condition = "input$choice1 == 'A'",
pickerInput(inputId = "select1",
label = "Select first:",
choices = list(
"Hierarchies" = grouplist_1),
selected = NULL,
options = list(`actions-box` = TRUE, `none-selected-text` = "Select hierarchy", `live-search` = TRUE, title = "Select hierarchy"),
multiple = FALSE
)
)
)
})
However, this doesn't work and shows both the checkboxGroupInput as well as the PickerInput. Does anyone know how to fix this?
The shiny package functions (such as conditionalPanel) translate all of the R language code you supply into JS. Conditions you supply in conditionalPanel need to be interpretable in JS, which uses . in place of $.
You need to replace your condition = "input$choice1 == 'A'" with condition = "input.choice1 == 'A'".
Full working app is here:
library(shiny)
library(shinyWidgets)
ui <- fluidPage(
uiOutput("UI_selection")
)
server <- function(input, output, session) {
output$UI_selection <- renderUI({
tagList(
p(tags$i("Define the network")),
checkboxGroupInput(inputId = "choice1",
label = "Make a choice",
choices = list("A", "B")
),
conditionalPanel(condition = "input.choice1 == 'A'",
pickerInput(inputId = "select1",
label = "Select first:",
choices = list(
"Hierarchies" = c("X","Y","Z")),
selected = NULL,
options = list(`actions-box` = TRUE, `none-selected-text` = "Select hierarchy", `live-search` = TRUE, title = "Select hierarchy"),
multiple = FALSE
)
)
)
})
}
shinyApp(ui, server)

Rshiny: How to pass renderUI output to chocies parameter in Selectinput

I am trying to input renderUI output in server.UI as an input for choices= parameter in selectInput. The reason being, i need to use the values selected by user as an input for a function. I see names, ID, attr instead of values when I pass these arguments. Below is my code. Appreciate any help. Thank you
The server.r, lists the searchable fields in pubmed ( Eg:title, ALl fields, # UID etc). These list need to pop up for user UI, and when he selects his fields # of interest. The selection need to be passed to other function.
At this time, the code runs but does not give me the list of searchable fields and unable to pass any selection.
server.R
shinyServer(
function(input, output) {
output$Choose_Feild <- renderUI({
a = data.frame(entrez_db_searchable(db = "pubmed",
config = NULL))
unlist(a$FullName, use.names = FALSE)
}))
ui.R
shinyUI(
fluidPage(
titlePanel(title = "My data "),
sidebarLayout(
sidebarPanel(
h3("Enter values"),
selectInput( inputId = "var_y",label = "Field" , choices =
uiOutput("Choose_Feild") )))
You simply cannot use renderUI in such a way.
Since choices don't depend on any input you can pass choices directly:
library(shiny)
shinyUI(bootstrapPage(
selectInput(
inputId = "var_y", label = "Field" ,
choices = sapply(
rentrez::entrez_db_searchable(db = input$db, config = NULL),
function(x) x$FullName, USE.NAMES=FALSE
)
)
))
If you want to generate choices dynamically, for example based on another input you can use updateSelectInput:
server.R
shinyServer(function(input, output, session) {
observe({
choices <- sapply(
rentrez::entrez_db_searchable(db = input$db, config = NULL),
function(x) x$FullName, USE.NAMES=FALSE
)
updateSelectInput(session, "var_y", choices=choices)
})
})
ui.R
shinyUI(bootstrapPage(
selectInput(
inputId = "db", label="DB",
choices=c("pmc"="pmc", "pubmed"="pubmed")
),
selectInput(
inputId = "var_y", label = "Field" ,
choices = c()
)
))

How to get the drop box empty at the start in shiny

I use drop box to display three options,
selectInput("select", label = h5("Choose Annotation DB"),
choices = list("h10kcod.db"="h10kcod","h20kcod.db"="h20kcod","hwgcod.db"="hwgcod")),
selected = NULL)
But it always have the first choice in the drop box already selected, however i am interested to get the drop box empty(with no one selected). How should i do that. Thanks
Hello look at this example :
library("shiny")
ui = fluidPage(
# method 1 : put an empty element "" in your list
selectInput("select", label = h5("Choose Annotation DB"),
choices = list("", "h10kcod.db"="h10kcod","h20kcod.db"="h20kcod","hwgcod.db"="hwgcod")),
# method 2 : use selectizeInput and the placeholder option
selectizeInput("select", label = h5("Choose Annotation DB"),
choices = list("h10kcod.db"="h10kcod","h20kcod.db"="h20kcod","hwgcod.db"="hwgcod"),
options = list(placeholder = 'A placeholder',
onInitialize = I('function() { this.setValue(""); }')))
)
server = function(input, output) {
}
shinyApp(ui = ui, server = server)

Resources