tutorial publicar examples ejemplos app r shiny

publicar - Brillante: cómo hacer que el valor reactivo se inicialice con el valor predeterminado



shiny r tutorial (2)

shinyServer(function(input, output) { values <- reactiveValues(default = 0) observeEvent(input$goButton,{ values$default <- input$goButton }) # builds a reactive expression that only invalidates # when the value of input$goButton becomes out of date # (i.e., when the button is pressed) ntext <- eventReactive(input$goButton, { input$n }) output$nText <- renderText({ if(values$default == 0){ 50 } else{ ntext() } }) })

Considere la siguiente demo actionButton: http://shiny.rstudio.com/gallery/actionbutton-demo.html

servidor.R:

shinyServer(function(input, output) { # builds a reactive expression that only invalidates # when the value of input$goButton becomes out of date # (i.e., when the button is pressed) ntext <- eventReactive(input$goButton, { input$n }) output$nText <- renderText({ ntext() }) })

ui.R:

shinyUI(pageWithSidebar( headerPanel("actionButton test"), sidebarPanel( numericInput("n", "N:", min = 0, max = 100, value = 50), br(), actionButton("goButton", "Go!"), p("Click the button to update the value displayed in the main panel.") ), mainPanel( verbatimTextOutput("nText") ) ))

En este ejemplo, antes de presionar el botón de acción, el panel del lado derecho está vacío. En cambio, me gustaría que el texto con el valor predeterminado "50" se represente de manera predeterminada.

¿Cómo puedo obtener la salida para mostrar con las entradas predeterminadas si el botón de acción aún no se ha presionado?


observeEvent también toma ignoreNULL como se documenta aquí , que le permite inicializar el objeto sin una instrucción if .

Al agregar el ,ignoreNULL = FALSE a la publicación original (dar o tomar algún formato), verbatimTextOutput muestra 50 en el inicio.

Esto supone un poco de economía en el lado del servidor, supongo.

ui <- fluidPage(titlePanel("actionButton test"), sidebarLayout( sidebarPanel( numericInput( "n", "N:", min = 0, max = 100, value = 50 ), br(), actionButton("goButton", "Go!"), p("Click the button to update the value displayed in the main panel.") ), mainPanel(verbatimTextOutput("nText")) )) server <- function(input, output) { ntext <- eventReactive(input$goButton, { input$n } # Adding this parameter to the original example makes it work as intended # with 50 in the output field to begin with , ignoreNULL = FALSE ) output$nText <- renderText({ ntext() }) } shinyApp(ui = ui, server = server)