LaTeX formula in Shiny panel - r

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:

Related

$ operator is invalid for atomic vectors in shiny R

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)

filtering based on user inputs in Shiny is not work

User new to Shiny here. It is a very easy mode. But it's not work and it doesn't show any errors.
The same result when I use subset function.
Here's my code:
library(shiny)
library(dplyr)
# Define UI for application that draws a histogram
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput("variable", "Variable:",colnames(iris))
),
mainPanel(
tableOutput("table")
)
)
)
server <- function(input, output) {
output$table <- renderTable({
iris %>% dplyr::filter(input$variable>=5)
})
}
# Run the application
shinyApp(ui = ui, server = server)
input$variable is a character you can use .data pronoun here to use it as column value.
library(dplyr)
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
varSelectInput("variable", "Variable:",iris)
),
mainPanel(
tableOutput("table")
)
)
)
server <- function(input, output) {
output$table <- renderTable({
iris %>% dplyr::filter(.data[[input$variable]]>=5)
})
}
# Run the application
shinyApp(ui = ui, server = server)

Actionbutton and observeEvent not working (R shiny)

Can somebody check the code below for me and tell me what's wrong?
shinyUI(fluidPage(
sidebarLayout(
sidebarPanel(
sliderInput("Mean",
label="Drag to select the mean of the normal distribution",
min=0,max=5,value=0),
actionButton("show","Go")
),
mainPanel(
h3("The number is"),
textOutput('number')
)
)
))
shinyServer(function(input, output) {
#observeEvent(input$show, {
#output$number <- round(rnorm(as.numeric(input$mu)), 2)
#})
output$number <- eventReactive(input$show, {
round(rnorm(as.numeric(input$mu)), 2)
})
}
)
Just want to have a random number sampled from a normal distribution with a given mean each time I click 'Go'. Neither eventReactive nor observeEvent worked.
I'n not a big fan of having objects inside observeEvent so here is the solution with eventReactive
library(shiny)
ui <- shinyUI(fluidPage(
sidebarLayout(
sidebarPanel(
sliderInput("Mean",label="Drag to select the mean of the normal distribution",min=0,max=5,value=0),
actionButton("show","Go")
),
mainPanel(
h3("The number is"),
textOutput('number')
)
)
))
server <- shinyServer(function(input, output) {
data <- eventReactive(input$show, {
round(rnorm(as.numeric(input$Mean)), 2)
})
output$number <- renderText({
data()
})
}
)
shinyApp(ui, server)
The output of eventReactive is a reactive variable. You will need to use renderText and observeEvent. Also you have not defined input$mu in your ui, I think it should be input$Mean
After implementing the above modifications your server code should be something like this:
server <- shinyServer(function(input, output) {
observeEvent(input$show, {
output$number <- renderText(round(rnorm(as.numeric(input$Mean)), 2))
})
}
)
Hope it helps!

renderUI in R shiny doesn't display

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.

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)

Resources