在闪亮的应用程序中显示一个按钮

时间:2018-04-02 12:56:04

标签: r shiny

我有以下Shiny Application:

library(shiny)
library(shinyjs)
library(shinydashboard)

UI <- fluidPage(
     actionButton("get_tweets", "Fetch tweets"),
     numericInput("tweet_amount", "Set the amount of Tweets", 10, min = 10, max = 1000),
     selectInput("tweet_name", "Select the tweeter", selected = NULL, choices = c("@RealDonaldTrump")),

     #Set hidden buttons
     hidden(
       div(id="status_update",
           verbatimTextOutput("status")
           )
     ),
     hidden(
       div(id="tweet_fetcher",
        verbatimTextOutput("status2")
       )
     )
)

Server <- function(input, output){

  list = c(1,2,3)
  get_connected <- reactive({
    for(i in 1:length(list)){
      Sys.sleep(2)
    }
  })

  observeEvent(input$get_tweets, {
    get_connected()
    toggle("status_update")
    output$status <- renderText({"You're now connected to the API"})
    toggle("tweet_fetcher")
    output$status2 <- renderText("Test")
  })

}

shinyApp(ui = UI, server = Server)

现在我正在工作。但是,理想情况下我想确保出现一个按钮。因此我想改变:

output$status2 <- renderText("Test") 

和这个

verbatimTextOutput("status2") #actionButton("status2", "a_button")

这不起作用。如果我想让JS出现按钮,我应该使用什么提示?

1 个答案:

答案 0 :(得分:1)

如果我正确理解了你想要交换的问题 verbatimTextOutput("status2")actionButton("status2", "a_button")

然后你应该使用renderUI()

  • 服务器端:output$status2 <- renderUI(actionButton("status2", "a_button"))

  • UI方面:uiOutput("status2")

完整的应用程序会显示:

library(shiny)
library(shinyjs)
library(shinydashboard)

UI <- fluidPage(
  actionButton("get_tweets", "Fetch tweets"),
  numericInput("tweet_amount", "Set the amount of Tweets", 10, min = 10, max = 1000),
  selectInput("tweet_name", "Select the tweeter", selected = NULL, choices = c("@RealDonaldTrump")),

  #Set hidden buttons
  hidden(
    div(id="status_update",
        verbatimTextOutput("status")
    )
  ),
  hidden(
    div(id="tweet_fetcher",
        uiOutput("status2")
    )
  )
)

Server <- function(input, output){

  list = c(1,2,3)
  get_connected <- reactive({
    for(i in 1:length(list)){
      Sys.sleep(2)
    }
  })

  observeEvent(input$get_tweets, {
    get_connected()
    toggle("status_update")
    output$status <- renderText({"You're now connected to the API"})
    toggle("tweet_fetcher")
    output$status2 <- renderUI(actionButton("status2", "a_button"))
  })

}

shinyApp(ui = UI, server = Server)