MathJax output dependent on reactive SelectInput choices - r

I am having trouble getting Rshiny to do what I want.
I would like the user to select an input from the SelectInput choices and based on that input I would like some Text output that is mathematical notation.
I have tried to implement this with withMathJax(), but I cannot seem to get the code right.
Here is some toy code illustrating what I have already:
######################################
ui <-navbarPage(title = "test",
helpText("Here we select which parameters we want to include in our model"),
selectInput("torchp",
label = h4("Torching Parameters"),
choices = list("One parameter",
"Two parameters" ),
selected = 1),
mainPanel(
textOutput("torchvalue")
)
)
server <- function(input, output) {
withMathJax()
torchp_input <- reactive({
switch(input$torchp,
"One parameter" = '$$q$$',
"Two parameters" = '$$q_m, q_f$$'
)
})
output$torchvalue <- renderText({
paste("You have selected", torchp_input())
})
}
shinyApp(ui = ui, server = server)
###################################
The output I get does not recognise my mathematical notation.
Thanks.

Discovered an answer thanks to help in the comments by #Limey.
The issue was which rendering function I used.
In this instance you should use uiOutput and renderUI as follows:
In the UI put:
uiOutput("torchvalue")
In the server use:
output$torchvalue <- renderUI({ p( withMathJax("you have selected", torchp_input())) })

Related

In shiny app, how to illustrate an example to change reactive and a dependent reactiveValues with only one click event?

Here's my app displaying a greeting with names and showing the number of letters of the name.
I would like to change the name and rv$len on one single click, but I need to click twice to get the rv$len changed to 4. Is there anything I can do to first update the name to pikapika, then manually change rv$len to 4 with a single click?
Thanks in advance!
library(shiny)
ui <- fluidPage(
titlePanel("Thanks in advance for your help"),
sidebarLayout(
sidebarPanel(
textInput("name", "name of the user", value = "TEST"),
actionButton(inputId = "Example", label = "pikapika as an example"),
),
mainPanel(
verbatimTextOutput("greetings")
)
)
)
server <- function(input, output) {
name <- reactive(input$name)
rv <- reactiveValues(len = 4)
# by default it reads the number of characters
observe({
rv$len <- nchar(name())
})
greeting <- reactive(paste("Hello", name(), "! Your name has", rv$len, "letters."))
output$greetings <- renderPrint(greeting())
# I would like to illustrate with a custom case
observeEvent(input$Example, {
updateTextInput(inputId = "name", value = "pikapika")
rv$len <- 4
})
}
# Run the application
shinyApp(ui = ui, server = server)
Thanks from Pikachu

How to Set Values in selectInput R Shiny for a long set of options

I am creating an R Shiny app where I have an extremely long list of options for selectInput. Depending on the option you select, the value is going to change. I know that for a small list of options you can set the values yourself in the server function like so:
server <- function(input, output) {
output$graph <- renderPlot({
player <- switch(input$var,
"LeBron James" = 23,
"Kobe Bryant" = 24,
"DeMar DeRozan" = 10,
"Kyle Lowry" = 7)
plotGraph(player)
})
}
But my list has at least 100 options and it's certainly not clean nor efficient to set the values like this for all 100 options. Is there a way to set the values depending on the option selected without having to do it manually?
Below is my code in my ui function
ui <- fluidPage(
titlePanel(h1("Fantasy Dashboard")),
sidebarLayout(
sidebarPanel(h2("Player Name Goes Here"),
selectInput("playername",
label = "Choose a player",
choices = player_choices,
selected = NULL),
),
mainPanel(plotOutput("graph"))
)
)
The choices will be stored in player_choices. These choices are read from a txt file. And depending on the option selected, the variable player should be set to the corresponding value. Thanks in advance!
Try:
library(shiny)
playernames <- list("Smith","Johnston","Andrew")
shinyApp(
ui = fluidPage(
uiOutput("selectname"),
textOutput("result")
),
server = function(input, output) {
output$selectname <- renderUI( {
selectInput("playername", "Choose player",playernames)})
output$result <- renderText({
paste("You chose", input$playername)
})
}
)
The playernames list can also be reactive and be modified by other inputs.

In R shiny, how can I use data that I generated in an observeEvent (button click) outside of that observeEvent?

