I am trying to use conditionalPanel to display message when a file is being loaded. However, the panel is not disappearing once the condition is TRUE. I have created a reproducible code below:
server.R
library(shiny)
print("Loading start")
print(paste("1->",exists('FGram')))
FGram <- readRDS("data/UGram.rds")
print(paste("2->",exists('FGram')))
print("Loading end")
shinyServer( function(input, output, session) {
})
ui.R
library(shiny)
shinyUI( fluidPage(
sidebarLayout(
sidebarPanel(
h4("Side Panel")
)
),
mainPanel(
h4("Main Panel"),
br(),
textOutput("First Line of text.."),
br(),
conditionalPanel(condition = "exists('FGram')", HTML("PLEASE WAIT!! <br>App is loading, may take a while....")),
br(),
h4("Last Line of text..")
)
)
)
Conditions supplied to conditionalPanel are executed in the javascript environment, not the R environment, and therefore cannot reference or inspect variables or functions in the R environment. A solution to your situation would be to use uiOutput, as in the example below.
myGlobalVar <- 1
server <- function(input, output) {
output$condPanel <- renderUI({
if (exists('myGlobalVar'))
HTML("PLEASE WAIT!! <br>App is loading, may take a while....")
})
}
ui <- fluidPage({
uiOutput('condPanel')
})
shinyApp(ui=ui, server=server)
Related
There is a function in psych package called 'alpha' which gives out various statistics. I want a specific column from the output so I use a code. This code works perfectly in console but it doesn't work when I try to use it in shiny.
library(shiny)
library(mirt)#This contains a dataset called deAyala
library(psych)#This has the alpha() function
server<- shinyServer(
function(input, output) {
output$data <- renderUI({
alpha(deAyala,warnings=FALSE)$item.stats$raw.r #Warning disables the warnings
})
}
)
ui<- shinyUI(fluidPage(
titlePanel(title = h4("Output", align="center")),
sidebarLayout(
sidebarPanel(
),
mainPanel(
uiOutput("data"),
)
)
))
shinyApp(ui = ui, server = server)
You can use renderTable or renderText to display the output instead of renderUI.
library(shiny)
server<- shinyServer(
function(input, output) {
output$data <- renderTable({
alpha(deAyala,warnings=FALSE)$item.stats$raw.r
})
}
)
ui<- shinyUI(fluidPage(
titlePanel(title = h4("Output", align="center")),
sidebarLayout(
sidebarPanel(
),
mainPanel(
tableOutput("data"),
)
)
))
shinyApp(ui = ui, server = server)
I'm using conditionalPanel to create a UI that first presents a panel with options to the user and then displays a tabbed dashboard using tabsetPanel. The simple act of adding another tabPabel as shown below somehow prevents the server.R file from running. I've tested using print statements. It seems like the Shiny app is breaking, but I can't find a syntax error or any reason for it to be.
conditionalPanel(
condition = "output.panel == 'view.data'",
tabsetPanel(id = "type",
tabPanel("Script", value = "script",
fluidPage(
br(),
fluidRow(
column(3, uiOutput("script.name")),
column(3, uiOutput("script.message"))
),
hr(),
plotlyOutput("plotly")
)
),
tabPanel("Location", value = "location",
fluidPage(
br(),
fluidRow(
# column(3, uiOutput("id.range"))
),
hr(),
plotlyOutput("plot")
)
)
# when this tabPanel is uncommented it doesn't work
# ,tabPanel("Accelerometer", value = "accelerometer",
# fluidPage(
# br(),
# hr(),
# plotlyOutput("plot")
# )
# ),
)
)
It's not failing because of the additional tabPanel, it's failing because it contains a duplicate reference to output$plot. Each output of the server function can only be displayed once. For example, this runs, but will fail if the duplicated line is uncommented:
library(shiny)
ui <- shinyUI(fluidPage(
# textOutput('some_text'),
textOutput('some_text')
))
server <- shinyServer(function(input, output){
output$some_text <- renderText('hello world!')
})
runApp(shinyApp(ui, server))
A simple solution is to save the results of the render* function to a local variable, which you can then save to two outputs:
library(shiny)
ui <- shinyUI(fluidPage(
textOutput('some_text'),
textOutput('some_text2')
))
server <- shinyServer(function(input, output){
the_text <- renderText('hello world!')
output$some_text <- the_text
output$some_text2 <- the_text
})
runApp(shinyApp(ui, server))
I'm experimenting a Shiny App to show dynamic contexts, but I cannot get renderDataTable working into a renderUi component.
Below two simple replicable tests: the first one is not working, the second one without renderUi works fine, of course.
What is the conceptually difference between this two, and why the first one cannot work in Shiny?
This one not works: note that the uiOutput myTable, contains two reactive component, a selectInput and a renderDataTable, but only the selectInput is rendered.
library(shiny)
runApp(list(
ui = fluidPage(
fluidRow(h2("where is the table?")),
uiOutput('myTable')
),
server = function(input, output) {
output$myTable <- renderUI({
fluidPage(
fluidRow(selectInput("test", "test", c(1,2,3))),
fluidRow(renderDataTable(iris))
)
})
}
))
This is fine, both selectInput and renderDataTable are rendered:
library(shiny)
runApp(list(
ui = fluidPage(
fluidRow(h2("where is the table?")),
fluidRow(selectInput("test", "test", c(1,2,3))),
fluidRow(dataTableOutput('myTable'))
),
server = function(input, output) {
output$myTable = renderDataTable(iris)
}
))
How to get the first scenario working?
Thanks.
EDITING after Yihui comment (thanks Yihui):
In renderUi has to be used some ui function, and not some render function:
changed the sample code in the correct way, the result does not change: still no data is shown.
library(shiny)
runApp(list(
ui = basicPage(
uiOutput('myTable')
),
server = function(input, output) {
output$myTable <- renderUI({dataTableOutput(iris)
})
}
))
EDIT n.2
Just solved, got it working so:
library(shiny)
runApp(list(
ui = fluidPage(
mainPanel(
uiOutput('myTable')
)
),
server = function(input, output) {
output$myTable <- renderUI({
output$aa <- renderDataTable(iris)
dataTableOutput("aa")
})
}
))
I have to save the renderTableOutput in a output variable first, and then feeding it to dataTableOutput.
Thanks for pointing me to: here
It would be clearer if you split the part of datatable generation and ui generation :
library(shiny)
runApp(list(
ui = fluidPage(
mainPanel(
uiOutput('myTable')
)
),
server = function(input, output) {
output$aa <- renderDataTable({iris})
output$myTable <- renderUI({
dataTableOutput("aa")
})
}
))
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)
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 })
})
)