condition in conditionalPanel in shiny R not working well - r

I would like to create a dynamic app that depending on an input pops out other inputs or not. In the code below I want to pop out the checkboxInput with label x when the selectInput with label mdl is "First model". When I run the app and select the First model from the list the other checkboxInput does not appear. I know the condition has to be in javascript but I don't know that language. However I think that one of the conditions is right. Any suggestions? i have tried both codes shown below.
library(shiny)
ui <- fluidPage(
selectInput(inputId = "mdl", label = "Model", choices = list("First model",
"Second model", "Third model"),
conditionalPanel(
condition = "input.mdl == 'First model'",
checkboxInput(inputId = "x", label = "Length")
)
),
)
server <- function(input, output){
}
shinyApp(ui = ui, server = server)
library(shiny)
ui <- fluidPage(
selectInput(inputId = "mdl", label = "Model", choices = list("First model",
"Second model", "Third model"),
conditionalPanel(
condition = "input.mdl == First model",
checkboxInput(inputId = "x", label = "Length")
)
),
)
server <- function(input, output){
}
shinyApp(ui = ui, server = server)

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"

Comparing string/character inputs in R Shiny dashboard

I have the code below in a shiny dashboard where I want to display different things based on what the user have selected from the drop-down menu. However, the if condition always returns FALSE.
What am I missing here?
#ui.r
body <- dashboardBody(
selectInput(
inputId = "feel",
label = "choose level",
choices = c(
"Easy" = "1",
"Advanced" = "2"
),
selected = "1",
multiple = FALSE
)
if(textOutput("feel")=="1") {
}
)
#server.r
function (input,output){
output$feel<-renderText({
input$feel
})
}
You should do all the business logic inside the server.R
library(shiny)
ui <- fluidPage(
column(2,
selectInput(inputId = "feel",label = "choose level", choices = c("Easy"="1", "Advanced"="2"),
selected = "1", multiple = FALSE)
),
column(2,
textOutput("feeloutput")
)
)
server <- function(input, output, session) {
output$feeloutput <- renderText({
if(input$feel == "1"){
"Show something"
}
else{
"Show something else"
}
})
}
shinyApp(ui = ui, server = server)

dynamic number of selectInput

I am new to shiny so this might be a very basic question.
I want to write a shiny app where the user inputs 'n' and we get n number of selectInput options and am not able to do it. Basically any form of for loop is not working.
The code I attempted is following
library(shiny)
ui = fluidPage(
sidebarLayout(
sidebarPanel(
textInput(inputId = "number", label = "number of selectInput",value = 5)
),
mainPanel(
uiOutput(outputId = "putselect")
)
)
)
server = function(input,output){
output$putselect = renderUI(
if(input$number != 0 ){
for(i in 1:(input$number)){
selectInput(inputId = "i", label = "just write something", choices = c(2,(3)))
}
}
)
}
shinyApp(ui = ui , server = server)
You either need to store the inputs you create in a list and return that list, or you can simply wrap your statement in lapply instead of for. A working example is given below, hope this helps!
library(shiny)
ui = fluidPage(
sidebarLayout(
sidebarPanel(
textInput(inputId = "number", label = "number of selectInput",value = 5)
),
mainPanel(
uiOutput(outputId = "putselect")
)
)
)
server = function(input,output){
output$putselect = renderUI(
if(input$number != 0 ){
lapply(1:(input$number), function(i){
selectInput(inputId = "i", label = paste0("input ",i), choices = c(2,(3)))
})
}
)
}
shinyApp(ui = ui , server = server)

Add items to the list of the selectInput by using button (RShiny)

I have the following code:
library(shiny)
vec <- seq(1,10)
ui <- fluidPage(
titlePanel(""),
sidebarLayout(
sidebarPanel(
fluidRow(
selectInput("selection", "Select number", vec, multiple = TRUE),
actionButton("First_fives", "First Fives" ),
actionButton("Last_fives", "Last Fives"),
actionButton("ok", "OK"))
),
mainPanel(
fluidRow(
h5("Selected numbers:")),
textOutput('num')
)
)
)
server <- function(input, output, session) {
observeEvent(input$First_fives,{
updateSelectInput(session, inputId = "selection", choices = vec[1:5])
})
observeEvent(input$Last_fives,{
updateSelectInput(session, inputId = "selection", choices = vec[6:10])
})
data <- reactiveValues()
observeEvent(input$ok,{
data$selected <- input$city
})
output$num <- renderText({data$selected})
}
shinyApp(ui = ui, server = server)
I almost managed to do what I want but not quite.
My selectInput box is empty when running the code and you can select amongst 10 items (from 1 to 10). This is fine.
Now I would like, when clicking on the button "First fives", the numbers 1 to 5 to be added to this empty box. In others words I would like to get the same as on the picture below in one click.
Please add selected on the updateSelectInput. The code will be like this:
observeEvent(input$First_fives,{
updateSelectInput(session, inputId = "selection", choices = vec[1:5],selected = vec[1:5])
})
observeEvent(input$Last_fives,{
updateSelectInput(session, inputId = "selection", choices = vec[6:10],selected = vec[6:10])
})
Please not I have only checked this function,not others.
Pls check if this meet your requirements.

R Shiny - how to display choice label in selectInput

I defined a selectInput as below. I want to access the label of each choice, and render it on the main panel.
If the user selects "Sugar sweetened bev.", I want to render on the main panel something like this:
"You chose Sugar sweetened bev.", but instead I get "You chose ssb".
The reason I setup my selectInput choices this way is because I want the left-hand side for the title of the graph, and the right-side is the variable name.
Any advice or alternative direction is much appreciated!
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput("foodvars", "Select food:",
choices = c("Beef/Pork" = "beefpork",
"Sugar sweeteened bev." = "ssb",
"Total fruit" = "total_fruit"))),
mainPanel(
textOutput("dispText")))
)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput("foodvars", "Select food:",
choices = c("Beef/Pork" = "beefpork",
"Sugar sweeteened bev." = "ssb",
"Total fruit" = "total_fruit"))),
mainPanel(
textOutput("dispText")))
)
server <- function(input, output) {
output$dispText <- renderText({
paste("You chose ",input$foodvars)})
}
shinyApp(ui = ui, server = server)
We create same named vector globally and then retrieve the name with names on a logical vector
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput("foodvars", "Select food:",
choices = c("Beef/Pork" = "beefpork",
"Sugar sweeteened bev." = "ssb",
"Total fruit" = "total_fruit"))),
mainPanel(
textOutput("dispText")))
)
choiceVec <- c("Beef/Pork" = "beefpork",
"Sugar sweeteened bev." = "ssb",
"Total fruit" = "total_fruit")
server <- function(input, output) {
output$dispText <- renderText({
paste("You chose ",names(choiceVec)[choiceVec == input$foodvars])})
}
shinyApp(ui = ui, server = server)
-output

Resources