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 })
})
)
Related
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)
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))))
}
I am looking for a way to link from an HTML text (nested in the server part) to a specific Shiny tabPanel (nested in UI). Let's say we have the following app:
library(shiny)
shinyUI(fluidPage(
sidebarLayout(
mainPanel(
tabsetPanel(
type="tabs",
tabPanel("Contents", htmlOutput("contents")),
tabPanel("Plot", plotOutput("plot")) # <- A link to here
)
)
)
))
shinyServer(function(input, output) {
output$contents <- renderText({
HTML("A link to <a href='#Plot'>Plot</a>") # <- from there
})
output$plot({
some ggplot
})
})
How could I create a link within the text that then redirects to a certain tab. I tried anchor tags but they don't seem to work as the id keeps changing upon each start of the app.
Thanks in advance.
I don't know whether this is possible with a link. But you can use a button and updateTabsetPanel.
library(shiny)
library(ggplot2)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(),
mainPanel(
tabsetPanel(
type="tabs",
id = "tabset",
tabPanel("Contents", actionButton("go", "Go to plot")),
tabPanel("Plot", plotOutput("plot"))
)
)
)
)
server <- function(input, output, session) {
observeEvent(input$go, {
updateTabsetPanel(session, "tabset", "Plot")
})
output$plot <- renderPlot({
ggplot(mtcars, aes(x=cyl, y=disp)) + geom_point()
})
}
shinyApp(ui, server)
Thanks to Stéphane Laurent, who pointed me in the right direction, I managed to create the solution I wanted. In order to keep all the HTML text in the server function I used a combination of renderUI and actionLink. The solution now looks as follows:
library(shiny)
shinyUI(fluidPage(
sidebarLayout(
mainPanel(
tabsetPanel(
type="tabs",
id = "tabset", # <- Key element 1
tabPanel("Contents", htmlOutput("contents")),
tabPanel("Plot", plotOutput("plot"))
)
)
)
))
shinyServer(function(input, output, session) {
output$contents <- renderUI({ # <- Key element 2
list(
HTML(<p>Some text..</p>),
actionLink("link", "Link to Plot") # <- Key element 3
)
})
observeEvent(input$link, {updateTabsetPanel(session, "tabset", "Plot")}) # <- Key element 4
output$plot({
some ggplot
})
})
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)
ui.r
shinyUI(
fluidPage(
titlePanel("the title"),
mainPanel(
tabsetPanel(tabPanel("Raw Data",verbatimTextOutput("theText")),
tabPanel("Raw Data2",verbatimTextOutput("theText"))
)
)
)
)
server.r
library("shiny")
library("dplyr")
shinyServer(
function(input, output,session) {
print("do it")
output$theText <- renderText({
return("please work")})
}
)
If I remove one tabPanel it works, and "do it" is printed in the console and the title and "please work" is printed in the UI. Otherwise, with both, the UI with two tabs shows up and nothing is printed or displayed within the tabs, though an empty grey box does show up.
Using RStudio 0.99.332, R 3.1.2, shiny 0.11.1
In rshiny one output can go only to one place, which means that you have to create new output for other tabPanel.
library(shiny)
server <- function(input, output, session) {
print("do it")
output$theText <- renderText({
return("please work")})
output$theText2 <- renderText({
return("please work")})
}
ui <- fluidPage(
titlePanel("the title"),
mainPanel(
tabsetPanel(tabPanel("Raw Data",verbatimTextOutput("theText")),
tabPanel("Raw Data2",verbatimTextOutput("theText2"))
)
)
)
shinyApp(ui = ui, server = server)