hot to make renderValueBox reactive

1,147 views
Skip to first unread message

Federico Urban

unread,
Aug 28, 2015, 1:19:56 PM8/28/15
to Shiny - Web Framework for R

I'm trying to visualize a valueBox using reactive expression.

The code follows

## server.R ##

server <- function(input, output,session) {
  
  inData <- reactive({
    inFile <- input$file1
    if (is.null(inFile))
      return(NULL)
    if (!is.null(inFile))
      return("OK")
  })
  
  output$approvalBox <- renderValueBox({
    if (is.null(inData())){
      valueBox(
        "Arggg", "File not yet uploaded", icon = icon("thumbs-down", lib = "glyphicon"),
        color = "red"
      )
    }
    else if (inData()=="OK"){
      valueBox(
        "Great!", paste("You've uploaded the file: ",input$file1$name), icon = icon("thumbs-up", lib = "glyphicon"),
        color = "green"
      )
    }
  })
  vBoxes <- reactive({
    if (inData()=="OK"){
      output$dataStats1 <- renderValueBox({
        valueBox(
          paste0(100, "%"), "Progress", icon = icon("list"),
          color = "purple"
        )
      })
    }
  })
}


## ui.R ##
library(shinydashboard)
library(shiny)

dashboardPage(
  dashboardHeader(title = "Basic Example"),
  dashboardSidebar(
    sidebarMenu(
      menuItem("Data", tabName = "data", icon = icon("th")),
      menuItem("Analysis",tabName = "analysis", icon = icon("pagelines"))
    )),
  dashboardBody(
    tabItems(
      tabItem(tabName = "data",h3("data"),
              fluidRow(
                box(
                  title = "Import",
                    fileInput('file1', 'Import File',
                              accept=c('sheetName', 'header'), multiple=FALSE)
                  ),
                  valueBoxOutput("approvalBox"),
                  valueBoxOutput("dataStats1")
                )
              ),
      tabItem(tabName = "analysis",
              h2("Data Analysis")
              )
      )
    )
  )

The idea is to display the value boxes only in the occurrence that the file has been imported.

Using the code above the "reactive" value box is not displayed at all.

loiy...@gmail.com

unread,
May 14, 2016, 10:47:51 AM5/14/16
to Shiny - Web Framework for R
I am going through the same problem. Google search on the same issue landed me here. But no one posted any solution to this!

Joe Cheng

unread,
May 15, 2016, 9:24:51 PM5/15/16
to loiy...@gmail.com, Shiny - Web Framework for R
Federico's code was wrong, you don't put outputs inside reactives (or vice versa for that matter). Can you send your code?
--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/ef5cf311-dc3d-4840-b5d0-f79f91cbbbdf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

loiyumba

unread,
May 16, 2016, 12:27:57 AM5/16/16
to Joe Cheng, Shiny - Web Framework for R
Hi Joe, 
Thanks for the reply. Now that you have mentioned about Federico's code, I too noticed the error. For my code after going through the official tutorial pages on shiny dashboard, I fixed it and understood how valueBoxOutput and renderValueBox work. Thanks once again for creating Shiny! 
Regards,
Loy
--
Loiyumba

Joe Cheng

unread,
May 16, 2016, 12:41:34 AM5/16/16
to loiyumba, Shiny - Web Framework for R
Great, thanks!
Reply all
Reply to author
Forward
0 new messages