How can I solve this bug in R shiny? - r

i want to run an r shiny app using the typical command runApp(). I made sure that i am in the correct working directory and that the shiny package is loaded. when running the app, i get this error message here:
Error in p(class = "text-right", textOutput(outputId = "callsCount")) :
unused argument (class = "text-right")```
What can i do to solve that error ?
Thanks

This is the closest I got at reproducing your error:
library(shiny)
ui <- fluidPage(
br(),
uiOutput(outputId = "myUI")
)
server <- function(input, output, session) {
p <- reactive(3)
output$myUI <- renderUI({
p(class = "text-right", textOutput(outputId = "callsCount"))
})
}
shinyApp(ui, server)
Warning: Error in p: unused arguments (class = "text-right", textOutput(outputId = "callsCount"))
There are two fixes you need to make:
fix1:
It seems you have a reactive expression named p. Change it's name to something else. An alternative is to specify explicitly from which environment shiny should get the function p():
output$myUI <- renderUI({
shiny::p(class = "text-right", textOutput(outputId = "callsCount"))
})
fix2:
Replace textOutput(outputId = "callsCount") with the text you want to render.
For example:
output$myUI <- renderUI({
shiny::p(class = "text-right", "This is the text to render")
})
final ui and server:
library(shiny)
ui <- fluidPage(
br(),
uiOutput(outputId = "myUI")
)
server <- function(input, output, session) {
p <- reactive(3)
output$myUI <- renderUI({
shiny::p(class = "text-right", "This is the text to render")
})
}
shinyApp(ui, server)

Related

Rendering Latex using MathJax in Shiny

I am trying to create a dynamic Shiny app that uses the yacas Computer Algebra System to process entered functions. As a first step, I want the UI to confirm what it understands has been typed in.
However, the following Shiny code is not displaying the entered function in Latex format.
library(shiny)
library(Ryacas) # for the TeXForm command
library(Ryacas0)
library(mathjaxr) # for rendering Latex expressions in Shiny
ui <- fluidPage(
sidebarPanel(
textInput(
inputId = "ui_function",
label = 'f(x) = ',
value = "x^2",
placeholder = "Enter function here"),
),
mainPanel(
uiOutput("entered")
)
)
server <- function(input, output) {
output$entered = renderUI({
withMathJax(
helpText(yac_str(paste0("TeXForm(",
input$ui_function,
")")
)
)
)
})
} # end server function
shinyApp(ui = ui, server = server)
When I remove the 'withMathJax' commands from the above code, it behaves exactly the same way, so it's as if the 'withMathJax' command is not having any effect on the output.
By way of a simple example, I'm looking for the user to enter 'x^2' and it should displays
x²
I welcome any help that anyone can offer.
I'm running this on latest RStudio 2022.02.1 Build 461, with R4.1.3, Shiny 1.7.1 and MathJax 1.6-0
You can do as follows:
library(shiny)
library(Ryacas) # for the TeXForm command
ui <- fluidPage(
sidebarPanel(
textInput(
inputId = "ui_function",
label = 'f(x) = ',
value = "x^2",
placeholder = "Enter function here"),
),
mainPanel(
helpblock(withMathJax(uiOutput("entered", inline = TRUE)))
)
)
server <- function(input, output) {
output$entered = renderUI({
paste0(
"\\(",
yac_str(paste0("TeXForm(", input$ui_function, ")")),
"\\)"
)
})
} # end server function
shinyApp(ui = ui, server = server)
The mathjaxr package is not for Shiny, it is used for help files (Rd).
Following on from Stephane's suggestion, I re-looked at my code and this version now works, as intended:
library(shiny)
library(Ryacas)
library(Ryacas0)
library(mathjaxr) # for rendering Latex expressions in Shiny
ui <- fluidPage(
sidebarPanel(
textInput(
inputId = "ui_function",
label = 'f(x) = ',
value = "x^2",
placeholder = "Enter function here"),
),
mainPanel(
withMathJax(uiOutput("entered"))
)
)
server <- function(input, output) {
output$entered = renderUI({
withMathJax(
helpText(
paste0(
"\\(",
yac_str(paste0("TeXForm(", input$ui_function, ")")),
"\\)"
)
)
)
})
} # end server function
shinyApp(ui = ui, server = server)
The inclusion of withMathJax inside mainPanel in the ui seemed to make the difference. It also seems that the "\\(" strings that are concatenated to the string inside the server are critical to its success.

How to generate output of var_label()?

I'm trying to get the label output in shiny using textOutput function with var_label from the labelled package. I tried a couple of things but I'm not able to view the label in the output. The error I'm getting is Error in var_label.data.frame: object 'var1' not found.
Code:
library(shiny)
library(labelled)
library(haven)
dat <- read_spss("http://staff.bath.ac.uk/pssiw/stats2/SAQ.sav")
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput("var1", "Frequency Table", choices = names(dat), selected = NULL)
),
mainPanel(
verbatimTextOutput("name"),
textOutput("label")
)
)
)
server <- function(input, output) {
output$name <- renderPrint({
input$var1
})
output$label <- renderText({
var_label(input$var1)
})
}
shinyApp(ui = ui, server = server)
The issue is that input$var1 is the name of the variable. It does not contain any information about the variable in your dataset. To get the label associated with the variable called input$var1 use var_label(dat[[input$var1]]).

Invalid argument type with verbatimTextOutput in shiny

I am having a very difficult time getting to understand how Shiny modules work. I am building an app using a Golem template (https://github.com/ThinkR-open/golem).
Edit I realized that my error was not tied to any modules, so generated a minimal example. To review the old code, look in the edit history
library(shiny)
app_ui <- function() {
fluidPage(
textInput(inputId = "test", label = "Labley", value = "val"),
verbatimTextOutput(outputId = "out", placeholder = "None")
)
}
app_server <- function(input, output, session) {
output$out <- renderText(expr = input$test)
}
shinyApp(ui = app_ui, server = app_server)
This causes:
Error in !: invalid argument type
How do I get UI to print my text input?
#KasperThystrupKarstensen the problem here is, that verbatimTextOutput's placeholder argument requires a boolean input (see usage in ?verbatimTextOutput). However, you passed a string.
This works:
library(shiny)
app_ui <- fluidPage(
textInput(
inputId = "test",
label = "Labley",
value = "val"
),
verbatimTextOutput(outputId = "out", placeholder = FALSE)
)
app_server <- function(input, output, session) {
output$out <- renderText(expr = input$test)
}
shinyApp(ui = app_ui, server = app_server)
?verbatimTextOutput:
placeholder: if the output is empty or NULL, should an empty rectangle
be displayed to serve as a placeholder? (does not affect behavior when
the the output in nonempty)

How to use shiny:renderUI and shiny:UiOutput to produce conditional outputs with shiny modules

This is a slight extension of an earlier question.
Note: This solution now works after a typo was identified in the code and corrected. I hope this is a useful pattern that others can use too.
I would like different output types to be displayed via uiOutput, but in a modular framework.
What I have so far is:
module_ui <- function(id){
ns <- NS(id)
tagList(
selectInput(ns("output_type"),
label = "Select type of output",
selected = "table",
choices = c("table", "barplot", "graph")
),
uiOutput(ns("diff_outputs"))
)
}
module_server <- function(input, output, session){
ns <- session$ns
output$table <- renderTable({head(women)})
output$barplot <- renderPlot({barplot(women$height)})
output$scatterplot <- renderPlot({plot(women$height ~ women$weight)})
output_selected <- reactive({input$output_type})
output$diff_outputs <- renderUI({
if (is.null(output_selected()))
return()
switch(
output_selected(),
"table" = tableOutput(ns("table")),
"barplot" = plotOutput(ns("barplot")),
"graph" = plotOutput(ns("scatterplot"))
)
})
}
ui <- fluidPage(
titlePanel("Dynamically generated user interface components"),
fluidRow(
module_ui("module")
)
)
server <- function(input, output){
callModule(module_server, "module")
}
shinyApp(ui = ui, server = server)
The problem is that uiOutput is currently blank.
If a solution were found for this kind of problem, it would be very helpful indeed.
I think only very minor modifications are needed, but I am still new to using shiny modules.
It works but you have a typo: taglist should be tagList.

R shiny - stop() in eventReactive without error message in console

I just new in Shiny, and i have problem. i have a event reactive and the stop function inside. when I run my code(no checkbox and do click button), the shiny is work well. but in console display the error message "eventReactiveHandler". do you have a solution for my problem? i want to no error message in my console.
and i not expect the solution is
opt <- options(show.error.messages=FALSE)
on.exit(options(opt))
because the error will not display in my all code, i want just specifically in this error.
thank you... this is the code...
rm(list = ls())
library(shiny)
library(shinyBS)
var.x<-reactiveValues()
shinyApp(
ui =
fluidPage(
sidebarLayout(
sidebarPanel(
checkboxGroupInput("indepvar","Independent Variable",
choices = c("1"=1,"2"=2)),
actionButton("tabBut", "View Table")
),
mainPanel(
uiOutput("coba"),
uiOutput("popup4")
)
)
),
server =
function(input, output, session) {
output$coba <- renderUI({
gendata()
indep<-NULL
for(i in 1:length(var.x)){
indep <- paste(indep,var.x[i],",")
}
list(
renderText(indep)
)
})
gendata<- eventReactive(input$tabBut,{
if(is.null(input$indepvar)){
stop()
}
var.x<<- input$indepvar
})
output$popup4 <- renderUI({
if(!is.null(input$indepvar))return()
list(
bsModal("modalExample4", "Peringatan", "tabBut", size = "small",wellPanel(
"Anda belum memilih independent variabel..."
))
)
})
}
)
I wouldn't advise suppressing error messages, as there are in there for you, I suggest you look into validate and need in shiny, you can go read validation article
To quickfix you issue you can just return NULL
rm(list = ls())
library(shiny)
library(shinyBS)
var.x<-reactiveValues()
shinyApp(
ui =
fluidPage(
sidebarLayout(
sidebarPanel(
checkboxGroupInput("indepvar","Independent Variable",
choices = c("1"=1,"2"=2)),
actionButton("tabBut", "View Table")
),
mainPanel(
uiOutput("coba")
)
)
),
server =
function(input, output, session) {
output$coba <- renderUI({
gendata()
indep<-NULL
for(i in 1:length(var.x)){
indep <- paste(indep,var.x[i],",")
}
list(
renderText(indep)
)
})
gendata<- eventReactive(input$tabBut,{
if(is.null(input$indepvar)){
var.x <<- NULL
return(NULL)
stop()
}
var.x<<- input$indepvar
})
}
)
You need to do two things as per the code below:
Make sure that gendata returns nothing when there are no independent variables selected (see lines 37-40). This stops your original error message
Make sure that output$coba is not evaluated when gendata has no value (see line 25)
Hope this helps,
John
rm(list = ls())
library(shiny)
library(shinyBS)
var.x<-reactiveValues()
shinyApp(
ui =
fluidPage(
sidebarLayout(
sidebarPanel(
checkboxGroupInput("indepvar","Independent Variable",
choices = c("1"=1,"2"=2)),
actionButton("tabBut", "View Table")
),
mainPanel(
uiOutput("coba"),
uiOutput("popup4")
)
)
),
server =
function(input, output, session) {
output$coba <- renderUI({
req(gendata())
indep<-NULL
for(i in 1:length(var.x)){
indep <- paste(indep,var.x[i],",")
}
list(
renderText(indep)
)
})
gendata<- eventReactive(input$tabBut,{
if(is.null(input$indepvar)) {
var.x <<- NULL
return()
}
var.x<<- input$indepvar
})
output$popup4 <- renderUI({
if(!is.null(input$indepvar))return()
list(
bsModal("modalExample4", "Peringatan", "tabBut", size = "small",wellPanel(
"Anda belum memilih independent variabel..."
))
)
})
}
)

Resources