It should be easy to prevent "London" showing up at the top of the page, but I couldn't find how.
library(shiny)
ui <- fluidPage(
mainPanel(
mylist <- c("London","Paris"),
selectInput("s", "Select", mylist)
)
)
server <- function(input, output) {}
shinyApp(ui = ui, server = server)
Take mylist out of fluidPage:
library(shiny)
mylist <- c("London","Paris")
ui <- fluidPage(
mainPanel(
selectInput("s", "Select", mylist)
)
)
server <- function(input, output) {}
shinyApp(ui = ui, server = server)
Otherwise mylist is included in the mainPanel function as another output to include such as a header.
Of course, as an alternative you could just include your option list directly in selectInput and omit the mylist vector completely:
selectInput("s", "Select", choices = c("London","Paris"))
You can use the vector into direct inside selectInput like:
library(shiny)
ui <- fluidPage(
mainPanel(
selectInput("s", "Select", choices = c("London","Paris"))
)
)
server <- function(input, output) {}
shinyApp(ui = ui, server = server)
if you have to use some static validation in your option based on selection you can use something like:
library(shiny)
library(shinyalert)
ui <- fluidPage(
mainPanel(
useShinyalert(),
selectInput("option_select", "Select", choices = c("London"="lon","Paris"="par")),
actionButton("check_city","City Selected")
)
)
server <- function(input, output) {
observeEvent(input$check_city,{
if(input$option_select=="lon")
{
shinyalert("City Selected: London")
}
else
{
shinyalert("City Selected: Paris")
}
})
}
shinyApp(ui = ui, server = server)
Related
I am trying to alternate the presence of a TinyMCE editor in R Shiny.
I can load the editor, then remove it with the respective actionButtons. However, upon attempting to load it more than once, only a textAreaInput-type interface is rendered:
library(shiny)
library(ShinyEditor)
ui <- fluidPage(
use_editor("API-KEY"),
uiOutput("tiny"),
actionButton("load", "Load TinyMCE"),
actionButton( "remove", "Remove TinyMCE" ))
server <- function(input, output, session) {
observeEvent(input$load, {
output$tiny = renderUI( editor('textcontent'))})
observeEvent(input$remove, {
output$tiny = renderUI( NULL)})
}
shinyApp(ui = ui, server = server)
How would it be possible to reload it? Thank you.
I would try that:
library(shiny)
library(ShinyEditor)
ui <- fluidPage(
use_editor("API-KEY"),
uiOutput("tiny"),
actionButton("btn", "Load/Remove TinyMCE"),
)
server <- function(input, output, session) {
output$tiny <- renderUI({
if(input$btn %% 2 == 0) {
editor('textcontent')
} else {
NULL
}
})
}
shinyApp(ui = ui, server = server)
And if that doesn't work I would hide it instead of removing it:
library(shiny)
library(ShinyEditor)
ui <- fluidPage(
use_editor("API-KEY"),
conditionalPanel(
condition = "input.btn %% 2 == 0",
uiOutput("tiny")
),
actionButton("btn", "Load/Remove TinyMCE"),
)
server <- function(input, output, session) {
output$tiny <- renderUI({
editor('textcontent')
})
}
shinyApp(ui = ui, server = server)
The following is based on #Stéphane Laurent's advice.
library(shiny)
library(ShinyEditor)
library(shinyjs)
ui <- fluidPage(
use_editor("API-KEY"),
useShinyjs(),
uiOutput("tiny"),
actionButton( "toggle", "Toggle TinyMCE" ))
server <- function(input, output, session) {
output$tiny = renderUI( editor('textcontent'))
observe({if(input$toggle %% 2 == 0) {
hide('tiny')
} else {
show('tiny')
}
})
}
shinyApp(ui = ui, server = server)
User new to Shiny here. It is a very easy mode. But it's not work and it doesn't show any errors.
The same result when I use subset function.
Here's my code:
library(shiny)
library(dplyr)
# Define UI for application that draws a histogram
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput("variable", "Variable:",colnames(iris))
),
mainPanel(
tableOutput("table")
)
)
)
server <- function(input, output) {
output$table <- renderTable({
iris %>% dplyr::filter(input$variable>=5)
})
}
# Run the application
shinyApp(ui = ui, server = server)
input$variable is a character you can use .data pronoun here to use it as column value.
library(dplyr)
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
varSelectInput("variable", "Variable:",iris)
),
mainPanel(
tableOutput("table")
)
)
)
server <- function(input, output) {
output$table <- renderTable({
iris %>% dplyr::filter(.data[[input$variable]]>=5)
})
}
# Run the application
shinyApp(ui = ui, server = server)
Is there a way to close the context menu of a dropdownButton in a shiny app after clicking on a button? I was looking for an attribute like closed/opened in the dropdownButton-documentation and couldn't find anything but I believe there must be a way to do this.
This is an example app:
library(shiny)
library(shinyWidgets)
ui <- fluidPage(
dropdownButton(
actionButton("button", "Press this Button to close the dropdownButton!"),
circle = TRUE, status = "primary", icon = icon("user-circle")
)
)
server <- function(input, output) {
observeEvent(
input$button, {
# Set dropdownButton closed
print("Test")
}
)
}
shinyApp(ui = ui, server = server)
Do you mean something like this?
library(shiny)
library(shinyWidgets)
ui <- fluidPage(
uiOutput('help')
)
server <- function(input, output) {
observeEvent(
input$button, {
shinyjs::hide("button")
#output$help <- renderUI({} )
}
)
output$help <- renderUI(dropdownButton(
actionButton("button", "Press this Button to close the dropdownButton!"),
circle = TRUE, status = "primary", icon = icon("user-circle")
) )
}
shinyApp(ui = ui, server = server)
library(shiny)
library(shinyWidgets)
ui <- fluidPage(
uiOutput('help')
)
server <- function(input, output) {
observeEvent(
input$button, {
shinyjs::hideElement("dropdown-menu")
}
)
output$help <- renderUI(dropdownButton(
actionButton("button", "Press this Button to close the dropdownButton!"),
circle = TRUE, status = "primary", icon = icon("user-circle")
) )
}
shinyApp(ui = ui, server = server)
By dropping "sw-show" class from your dropdown menu, its context will be disappeared.
Use shinyjs::removeClass to do it.
Don't forget to add sw-content- prefix to the menu's ID.
`
library(shiny)
library(shinyjs)
library(shinyWidgets)
ui <- fluidPage(
useShinyjs(),
uiOutput('drop_down_output')
)
server <- function(input, output) {
output$drop_down_output <- renderUI({
dropdown(
inputId = 'drop_down_1',
actionButton("button", "Run!")
)
})
observeEvent(input$button,{
shinyjs::removeClass(id = 'sw-content-drop_down_1', class = 'sw-show')
})
}
shinyApp(ui = ui, server = server)
`
Sometimes we'd like to put content in a uiOutput/renderUI. But this doesn't always work. For instance, the example below. In my mind, code#1 and code#2 should give me the same GUI. However, code#2 doesn't work as expected. Can anyone tell me the reason? Thanks!
Code#1:
library(shiny)
ui <- navbarPage("test",
navbarMenu("More",
tabPanel("Table"
)
)
)
server <- shinyServer(function(input, output, session) {
})
shinyApp(ui = ui, server = server)
Code#2:
library(shiny)
ui <- navbarPage("test",
uiOutput("ui_data")
)
server <- shinyServer(function(input, output, session) {
output$ui_data <- renderUI({
navbarMenu("More",
tabPanel("Table"
)
)
})
})
shinyApp(ui = ui, server = server)
In the second example, uiOutput wraps the content of navbarMenu inside a div with the class "shiny-html-output". Divs of this class are however not allowed as an argument for navbarPage. AFAIK, there are two ways to resolve this
The first is to create the whole navbarPage on the server-side.
library(shiny)
ui <- uiOutput("page")
server <- shinyServer(function(input, output, session) {
output$page <- renderUI({
navbarPage("test", navbarMenu("More", tabPanel("Table")))
})
})
shinyApp(ui, server)
The other one is to only create the contents of the tabPanel in the server
library(shiny)
ui <- navbarPage(
"test",
navbarMenu("More", tabPanel("Table", uiOutput("tab_content")))
)
server <- shinyServer(function(input, output, session) {
output$tab_content <- renderUI({
"Some content"
})
})
shinyApp(ui = ui, server = server)
Please try to set your working directory first like example below.
setwd("c:/Users/ID/Desktop/folder")
You should get working directory with location of ui.R and server.R.
I need to embed a webpage reached through a URL inputted by the user.
I found this script but I can't make the iframe depend on a textInput() containing a URL. This example fails and I am not sure why.
library(shiny)
ui <- fluidPage(
textInput('url','url',value = "www.google.com"),
uiOutput('o')
)
server <- function(input, output, session) {
output$o = renderUI({
tags$iframe(src=input$url)
})
}
shinyApp(ui, server)
You can do like this:
library(shiny)
ui <- fluidPage(titlePanel("Getting Iframe"),
sidebarLayout(
sidebarPanel(
textInput("url", label = "Enter url"),
actionButton("go", "Go")
),
mainPanel(
htmlOutput("frame")
)
))
server <- function(input, output) {
output$frame <- renderUI({
validate(need(input$go, message=FALSE))
tags$iframe(src=isolate(input$url), height=600, width=535)
})
}
shinyApp(ui, server)