There are numerous posts regarding changing titles of other pieces of Shiny apps, e.g.:
Change the title by pressing a shiny button Shiny R
Shiny page title and image
Shiny App: How to dynamically change box title in server.R?
My question is related, but not answered by any of these. I would like to make the <head><title>...</title></head> tag reactive, or at least controllable from within an observeEvent in server.R.
The following does not work, since ui can't find theTitle, but is the kind of approach I'd hope is possible:
library(shiny)
ui <- fluidPage(
title = theTitle(),
textInput("pageTitle", "Enter text:")
)
server <- function(input, output, session) {
theTitle <- reactiveVal()
observeEvent( input$pageTitle, {
if(is.null(input$pageTitle)) {
theTitle("No title yet.")
} else {
theTitle(input$pageTitle)
}
})
}
I've tried making output$theTitle <- renderText({...}) with the if..else logic in that observeEvent, and then setting title = textOutput("theTitle") in ui's fluidPage, but that generates <div ...> as the title text, or <span ...> if we pass inline=True to renderText.
In case this clarifies what I'm looking for, the answer would make something equivalent to the literal (replacing string variables with that string) ui generated by
ui <- fluidPage(
title = "No title yet.",
....
)
before the user has entered any text in the box; if they have entered "Shiny is great!" into input$pageTitle's box, then we would get the literal
ui <- fluidPage(
title = "Shiny is great!",
....
)
One way would be to write some javascript to take care of that. For example
ui <- fluidPage(
title = "No title yet.",
textInput("pageTitle", "Enter text:"),
tags$script(HTML('Shiny.addCustomMessageHandler("changetitle", function(x) {document.title=x});'))
)
server <- function(input, output, session) {
observeEvent( input$pageTitle, {
title <- if(!is.null(input$pageTitle) && nchar(input$pageTitle)>0) {
input$pageTitle
} else {
"No title yet."
}
session$sendCustomMessage("changetitle", title)
})
}
shinyApp(ui, server)
This was created following the How to send messages from the browser to the server and back using Shiny guide
As of June 2021, there is an R package called shinytitle that can update the window title from within Shiny's reactive context: https://cran.r-project.org/package=shinytitle
Related
In Shiny, one can use the following line to force displaying/refreshing an output even if not displayed within the ui:
outputOptions(output, "my_output", suspendWhenHidden = FALSE)
Is there a similar way to "force" input widget creation?
My context: I do have a button that pre-populate a textinput on another tab. Potentially, this textinput may not been generated yet if user didn't go to this specific tab. In such a case, the pre-population legitimely fails.
Good practice would probably be to use a reactiveValues, to feed it with the "pre-populate value" when clicking the button, and then to use this rv within the input widget creation. But I was wondering if a similar option as the above was available in Shiny.
Here’s a simple, working example of the situation I think you are describing.
That is, you don’t need to do anything special.
library(shiny)
ui <- fluidPage(
tabsetPanel(
tabPanel("A", actionButton("populate", "Populate B things")),
tabPanel("B", textInput("b_text", "Add some B text"))
)
)
server <- function(input, output, session) {
observeEvent(input$populate, {
updateTextInput(session, "b_text", value = "Updated from tab A")
})
}
shinyApp(ui, server)
If the input you want to update is generated with uiOutput() then you can
use outputOptions() to force evaluation, like you already mentioned:
library(shiny)
ui <- fluidPage(
tabsetPanel(
tabPanel("A", actionButton("populate", "Populate B things")),
tabPanel("B", uiOutput("b_panel"))
)
)
server <- function(input, output, session) {
observeEvent(input$populate, {
updateTextInput(session, "b_text", value = "Updated from tab A")
})
output$b_panel <- renderUI({
textInput("b_text", "Add some B text")
})
outputOptions(output, "b_panel", suspendWhenHidden = FALSE)
}
shinyApp(ui, server)
I have been trying to make a login for a single tabPanel in Shiny. I have used the shinyAlert method, (as described here: How to access Shiny tab ids for use with shinyalerts?) which works, but unfortunately, it shows parts of the tabPanel's content before the user is logged in.
Is there a way to change this? I am trying to figure out how to make the "backdrop" of the shinyAlert just a white page until the user is successfully logged in. I read that this might be possible with CSS, but it is unclear to me how.
Or is there another method to do this, that I haven't considered? I am pretty new to Shiny.
Edit: the relevant parts of the code.
ui <- fluidPage(navbarPage("Eksempel", theme = shinytheme("cerulean"),
tabPanel("Home", icon = icon("home"),
fluidRow(
box(
Title = "Welcome to the example layout",
width = 10,
solidHeader = TRUE,
"Welcome text")
)),
tabPanel("Prototype", icon = ("chart-line"),
fluidPage(tagList(
textInput("user", "User:"),
passwordInput("password", "Password:"),
uiOutput("secrets"))),
# other tabPanels
server <- function(input, output, session){
output$secrets <- renderUI({
req(input$user == "admin", input$password == "shiny")
fluidPage( #contents of tabPanel, containing different plots ect.
)
})
The contents of the fluidPage I am trying to hide works fine when I don't try to hide it.
You can use req in combination with a renderUI and uiOutput to hide stuff until someone authenticates.
library(shiny)
ui <- fluidPage(
tagList(
textInput("user", "User:"),
passwordInput("password", "Password:"),
uiOutput("secrets")
)
)
server <- function(input, output) {
output$secrets <- renderUI({
req(input$user == "admin", input$password == "stackoverflow")
wellPanel("Hello admin! These are the secrets!")
})
}
shinyApp(ui = ui, server = server)
If you want a more enterprise-ready approach, you can try ShinyProxy or Shiny-Server Pro.
I've put together a simple RShiny app related to finance and trading.
https://obrienciaran.shinyapps.io/cfd_new_value/
This is likely a very basic question, but right now in the side bar you can see text that says 'Current margin:' with a percent under it. Obtained via:
tags$b("Current margin:"),
h5(textOutput("current_margin"))
I just want these outputs side by side so it is displayed as
'Current margin: x%'
and not
'Current Margin:'
'x%'
Use the argument inline=TRUE and put the text "Current margin" and the output in the same div, here it is h5.
library(shiny)
ui <-
fluidPage(
h5(tags$b("Current margin:"),textOutput("current_margin", inline = TRUE))
)
server <- function(input, output, session) {
output$current_margin <- renderText({
"10%"
})
}
shinyApp(ui, server)
I have a code that takes inputs using the readlines(prompt = ) function. Could you tell me which input function in Shiny will be adequate to adapt this code to a Shiny app?
I need an interactive function, I can't use a simple input with selectInput() because I have a lot of readlines(prompt = ) statements.
Something similar to this question:
Include interactive function in shiny to highlight "readlines" and "print"
Florian's answer is nice and easy to use, I would definitely recommend that! But in case you are keen on using prompts for inputs I am adding another solution, using javaScript:
This one shows a prompt when the user presses an actionButton and stores it in an input variable. (it doesn't necessarily have to be after a button press)
library(shiny)
ui <- fluidPage(
tags$head(tags$script("
$(document).on('shiny:inputchanged', function(event) {
if (event.name === 'go') {
var text = prompt('Write me something nice:');
Shiny.onInputChange('mytext', text);
}
});"
)),
actionButton("go", "Click for prompt"),
textOutput("txt")
)
server <- function(input, output, session) {
output$txt <- renderText( {
input$mytext
})
}
shinyApp(ui, server)
Maybe you could use textArea for this purpose. Working example below, hope this helps!
library(shiny)
ui <- fluidPage(
tags$textarea(id="text", rows=4, cols=40),
htmlOutput('val')
)
server <- function(input,output)
{
output$val <- renderText({
text = gsub('\n','<br>',input$text)
text
})
}
shinyApp(ui,server)
This question delas with the combination of navbarMenus and the shinyjs package. I've build a shiny app and I also added an user-login following this suggestion: https://gist.github.com/withr/9001831.
So I know, which user is logged in. Now I'd like to show and hide both a whole navbarMenu and single tabPanels, depending on which user is logged in. Let's say, there are two users, 'admin' and 'custom', and for 'admin' all navbarMenus and all tabPanels should be shown, while 'custom' only gets navbarMenu 2 and within this menu only tabPanel 22 to see.
library(shiny)
library(shinyjs)
# Define UI
ui <-
fluidPage(
titlePanel("NAVBARPAGE"), # title
br(),
################### NAVBAR ############################
navbarPage(
"", # title
################### TABS THEMA #######################
hidden(div(id='m1',
navbarMenu("Menu1",
tabPanel('tab11',plotOutput('tab_11')),
tabPanel('tab12',uiOutput('tab_12'))))),
navbarMenu("Menu2",
tabPanel('tab21',uiOutput('tab_21')),
hidden(div(id='xxx',tabPanel('tab22',uiOutput('tab_22')))))
#######################################################
))
###### SERVER #####
server <- function(input, output,session) {
#Define user
user<-'admin'
observe({
if (user=='admin') {
show('xxx')
show('m1')
}
else {
hide('xxx')
hide(m1)
}
})
# Run the application
shinyApp(ui = ui, server = server)
In this minimal example, the tabPanel ('tab22') is hidden, but is does not show up, when I define
user<-'custom'
Plus, the appearance of the first navbarMenu 'm1' is strange - it is actually not hidden, it is only only empty.
Does anyone know help?
Your code has some errors, missing } at the end of your server function, calling the hide funciton with m1 instead of "m1", and not initialize shinyjs with shinyjs::useShinyjs(). In addition, you can not use a div around navbarMenu or tabPanel, they will not be rendered. Lastly, it would be very easy for any user to show/hide the content.
A better option is to create the navbar according to the user using a uiOutput and control what to render using the user name. Below is an example based on your code to show content based on the users.
library(shiny)
library(shinyjs)
ui <- fluidPage(
selectInput("userSL", 'Users:', c('admin', 'custom')),
titlePanel("NAVBARPAGE"),
br(),
uiOutput("navbarPageUI")
)
server <- function(input, output,session) {
output$navbarPageUI <- renderUI({
user <- input$userSL
if (user == 'admin') {
navbarPage("",
navbarMenu("Menu1",
tabPanel('tab11',plotOutput('tab_11')),
tabPanel('tab12',uiOutput('tab_12'))
),
navbarMenu("Menu2",
tabPanel('tab21',uiOutput('tab_21')),
tabPanel('tab22',uiOutput('tab_22'))
)
)
} else {
navbarPage("",
navbarMenu("Menu2",
tabPanel('tab21',uiOutput('tab_21'))
)
)
}
})
}
shinyApp(ui = ui, server = server)