Render output of print(data.tree) to the UI in shiny - r

I have output of data.tree package as follows in the console :
print(acme, "cost", "p")
#Out of Data.tree print JPEG
I want to render the same output structure in the shiny UI.
I have tried paste(console.output(print(acme, "cost", "p")),collapse = ""), but the output structure is not same.
ui <- fluidPage(
sliderInput(inputId = "slider",
label = "My number",
min = 300,
max = 19000,
value = 5000),
htmlOutput("mytext")
)
server <- function(input, output) {
output$mytext <- renderText({
paste(capture.output(print(acme, "cost", "p")),collapse = "<br/>")
})
}
shinyApp(ui = ui, server = server)
Please suggest me any render functions , that will print the output exactly like the above in the Shiny UI.

TL:DR; Replace renderText() with renderPrint() in server and htmlOutput() with verbatimTextOutput() in ui.
The issue is with how renderText() works. It concatenates all the text input with cat() which will fail when using against the data.tree object, which is a list. You should use renderPrint() to capture the output of your print statement. No capture.output() required. The renderPrint() will not work with various output methods, the safest one to use is verbatimTextOutput(). I base that recommendation from experimentation, not actual documentation research. htmlOutput might work but I an not sure.
Updated code:
ui <- fluidPage(
sliderInput(inputId = "slider",
label = "My number",
min = 300,
max = 19000,
value = 5000),
verbatimTextOutput("mytext")
)
server <- function(input, output) {
output$mytext <- renderPrint({
print(acme, "cost", "p"))
})
}
shinyApp(ui = ui, server = server)

Related

MathJax output dependent on reactive SelectInput choices

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

how to display value of text box number using renderUI shiny app R

I want to display the value of the textbox number be displayed when typing
and that what I am trying to do
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
h1("Enter One Number"),
numericInput("number","enter a number", "" , min = 1 , max = 100),
uiOutput("uu")
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$uu <- renderUI({"you are typing " input$number })
}
You need to handle the string. It is not possible to just concatenate them inside a renderUI. In order to combine the strings use paste, sprintf or any appropriate string function.
library(shiny)
ui <- fluidPage(
h1("Enter One Number"),
numericInput("number","enter a number", "" , min = 1 , max = 100),
uiOutput("uu"))
server <- function(input, output, session) {
output$uu <- renderUI({paste("you are typing", input$number) })
}
shinyApp(ui, server)
You can't simply combine a text and an object in R, but you can use paste for such situations.
library(shiny)
ui <- fluidPage(
# Application title
h1("Enter One Number"),
numericInput("number","enter a number", "" , min = 1 , max = 100),
uiOutput("uu")
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$uu <- renderUI({paste0("you are typing ", input$number) })
}
shinyApp(ui = ui, server = server)

Reactive function not being found within app

I have a fairly involved app. When I call a particular eventReactive function, let's call it function A, within a reactive expression, I get an error that function A cannot be found.
I'm unable to reproduce the exact app because it is proprietary, but I did create a dummy app that simulates the setup I have. I realize that there must be some difference between the dummy app and what I actually have, but I can't figure it out. The function in question is there, so I fundamentally don't understand why it's not being found.
library(shiny)
ui <- fluidPage(
sliderInput(inputId = "num",
label = "Choose a number",
value = 25, min = 1, max = 100),
actionButton(inputId = "action",
label = "Update"),
plotOutput("hist"),
verbatimTextOutput("stats")
)
server <- function(input, output) {
data <- eventReactive(input$action, {
input$num*2
})
data2 <- reactive({
data()*2
})
output$stats <- renderPrint({
data2()
})
}
shinyApp(ui = ui, server = 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.

R Shiny - Server rendered checkbox won't check

I'm trying to use awesomeCheckbox from the shinyWidgets package and am running into an issue where I can't check/uncheck the box that a server rendered.
library(shiny)
library(shinyWidgets)
ui <- fluidPage(
awesomeCheckbox(inputId = "checkboxA",
label = "A checkbox",
value = TRUE),
uiOutput("checkboxB"),
uiOutput("FS1")
)
server <- function(input, output) {
output$checkboxB <- renderUI({
awesomeCheckbox(inputId = "checkboxB",
label = "B checkbox",
value = TRUE)
})
output[[paste0("FS", 1)]] <- renderUI({
awesomeCheckbox(inputId = paste0("FS", 1),label = "FS", value = FALSE)
})
}
shinyApp(ui= ui, server=server)
I need this piece of code as part of a larger Shiny App where checkboxes are generated dynamically in the server (hence the weird paste0 naming).
I've checked my version of R and have tried using both Chrome and Safari but can't seem to get the FS checkbox to check/uncheck. I also can't seem to find anything out of the ordinary when I use "Inspect element" in my browser.
You have outputids checkboxB and FS1 respectively already rendered once, yet you create other components with the same names, hence they wont work, change the names as you cant have duplicate divs like so:
library(shiny)
library(shinyWidgets)
ui <- fluidPage(
awesomeCheckbox(inputId = "checkboxA",label = "A checkbox",value = TRUE),
uiOutput("checkboxB"),
uiOutput("FS1")
)
server <- function(input, output) {
output$checkboxB <- renderUI({
awesomeCheckbox(inputId = "checkboxBx",label = "B checkbox", value = TRUE)
})
output[[paste0("FS", 1)]] <- renderUI({
awesomeCheckbox(inputId = paste0("FSx", 1),label = "FS", value = FALSE)
})
}
shinyApp(ui= ui, server=server)

Resources