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)
Related
I am trying to render a few outputs in a shiny application that are contained within a shinyjs::hidden section upon the application running rather than once the section is visible.
EDIT: I had the app set up incorrectly in the original example so have changed it.
I want to be able to run the reactive statement before running the final observe to change the UI from the Alpha text to the Beta text and plot. Ideally this would mean in the console would see "Done plotting" before "Observe run".
library(shiny)
library(shinyjs)
ui <- fluidPage(
useShinyjs(),
div(id = "before-content", h3("Aux Text Alpha")),
shinyjs::hidden(
div(
id = "after-content",
h1("Aux Text Beta"),
plotOutput("text")
)
)
)
server <- function( session,input, output) {
in_plot <- reactive({
Sys.sleep(3)
print("Done plotting")
plot(iris)
})
output$text <- renderPlot({
in_plot()
})
observe({
print("Observe run")
hide("before-content")
show("after-content")
})
}
shinyApp(ui, server)
An alternative would be to have a layer over what is classed as the hidden section but am not too sure on how that is accomplished.
You can hide it in the reactive, like so:
library(shiny)
library(shinyjs)
ui <- fluidPage(
useShinyjs(),
actionButton("button", "Click me"),
plotOutput("text")
)
server <- function( session,input, output) {
in_plot <- reactive({
hide("text")
Sys.sleep(3)
print("Done plotting")
plot(iris)
})
output$text <- renderPlot({
in_plot()
})
observeEvent(input$button, {
show("text")
})
}
shinyApp(ui, server)
Sometimes we'd like to put content in a uiOutput/renderUI. But this doesn't always work. For instance, the example below. In my mind, code#1 and code#2 should give me the same GUI. However, code#2 doesn't work as expected. Can anyone tell me the reason? Thanks!
Code#1:
library(shiny)
ui <- navbarPage("test",
navbarMenu("More",
tabPanel("Table"
)
)
)
server <- shinyServer(function(input, output, session) {
})
shinyApp(ui = ui, server = server)
Code#2:
library(shiny)
ui <- navbarPage("test",
uiOutput("ui_data")
)
server <- shinyServer(function(input, output, session) {
output$ui_data <- renderUI({
navbarMenu("More",
tabPanel("Table"
)
)
})
})
shinyApp(ui = ui, server = server)
In the second example, uiOutput wraps the content of navbarMenu inside a div with the class "shiny-html-output". Divs of this class are however not allowed as an argument for navbarPage. AFAIK, there are two ways to resolve this
The first is to create the whole navbarPage on the server-side.
library(shiny)
ui <- uiOutput("page")
server <- shinyServer(function(input, output, session) {
output$page <- renderUI({
navbarPage("test", navbarMenu("More", tabPanel("Table")))
})
})
shinyApp(ui, server)
The other one is to only create the contents of the tabPanel in the server
library(shiny)
ui <- navbarPage(
"test",
navbarMenu("More", tabPanel("Table", uiOutput("tab_content")))
)
server <- shinyServer(function(input, output, session) {
output$tab_content <- renderUI({
"Some content"
})
})
shinyApp(ui = ui, server = server)
Please try to set your working directory first like example below.
setwd("c:/Users/ID/Desktop/folder")
You should get working directory with location of ui.R and server.R.
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)
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")
})
}
))
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 })
})
)