简体   繁体   中英

Hide renderPrint Pre Tag Output from Shiny Application in R

I am developing a Shiny application in R. For certain pieces of renderPrint output I would like the shiny user interface to display nothing. Kind of like the hidden option for pre or div tags in HTML5 example shown below:

http://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_global_hidden

Below is my shiny example code. A brief explanation: you can use a drop down menu to select one of two variables (factor variable or continuous variable). If you select the factor variable I want to show the caption and the table output. If you select the continuous variable I don't want to see anything. Right now, the caption disappears if you insert a blank string "" as the return to renderText . However, I don't know how to get renderPrint to show nothing. I've tried:

  1. "" . Doesn't work as it returns the actual blank string
  2. NULL . Doesn't work as it returns the string NULL
  3. invisible() . Best so far, but still doesn't work as it returns the grey formatted box.

Goal is to just display nothing. Shiny ui.r and server.r code given below: library(shiny)

##
## Start shiny UI here
##
shinyUI(pageWithSidebar(
headerPanel("Shiny Example"),
    sidebarPanel(
        wellPanel(
        selectInput(    inputId = "variable1",label = "Select First Variable:", 
                choices = c("Binary Variable 1" = "binary1",
                "Continuous Variable 1" = "cont1"),
                selected = "Binary Variable 1"
        )
        )


),
mainPanel(
h5(textOutput("caption2")),
verbatimTextOutput("out2")
)
))

##
## Start shiny server file and simulated data here
##
binary1 <- rbinom(100,1,0.5)
cont1   <- rnorm(100)
dat <- as.data.frame(cbind(binary1, cont1))
dat$binary1 <- as.factor(dat$binary1)
dat$cont1 <- as.numeric(dat$cont1)

library(shiny)

shinyServer(function(input, output) {

inputVar1 <- reactive({
parse(text=sub(" ","",paste("dat$", input$variable1)))
})

output$caption2 <- renderText({
if ( (is.factor(eval(inputVar1()))==TRUE) ) {
caption2 <- "Univariate Table"
} else {
if ( (is.numeric(eval(inputVar1()))==TRUE) ) {
caption2 <- ""
}
}
})

output$out2 <- renderPrint({
if ( (is.factor(eval(inputVar1()))==TRUE) ) {
table(eval(inputVar1()))
} else {
if ( (is.numeric(eval(inputVar1()))==TRUE) ) {
invisible()
} 
}
})
})

A few questions...

  1. Why does renderText handle hidden/invisible presentation different than renderPrint ? Is it because the former outputs text as pre tag; whereas, the latter displays formatted output in div tag?
  2. To those HTML experts (upfront, I am not one)...what option would be best to get my output to display nothing? Is the hidden option embedded in a pre or div tag best (I know it doesn't work in IE browsers). Should I try something else? CSS options, etc?
  3. Assuming hidden is the best way to go (or that I get an answer to 2. above), how do I pass this option/argument through the renderPrint function in shiny? Or would I need to use a different shiny function to achieve this functionality?

Btw...My R version is: version.string R version 3.0.1 (2013-05-16) and I am using shiny version {R package version 0.6.0} . Thanks in advance for your help.

I am not sure that I have understood your question, but try the following: here is the Ui first:

library(shiny)
 ui <- fluidPage(pageWithSidebar(
         headerPanel("Shiny Example"),
          sidebarPanel(
           wellPanel(
           selectInput(inputId = "variable1",label = "Select First Variable:", 
                  choices = c("Binary Variable 1" = "binary1",
                              "Continuous Variable 1" = "cont1"),
                  selected = "Binary Variable 1"
  )
 )
),
   mainPanel(
     h5(textOutput("caption2")),
     verbatimTextOutput("out2", placeholder=TRUE)
 )
 ))

Start shiny Server file and simulated data here:

binary1 <- rbinom(100,1,0.5)
cont1   <- rnorm(100)
dat <- as.data.frame(cbind(binary1, cont1))
dat$binary1 <- as.factor(dat$binary1)
dat$cont1 <- as.numeric(dat$cont1)

server <- (function(input, output) {
  inputVar1 <- reactive({
   parse(text=sub(" ","",paste("dat$", input$variable1)))
  })

 output$caption2 <- renderText({
   if ((is.factor(eval(inputVar1()))==TRUE) ) {
     caption2 <- "Univariate Table"
   } else {
     if ((is.numeric(eval(inputVar1()))==TRUE) ) {
       caption2 <- "Continous"
    }
  }
})

 output$out2 <- renderPrint({
   if ((is.factor(eval(inputVar1()))==TRUE) ) {table(eval(inputVar1()))} 
 })
})

And finally:

shinyApp(ui = ui, server = server)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM