I'm trying to write a simple shiny app with an actionButton. When the actionButton is pressed, some text output should be printed directly below. The code below takes me part way to a solution:
shinyApp(
ui = shinyUI( fluidPage(
actionButton("button", "don't press the button"),
verbatimTextOutput("text")
)
),
server = function(input, output, session){
observeEvent(input$button, {
output$text <- renderText({"ahh you pressed it"})
})
}
)
There are two things I'd like to change, but not sure how:
1) The code above shows an empty gray box before the button is pressed - I'd like there to be nothing there prior to the button being pressed. It looks like conditionalPanel might be the right approach, but not sure how to implement this.
2) Can the above code be adapted so that once the button is pressed a second time, the text output is hidden again?
You can try this with shinyjs, hidden and toggle
library(shiny)
library(shinyjs)
shinyApp(
ui = shinyUI(fluidPage(useShinyjs(),
actionButton("button", "don't press the button"),
hidden(
div(id='text_div',
verbatimTextOutput("text")
)
)
)
),
server = function(input, output, session){
observeEvent(input$button, {
toggle('text_div')
output$text <- renderText({"ahh you pressed it"})
})
}
)
Related
I have a reset (actionButton) and update button (submitButton) in my Shiny app. The problem is that to reset the app, I have to click on the reset button followed by the update button. Is it possible to reset the app without having to click on update?
EDIT: I do want the app to update only after the user explicitly clicks update. This is because in my app they will have the option to select several selectors to filter the data. Happy to use something else other than submitbutton, but so far this has been the only function that worked for the purpose.
In the example below, I have to click on update twice to get the whole app to reset :
library(shiny)
shinyApp(
ui = basicPage(
numericInput("num", label = "Make changes", value = 1),
submitButton("Update", icon("refresh")),
shinyjs::useShinyjs(),
actionButton("reset", "Reset"),
helpText(
"When you click the button above, you should see",
"the output below update to reflect the value you",
"entered at the top:"
),
verbatimTextOutput("value")
),
server = function(input, output) {
# submit buttons do not have a value of their own,
# they control when the app accesses values of other widgets.
# input$num is the value of the number widget.
output$value <- renderPrint({
input$num
})
observeEvent(input$reset, {
shinyjs::reset("num")
})
}
)
I hope someone can enlighten me!
Perhaps actionButton in combination with updateNumericInput() will meet your needs. Try this
library(shiny)
shinyApp(
ui = basicPage(
numericInput("num", label = "Make changes", value = 1),
actionButton("Update", "refresh"),
shinyjs::useShinyjs(),
actionButton("reset", "Reset"),
helpText(
"When you click the button above, you should see",
"the output below update to reflect the value you",
"entered at the top:"
),
verbatimTextOutput("value")
),
server = function(input, output,session) {
# submit buttons do not have a value of their own,
# they control when the app accesses values of other widgets.
# input$num is the value of the number widget.
observeEvent(input$Update, {
output$value <- renderPrint({
isolate(input$num)
})
})
observeEvent(input$reset, {
#shinyjs::reset("num")
updateNumericInput(session,"num",value=1)
})
}
)
I recently came across the click() function and want to incorporate this in to my app.
https://www.rdocumentation.org/packages/shinyjs/versions/1.1/topics/click
However attempting to run the given example:
if (interactive()) {
library(shiny)
library(shinyjs)
shinyApp(
ui = fluidPage(
useShinyjs(), # Set up shinyjs
"Count:", textOutput("number", inline = TRUE), br(),
actionButton("btn", "Click me"), br(),
"The button will be pressed automatically every 3 seconds"
),
server = function(input, output) {
output$number <- renderText({
input$btn
})
observe({
click("btn")
invalidateLater(3000)
})
}
)
}
# }
Did not give the desired behaviour. It appears no auto-clicking is taking place! Can anybody explain? Thanks in advance.
I am trying to do something which I thought would be relatively simple, but I cannot seem to figure it out.
I am attempting to have an actionLink which, when pressed, provides additional information for the user. When pressed again it hides the information. I can do this fine, but what I am struggling with is updating the text of the actionLink.
I want it to read "Show additional" when the extra information is hidden, and then "Hide additional" when the information is exposed. I have read the following questions/answers, but cannot quite get it to work.
Modify shiny action button once it is clicked
Update label of actionButton in shiny
I have provided a simple code for this below, though the real example will be a lot more complex.
Thank you for your time and help.
shinyApp(
ui = shinyUI(fluidPage(useShinyjs(),
actionLink("button", "Show additional"),
hidden(
div(id='text_div',
verbatimTextOutput("text")
)
)
)
),
server = function(input, output, session){
observeEvent(input$button, {
toggle('text_div')
output$text <- renderText({"Additional"})
})
}
)
You can check for the value of input$button(which increments by 1 each time you click on it) and update the actionLink label parameter in function of its value with updateActionButton :
shinyApp(
ui = shinyUI(
fluidPage(useShinyjs(),
actionLink("button", "Show additional"),
hidden(div(id='text_div', verbatimTextOutput("text")))
)
),
server = function(input, output, session){
observeEvent(input$button, {
toggle('text_div')
output$text <- renderText({"Additional"})
if (input$button %% 2 == 1) {
txt <- "Hide Additional"
} else {
txt <- "Show Additional"
}
updateActionButton(session, "button", label = txt)
})
}
)
My objective is to create a ShinyApp that opens a new empty UI whenever user clicks on submitButton.
Currently this is my code below. If the user types something in the text box and press Submit. The app shows what the user typed in the main panel. However I dont want to see the text, instead when the user clicks on the submit button , it should open a new empty UI.
ui = shinyUI(fluidPage(
titlePanel("submitButton example"),
fluidRow(
column(3, wellPanel(
textInput("text", "Text:", "text here"),
submitButton("Submit")
)),
verbatimTextOutput("text")
)
)
)
server = function(input, output) {
output$plot1 <- renderPlot({
hist(rnorm(input$n))
})
output$text <- renderText({
paste("Input text is:", input$text)
})
}
shinyApp(ui=ui, server=server)
Is this possible ? Any tips or pointers are appreciated.
Well, this is not yet very functional, but does what you asked for.
ui = shinyUI(fluidPage(
titlePanel("submitButton example"),
fluidRow(
uiOutput("newWindowContent", style = "display: none;"),
tags$script(HTML("
$(document).ready(function() {
if(window.location.hash != '') {
$('div:not(#newWindowContent)').hide();
$('#newWindowContent').show();
$('#newWindowContent').appendTo('body');
}
})
")),
a(href = "#NEW", target = "_blank",
actionButton("Submit", "Submit")
)
))
)
server = function(input, output) {
output$newWindowContent <- renderUI({
"Welcome to your new window!"
})
}
shinyApp(ui=ui, server=server)
The app is created, such that the ui created in newWindowContent is displayed in the new window. Sadly, new windows are somewhat cut off from the parent page, such that there is no easy way to configure each page independently. At the moment, all show the same content. None have reactivity features. I guess there can be initial configurations, if one uses the window's hash. But this works only client sided.
Nevertheless, it's a good start!
I am trying to write a little shiny app that takes code entered into a textInput field, and when I press a button, displays it underneath the textInput. I don't want it to automatically update as I'm typing.
It works once, but after I hit the actionButton, the text in the verbatimTextOutput starts updating as I type. How can I stop this so that the verbatinTextOutput only updates when I hit the applyButton? Should I even be using verbatinTextOutput? Thanks.
server.R
library(shiny)
shinyServer(function(input, output) {
# You can access the value of the widget with input$text, e.g.
observeEvent(input$doBtn, {
#... # do some work
output$value <- renderPrint({input$text})
#... # do some more work
})
})
ui.R
library(shiny)
shinyUI(fluidPage(
# Copy the line below to make a text input box
textInput("text", label = h3("Text input"), value = "Enter text..."),
actionButton("doBtn", "Do something"),
hr(),
fluidRow(column(3, verbatimTextOutput("value")))
))
Figured it out.
server.R
library(shiny)
shinyServer(function(input, output) {
output$value <- renderText({
if (input$doBtn == 0)
return()
isolate({input$text})
})
})