Change the title by pressing a shiny button Shiny R - r

library(shiny)
ui <- fluidPage(
h1("Free",align = "center"),
actionButton("go", "Go")
)
server <- function(input, output) {
observeEvent(input$go,{
#change the h1 title for
code("Busy",align="center")
}
}
shinyApp(ui, server)
How to change the title when pressing a button? the idea is to change the word free to busy when the button is pressed.

Would make the h1 header using uiOutput in the ui. Then, you can dynamically change this text to whatever you want in server. Perhaps for your example, you can have a reactiveVal that contains the text you want in the header, which can be modified in your case when the actionButton is pressed.
library(shiny)
ui <- fluidPage(
uiOutput("text_header"),
actionButton("go", "Go")
)
server <- function(input, output) {
rv <- reactiveVal("Free")
observeEvent(input$go, {
rv("Busy")
})
output$text_header <- renderUI({
h1(rv(), align = "center")
})
}
shinyApp(ui, server)

Related

Shiny - Go to another section in same page

I am using below code and try to do below action.
Click on action button to go to next table. How can I do this?
library(shiny)
library(shinydashboard)
library(ggplot2)
ui <- fluidPage(
fluidRow(box(
actionButton("btn1", "Go to Next Table"),
tableOutput("tbl1")
)),
fluidRow(box(
tableOutput("tbl2")
))
)
server <- function(input, output, session) {
output$tbl1 <- renderTable(mtcars)
output$tbl2 <- renderTable(mpg)
}
shinyApp(ui, server)
Here's one solution:
library(shiny)
library(shinydashboard)
library(ggplot2)
ui <- fluidPage(
fluidRow(box(
actionButton("btn1", "Go to Next Table",
onclick = "location.href='#table2';"),
tableOutput("tbl1")
)),
fluidRow(id = "table2", box(
tableOutput("tbl2")
))
)
server <- function(input, output, session) {
output$tbl1 <- renderTable(mtcars)
output$tbl2 <- renderTable(mpg)
}
shinyApp(ui, server)
I've added a unique ID to the location in the UI - here the 2nd fluidRow, then added an onclick javascript function to the actionButton also in the UI. No server function means all the work is done by the user's browser which is handy sometimes.
You can add infinite complexity to the Javascript here to customise it to fit your needs.

Shiny: Render Outputs when hidden

I am trying to render a few outputs in a shiny application that are contained within a shinyjs::hidden section upon the application running rather than once the section is visible.
EDIT: I had the app set up incorrectly in the original example so have changed it.
I want to be able to run the reactive statement before running the final observe to change the UI from the Alpha text to the Beta text and plot. Ideally this would mean in the console would see "Done plotting" before "Observe run".
library(shiny)
library(shinyjs)
ui <- fluidPage(
useShinyjs(),
div(id = "before-content", h3("Aux Text Alpha")),
shinyjs::hidden(
div(
id = "after-content",
h1("Aux Text Beta"),
plotOutput("text")
)
)
)
server <- function( session,input, output) {
in_plot <- reactive({
Sys.sleep(3)
print("Done plotting")
plot(iris)
})
output$text <- renderPlot({
in_plot()
})
observe({
print("Observe run")
hide("before-content")
show("after-content")
})
}
shinyApp(ui, server)
An alternative would be to have a layer over what is classed as the hidden section but am not too sure on how that is accomplished.
You can hide it in the reactive, like so:
library(shiny)
library(shinyjs)
ui <- fluidPage(
useShinyjs(),
actionButton("button", "Click me"),
plotOutput("text")
)
server <- function( session,input, output) {
in_plot <- reactive({
hide("text")
Sys.sleep(3)
print("Done plotting")
plot(iris)
})
output$text <- renderPlot({
in_plot()
})
observeEvent(input$button, {
show("text")
})
}
shinyApp(ui, server)

Rendering of uiOutput in shinyWidget::dropdownButton

I have a uiOutput in a shinyWidget::dropdownButton. My problem is that outputs which depend on the control in uiOutput are not rendered before I click the dropdown button.
library(shiny)
library(shinyWidgets)
ui <- fluidPage(
dropdownButton(uiOutput("placeholder"), icon = icon("cog")),
verbatimTextOutput("out")
)
server <- function(input, output) {
output$placeholder <- renderUI(selectInput("dat", "Select Data:",
choices = c("mtcars", "ChickWeight")))
output$out <- renderPrint(summary(get(req(input$dat))))
}
shinyApp(ui, server)
In this app you see that the summary table is only shown after I have clicked the dropdown button for the first time. From a reactive flow I guess it makes sense, but I would like to know how I force the uiOutput to render (such that the subsequent out verbatim can render)?
You can do like this:
server <- function(input, output) {
output$placeholder <- renderUI(selectInput("dat", "Select Data:",
choices = c("mtcars", "ChickWeight")))
outputOptions(output, "placeholder", suspendWhenHidden=FALSE)
output$out <- renderPrint(summary(get(req(input$dat))))
}

How do I get a clickevent on a verbatimTextOutput in an R Shiny app?

I want to show a plotoutput instead of my verbatimTextOutput when this very verbatimTextOutput is clicked. How do I change my code to make this happen? I did not manage to find a way to get the clickevent.
library(ggplot2)
library(shiny)
ui <- fluidPage(
fluidRow(
verbatimTextOutput("myFirstText")
)
)
server <- function(input, output) {
output$myFirstText=renderText({
"Click on this text to see a histogram of the AirPassengers-data instead of this very text."
})
output$myPlot=renderPlot({
hist(AirPassengers)
})
}
shinyApp(ui, server)
I am not sure if it is really possible to do what you want. But you could use the shinyjs package to get you close. You could try:
ui <- fluidPage(
shinyjs::useShinyjs(),
fluidRow(
a(id = "toggleAdvanced", "Click on this text to see a histogram of the AirPassengers-data instead of this very text.", href = "#"),
shinyjs::hidden(
div(id = "advanced",
plotOutput("myPlot")
)
)
)
)
server <- function(input, output) {
shinyjs::onclick("toggleAdvanced",
shinyjs::toggle(id = "advanced", anim = TRUE))
output$myPlot=renderPlot({
hist(AirPassengers)
})
}
shinyApp(ui, server)

widget example of actionbutton in shiny doesn't work

I'm playing around with shiny, and can't get the simplest action button example to work.
First example found here:http://shiny.rstudio.com/gallery/widgets-gallery.html
Below are the code, which is a copy paste from the website.
#ui.R
shinyUI(fluidPage(
# Copy the line below to make an action button
actionButton("action", label = "Action"),
hr(),
fluidRow(column(2, verbatimTextOutput("value")))
))
#server.R
shinyServer(function(input, output) {
# You can access the value of the widget with input$action, e.g.
output$value <- renderPrint({ input$action })
})
Mine looks like:
http://imgur.com/t0Vx6Wr
edit:
The issue is that it also prints out some class information
Thanks
Use renderText rather then renderPrint if you want it to look like it does on the shiny website:
require(shiny)
runApp(list(ui = fluidPage(
actionButton("action", label = "Action"),
hr(),
fluidRow(column(2, verbatimTextOutput("value")))
)
, server = function(input, output) {
output$value <- renderText({ input$action })
})
)

Resources