$ operator is invalid for atomic vectors in shiny R - 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)

Related

How to get all tab names in tabsetpanel in r shiny

Is there any way to retrieve all tab names in a tabsetpanel in Shiny?
for example a code that separately gives the name of the tabs in the tabsets tabs1 and tabs2:
library(shiny)
ui <- fluidPage(
tabsetPanel(id = "tabs1",
tabPanel("Tab1"),
tabPanel("Tab2"),
tabPanel("Tab3"),
tabPanel("Tab4"),
),
tabsetPanel(id = "tabs2",
tabPanel("T1"),
tabPanel("T2"),
tabPanel("T3"),
tabPanel("T4"),
)
)
server <- function(input, output, session) {
}
shinyApp(ui, server)
We can use htmltools::tagQuery to achive this:
library(shiny)
library(htmltools)
ui <- fluidPage(
tabsetPanel(id = "tabs1",
tabPanel("Tab1"),
tabPanel("Tab2"),
tabPanel("Tab3"),
tabPanel("Tab4"),
),
tabsetPanel(id = "tabs2",
tabPanel("T1"),
tabPanel("T2"),
tabPanel("T3"),
tabPanel("T4"),
)
)
tabs1_panel_names <- sapply(tagQuery(ui)$find("#tabs1")$find("a")$selectedTags(), function(x){tagGetAttribute(x, "data-value")})
print(tabs1_panel_names)
tabs2_panel_names <- sapply(tagQuery(ui)$find("#tabs2")$find("a")$selectedTags(), function(x){tagGetAttribute(x, "data-value")})
print(tabs2_panel_names)
server <- function(input, output, session) {}
shinyApp(ui, 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)

conditionalPanel in Shiny not working

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)

Using renderDataTable within renderUi in Shiny

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")
})
}
))

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