renderUI in R shiny doesn't display - r

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.

Related

Paste function not working in title of shiny app in R

I am trying to add some text to the current title in shiny app. So
I used
paste("Hello Shiny!", textOutput("text1"))
in the title but unfortunately something is wrong. The output and R code are as follows:
library(shiny)
ui <- fluidPage(
titlePanel(
title=
#textOutput("text1")#
paste("Hello Shiny!", textOutput("text1")))
)
server <- function(input, output) {
output$text1<-renderText({"Title changed dynamically"})
}
shinyApp(ui = ui, server = server)
What's wrong with the code?
Paste the data on the server side -
library(shiny)
ui <- fluidPage(
titlePanel(
title= textOutput("text1"))
)
server <- function(input, output) {
output$text1<-renderText({
dynamic_title <- "Title changed dynamically"
paste("Hello Shiny!", dynamic_title)})
}
shinyApp(ui = ui, server = server)

Shiny - Go to another section in same page

I am using below code and try to do below action.
Click on action button to go to next table. How can I do this?
library(shiny)
library(shinydashboard)
library(ggplot2)
ui <- fluidPage(
fluidRow(box(
actionButton("btn1", "Go to Next Table"),
tableOutput("tbl1")
)),
fluidRow(box(
tableOutput("tbl2")
))
)
server <- function(input, output, session) {
output$tbl1 <- renderTable(mtcars)
output$tbl2 <- renderTable(mpg)
}
shinyApp(ui, server)
Here's one solution:
library(shiny)
library(shinydashboard)
library(ggplot2)
ui <- fluidPage(
fluidRow(box(
actionButton("btn1", "Go to Next Table",
onclick = "location.href='#table2';"),
tableOutput("tbl1")
)),
fluidRow(id = "table2", box(
tableOutput("tbl2")
))
)
server <- function(input, output, session) {
output$tbl1 <- renderTable(mtcars)
output$tbl2 <- renderTable(mpg)
}
shinyApp(ui, server)
I've added a unique ID to the location in the UI - here the 2nd fluidRow, then added an onclick javascript function to the actionButton also in the UI. No server function means all the work is done by the user's browser which is handy sometimes.
You can add infinite complexity to the Javascript here to customise it to fit your needs.

How to use R hands on table with shiny App

I am trying to create a shiny app using rhandsontable. I am using the code below:
library(shiny)
library(rhandsontable)
library(ggplot2)
ui = fluidPage(
rHandsontableOutput ('table'))
server = function(input, output, session) {
output$table < renderRHandsontable(mpg)}
shinyApp(ui, server)
When i run the code i get error "rendering objects from shinyoutput not allowed"
Any idea why its happening ?
This should do:
library(shiny)
library(rhandsontable)
library(ggplot2)
ui <- fluidPage(
mainPanel(
rHandsontableOutput('table')
)
)
server = function(input, output, session) {
output$table <- renderRHandsontable({
rhandsontable(head(mpg))
})
}
shinyApp(ui, server)

How can you pass a url to an iframe via textInput() in r shiny?

I need to embed a webpage reached through a URL inputted by the user.
I found this script but I can't make the iframe depend on a textInput() containing a URL. This example fails and I am not sure why.
library(shiny)
ui <- fluidPage(
textInput('url','url',value = "www.google.com"),
uiOutput('o')
)
server <- function(input, output, session) {
output$o = renderUI({
tags$iframe(src=input$url)
})
}
shinyApp(ui, server)
You can do like this:
library(shiny)
ui <- fluidPage(titlePanel("Getting Iframe"),
sidebarLayout(
sidebarPanel(
textInput("url", label = "Enter url"),
actionButton("go", "Go")
),
mainPanel(
htmlOutput("frame")
)
))
server <- function(input, output) {
output$frame <- renderUI({
validate(need(input$go, message=FALSE))
tags$iframe(src=isolate(input$url), height=600, width=535)
})
}
shinyApp(ui, server)

LaTeX formula in Shiny panel

I want to display a -LaTeX formated- formula in a Shiny panel, but I can't find a way to combine textOutput with withMathJax. I tried the following but it didn't work. Any help would be gratefully appreciated.
--ui.r
...
tabPanel("Diagnostics", h4(textOutput("diagTitle")),
withMathJax(textOutput("formula")),
),
...
--server.r
...
output$formula <- renderText({
print(paste0("Use this formula: $$\\hat{A}_{\\small{\\textrm{M€}}} =", my_calculated_value,"$$"))
})
...
Use uiOutput on the UI side and renderUI on the server side for dynamic content.
ui <- fluidPage(
withMathJax(),
tabPanel(
title = "Diagnostics",
h4(textOutput("diagTitle")),
uiOutput("formula")
)
)
server <- function(input, output, session){
output$formula <- renderUI({
my_calculated_value <- 5
withMathJax(paste0("Use this formula: $$\\hat{A}_{\\small{\\textrm{M€}}} =", my_calculated_value,"$$"))
})
}
shinyApp(ui, server)
More examples: http://shiny.leg.ufpr.br/daniel/019-mathjax/
ui.R
tabPanel("Diagnostics", h4(textOutput("diagTitle")),
withMathJax(uiOutput("formula")),
)
server.R
output$formula <- renderUI({
return(HTML(paste0("<p>", "Use this formula: $$\\hat{A}_{\\small{\\textrm{M€}}} =", my_calculated_value,"$$","</p>")))
})
How about using renderPrint()?
Minimal working example:
library(shiny)
server <- function(input, output, session) {
output$formula <- renderPrint({
print(paste0("Use this formula: $$\\hat{A}_{\\small{\\textrm{M€}}} =", 1,"$$"))
})
}
ui <- fluidPage(
titlePanel("Hello Shiny!"),
sidebarLayout(
sidebarPanel(
),
mainPanel(
withMathJax(textOutput("formula"))
)
)
)
shinyApp(ui = ui, server = server)
EDIT:
To me it looks like this:

Resources