Set selecInput to have no option selected - r

When running the app, you will see that Option 1 is already selected, however it is strange because in ui I inserted selected="No option selected". So what am I doing wrong?
My idea is that when running the algorithm, there is no option selected in selectInput.
Executable code below:
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
fluidRow(
column(
width = 6,
selectInput("test", label = h5("Choose"),choices = list("Option1 " = "1", "Option2" = "2", selected="No option selected")),
))),
mainPanel(
)
)
)
server <- function(input, output, session) {
}
shinyApp(ui = ui, server = server)

One way is to put an empty string in choices:
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
fluidRow(
column(
width = 6,
selectInput("test",
label = h5("Choose"),
choices = list("", "Option1 " = "1", "Option2" = "2"),
selected=NULL),
))),
mainPanel(
)
)
)
server <- function(input, output, session) {
}
shinyApp(ui = ui, server = server)

Related

materialSwitch does not work inside a renderUI

I'd like to use shinyWidgets::materialSwitch instead of a checkbox in my app for an improved UI.
However, I can't seem to get materialSwitch to work when used with renderUI/uiOutput. The input displays properly but doesn't seem to register a click to "switch".
For the purposes of my app - I need this to be inside a renderUI.
Pkg Versions:
shinyWidgets_0.7.2
shiny_1.7.2
library(shiny)
library(shinyWidgets)
# library(shinyjs)
ui <- fluidPage(
div(class="row",
column(width = 3,
uiOutput("switch")
)
)
)
server <- function(input, output, session) {
output$switch = renderUI({
materialSwitch(
inputId = "switch",
label = "Show Count",
right = TRUE,
status = "primary",
value = FALSE
)
})
}
shinyApp(ui = ui, server = server)
Why is this happening, and how can the problem be fixed?
The issue is that you give same name "switch" to both uiOutput.outputId and materiaSwitch.inputId.
It works OK when they get different ids:
library(shiny)
library(shinyWidgets)
ui <- fluidPage(
div(class="row",
column(width = 3,
uiOutput("switch"),
textOutput("result")
)
)
)
server <- function(input, output, session) {
output$switch = renderUI({
materialSwitch(
inputId = "switchButton",
label = "Show Count",
right = TRUE,
status = "primary",
value = FALSE
)
})
output$result = renderText(input$switchButton)
}
shinyApp(ui = ui, server = server)
Here is how it should work:
library(shiny)
library(shinyWidgets)
# library(shinyjs)
ui <- fluidPage(
div(style = 'position: absolute;left: 50px; top:100px; width:950px;margin:auto',
materialSwitch(inputId = "switch",
label = "Show Count",
right = TRUE,
status = "primary",
value = FALSE)
)
)
server <- function(input, output, session) {
output$value1 <- renderText({ input$switch })
}
shinyApp(ui = ui, server = server)

Make checkboxInput match the appearance of inputSlider in R shiny

In the example below how I can have "Input two" match the appearance of "Input one" in terms of bolding the font and putting the label above the checkbox?
I imagine the answer is some kind of css wizardry but not sure how to approach it.
ui <- fluidPage(
fluidRow(
column(3,
sliderInput(inputId = "foo",label="Input one",min = 0,max = 100,value = 50,step = 10)
),
column(3,
checkboxInput(inputId = "bar",label="Input two")
)
)
)
)
server <- function(input, output, session) {}
shinyApp(ui, server)
One simple solution could be:
library(shiny)
ui <- fluidPage(
fluidRow(
column(3,
sliderInput(inputId = "foo",label="Input one",min = 0,max = 100,value = 50,step = 10)
),
column(3,
p(strong("Input two")),
checkboxInput(inputId = "bar",label=NULL)
)
)
)
server <- function(input, output, session) {}
shinyApp(ui, server)

How to switch between shiny tab panels from inside a module?

I'm trying to modularize a shiny app that has a button to reactively switch between tabPanels using updateTabsetPanel(). This works in the non-modular app, but doesn't when making the reactive is contained in a module. I think it has to do with the panels being outside the scope of the module. Below are minimal examples (the non-modular version that works and the modular version that does not). Is there a way to have the module talk to the tabPanels?
# Non-modular version
library(shiny)
library(shinydashboard)
ui <- fluidPage(
fluidRow(
column(
width = 12,
tabBox(
id = "tabset2",
type = "tabs",
selected = "1",
tabPanel("T1", value = "1",
fluidRow(
box(width = 9, "Some content here"),
box(width = 3,
actionButton("switchbutton", "Click to swtich")
)
)
),
tabPanel("T2", value = "2",
fluidRow(
box(width = 9, "Some different content here")
)
)
)
)
)
)
server <- function(input, output, session) {
observeEvent(input$switchbutton, {
updateTabsetPanel(session = session,
inputId = "tabset2",
selected = "2")
})
}
shinyApp(ui, server)
# Modular version
library(shiny)
library(shinydashboard)
switcherButton <- function(id) {
ns <- NS(id)
fluidRow(
column(
width = 12,
tabBox(
id = "tabset2",
type = "tabs",
selected = "1",
tabPanel("T1", value = "1",
fluidRow(
box(width = 9, "Some content here"),
box(width = 3,
actionButton(ns("switchbutton"), "Click to switch")
)
)
),
tabPanel("T2", value = "2",
fluidRow(
box(width = 9, "Some different content here")
)
)
)
)
)
}
switcher <- function(input, output, session, parent) {
observeEvent(input$switchbutton, {
updateTabsetPanel(session = session,
inputId = "tabset2",
selected = "2")
})
}
ui <- fluidPage(
switcherButton("switcher1")
)
server <- function(input, output, session) {
callModule(switcher, "switcher1")
}
shinyApp(ui, server)
I
You have forgotten to wrap the tabBox id in ns(). Just replace "tabset2" by ns("tabset2")

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)

How to put h4() and selectInput in-line

I am new in shiny, I wonder how to put the "=" close beside the selectInput box?
library(shiny)
ui = fluidPage(
mainPanel(
titlePanel("Calculation:"),#Voltage calculation
fluidRow(
column(3,
selectInput("selc11", h4("Cable"),#Resistivity
choices = list("Copper" = 0.0174, "Alum" = 0.0282), selected = 1)),
h4("=")
)
)
)
server = function(input, output) {
}
shinyApp(ui = ui, server = server)
If you want something like this:
You can achive it with:
library(shiny)
library(shinyjs)
ui = fluidPage(
useShinyjs(),
mainPanel(
titlePanel("Calculation:"),#Voltage calculation
fluidRow(
column(1, h4('Cable')),
column(3, selectInput(
"selc11",
label = '',
choices = list("Copper" = 0.0174, "Alum" = 0.0282), selected = 1)
),
column(3, h4("="))
)
)
)
server = function(input, output) {
runjs("$('label.control-label').remove()")
}
shinyApp(ui = ui, server = server)

Resources