Actionbutton and observeEvent not working (R shiny) - r

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!

Related

Perform multiple actions on actioButton Shiny

I am pretty new to Shiny and dealing with the following problem, upon pressing an actionButton in shiny, I want it to do multiple calculations. I use the handler of observeEvent.
An example:
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(`
actionButton("calc","calculate stuff")),
mainPanel(
textOutput("result")
)
)
)
server <- function(input,output){
observeEvent(input$calc, {output$result <- renderText({"only this is not enough"}) })
}
shinyApp(ui,server')`
Now what I would want is where the output$result is made in the server-observeEvent, I would like to perform additional tasks, say assign a variable a <- 12, calculate B4 <- input$ID1*inputID2 etc.
This can not be hard I imagine.. but I am just not getting there.
kind regards,
Pieter
You can use isolate, see this example:
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
numericInput(inputId = 'x', label = 'Select a value for x', value = 1),
actionButton( "calc", "calculate stuff" )
),
mainPanel(
textOutput("result")
)
)
)
server <- function(input, output) {
output$result <- renderText({
input$calc
isolate({
y<- input$x *2
paste("The result is:", y)
})
})
}
shinyApp(ui, 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:

widget example of actionbutton in shiny doesn't work

I'm playing around with shiny, and can't get the simplest action button example to work.
First example found here:http://shiny.rstudio.com/gallery/widgets-gallery.html
Below are the code, which is a copy paste from the website.
#ui.R
shinyUI(fluidPage(
# Copy the line below to make an action button
actionButton("action", label = "Action"),
hr(),
fluidRow(column(2, verbatimTextOutput("value")))
))
#server.R
shinyServer(function(input, output) {
# You can access the value of the widget with input$action, e.g.
output$value <- renderPrint({ input$action })
})
Mine looks like:
http://imgur.com/t0Vx6Wr
edit:
The issue is that it also prints out some class information
Thanks
Use renderText rather then renderPrint if you want it to look like it does on the shiny website:
require(shiny)
runApp(list(ui = fluidPage(
actionButton("action", label = "Action"),
hr(),
fluidRow(column(2, verbatimTextOutput("value")))
)
, server = function(input, output) {
output$value <- renderText({ input$action })
})
)

Resources