Say I do an observeEvent like this where I generate random numbers, and then another to export those numbers to a csv:
observeEvent(input$generateButton, {
u = runif(input$nNumericInput)
})
observeEvent(input$exportButton, {
write.csv(data.frame(u),"randomNumbers.csv", row.names = FALSE)
})
That's basically what I'm trying to do but I'm generating the numbers in a more complex way that I don't want to repeat for each button because of efficieny.
Here's a working example. I'm also showing a table of the data that's produced when you update the numericInput. This should save in your working directory. Note that my_df is generated with an eventReactive, because it should be a reactive value (it should change when we change the numericInput), whereas write.csv is called within an observeEvent, because it's simply triggered by clicking the button (i.e., it's not creating any reactive object). Hope that helps.
library(shiny)
ui <- {
fluidPage(
fluidRow(
numericInput(
inputId = 'num_input',
label = 'Input',
value = 5),
actionButton(
inputId = 'num_input_button',
label = 'Generate df'),
actionButton(
inputId = 'write_data',
label = 'Write to csv')
),
fluidRow(
tableOutput('table')
)
)
}
server <- function(input, output, session) {
my_df <- eventReactive(input$num_input_button, {
runif(input$num_input)
})
observeEvent(input$write_data, {
write.csv(my_df(), 'random_numbers.csv')
})
output$table <- renderTable({
my_df()
})
}
shinyApp(ui, server)

R shiny mapping output to input

I'm building my first shiny app and I've run into a little difficulty which i cant understand
My code wants to take the input from the user - add one then print the output
please ignore the radio buttons for now -
ui <- shinyUI(fluidPage(
titlePanel("alpha"),
sidebarPanel(numericInput("expn",
"Please enter total number of reports received", 1,
min = 0,
max = 1000000
),
radioButtons(inputId = "'QC_Type'", label = "QC Type",
choices = c("Overall", "Solicited", "Spontaneous",
"Clinical Trial","Literature" ),
mainPanel(
textOutput("results"))
))))
server <- function (input, output) {
output$results <- renderText(
{ print(1 +(Input$expn))
}
)
}
shinyApp(ui = ui, server = server)
I'm unable to see any output when I run the code.
Thank you for your time :)
That's because of where your mainPanelis located. It should follow the sidebarPanel. Also, I recommend using as.character() instead of print(), unless you really want to print out the output on the console.
Here's the corrected code:
ui <- shinyUI(fluidPage(
titlePanel("alpha"),
sidebarPanel(
numericInput(
"expn",
"Please enter total number of reports received",
1,
min = 0,
max = 1000000
),
radioButtons(
inputId = "'QC_Type'",
label = "QC Type",
choices = c(
"Overall",
"Solicited",
"Spontaneous",
"Clinical Trial",
"Literature"
)
)
),
mainPanel(textOutput("results"))
))
server <- function (input, output) {
output$results <- renderText({
as.character(1 + (input$expn))
})
}
shinyApp(ui = ui, server = server)
I recommend using good practices when indenting code. It makes things easier to read and to find where braces and parentheses are.

Unchecked boxes using checkboxGroupInput in Shiny

EDIT: My original question asked about checkboxInput(), but I've updated to checkboxGroupInput() to better reflect my problem...
I am trying to get my Shiny app to do one of two things based on the (un)checked status of a checkboxGroupInput.
When the boxes are checked, I can get my code to work. However, I can't figure out how to make unchecking all boxes lead to a unique result.
How do I do this?
This google groups question asked this 4+ years ago, but the response then was that this is simply a bug. I'm assuming this has been addressed since??
Below is a reproducible example.
- In this example, unchecking the box leads to an error reading "Error in if: argument is of length zero."
library(shiny)
ui <- fluidPage(
checkboxGroupInput(inputId = "test.check", label = "", choices = "Uncheck For 2", selected = "Uncheck For 2"),
verbatimTextOutput(outputId = "test")
)
server <- function(input, output) {
output$test <- renderPrint({
if(input$test.check == "Uncheck For 2") {
1
} else {
2
}
})
}
shinyApp(ui = ui, server = server)
Is there perhaps an "if.unchecked" type of function I can use?
I've tried is.null after the else statement with the same result as the above example....
Here's code that works:
library(shiny)
ui <- fluidPage(
checkboxGroupInput(inputId="test.check", label="", choices="Uncheck For 2", selected="Uncheck For 2"),
verbatimTextOutput(outputId = "test")
)
server <- function(input, output) {
output$test <- renderPrint({
if(!is.null(input$test.check)) {
1
} else{
2
}
})
}
shinyApp(ui = ui, server = server)

Resources