R Shiny: Using numeric output variable as the initial value of slider - r

My shiny app looks like this:
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(title = "Basic dashboard"),
dashboardSidebar(),
dashboardBody(
fluidRow(
box(plotOutput("plot1", height = 250)),
box(textInput("my_text", # supposed to be a numeric input
"Text input:"),
title = "Controls",
sliderInput("slider",
"Number of observations:",
min = 1, max = 100,
value = 50 # Want it to be output$my_init
)
)
)
)
)
server <- function(input, output) {
set.seed(122)
histdata <- rnorm(500)
output$plot1 <- renderPlot({
data <- histdata[seq_len(input$slider)]
hist(data)
})
# My attempt
# output$my_init <- input$my_text + 28
}
shinyApp(ui, server)
I want to set the initial value of the slider as output$my_init, a numeric variable that will be the result of operating some input variables. I tried using renderPrint, but the output is not numeric.
Thanks in advance.

I piggybacked on #stefan's comments and came up with this answer:
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(title = "Basic dashboard"),
dashboardSidebar(),
dashboardBody(
fluidRow(
box(plotOutput("plot1", height = 250)),
box(numericInput("my_num", "Numeric input:", value = 50),
title = "Controls",
sliderInput("slider",
"Number of observations:",
min = 1, max = 100,
value = 50 # Want it to be output$my_init
)
)
)
)
)
server <- function(input, output) {
set.seed(122)
histdata <- rnorm(500)
output$plot1 <- renderPlot({
data <- histdata[seq_len(input$slider)]
hist(data)
})
# My attempt
observeEvent(input$my_num, {
updateSliderInput(inputId = "slider", value = input$my_num)
})
}
shinyApp(ui, server)
The idea is to use observeEvent() to trigger updateSliderInput() (update slider input widget) and update the value parameter of sliderInput()

Related

How to add rows to R Shiny table

I am trying to build a form with R Shiny which will be used to populate a table once the action button at the end of the form is clicked. What I have not been able to figure out is how to pick up the data in the form and add it to a new row in the table. Right now, it just keeps updating the first row with whatever is in the form. I have reproduced a simple version of the code here:
#ui.r
library(shiny)
shinyUI(fluidPage(
# Application title
titlePanel("Test App"),
sidebarPanel(
numericInput("x", "Enter Value of X", 1),
numericInput("y", "Enter Value of Y", 1),
actionButton("add_data", "Add Data", width="100%")
),
mainPanel(
tableOutput("xy_Table")
)
)
)
#server.R
library(shiny)
library(tidyverse)
shinyServer(function(input, output) {
x <- vector("numeric")
y <- vector("numeric")
xyTable <- tibble(x, y)
e <- reactive(input$x)
f <- reactive(input$y)
eventReactive(input$add_data, {
xyTable %>% add_row(x=e(), y=f())
})
output$xy_Table <- renderTable({
xyTable
})
})
Thanks a lot for any help.
You need to use a reactive xyTable in order for the output to update. Also,
append the rows inside an observer rather than a reactive expression, and make sure to save the updated reactive value:
library(shiny)
library(tidyverse)
ui <- fluidPage(
sidebarPanel(
numericInput("x", "Enter Value of X", 1),
numericInput("y", "Enter Value of Y", 1),
actionButton("add_data", "Add Data", width = "100%")
),
mainPanel(
tableOutput("xy_Table")
)
)
server <- function(input, output, session) {
xyTable <- reactiveVal(
tibble(x = numeric(), y = numeric())
)
observeEvent(input$add_data, {
xyTable() %>%
add_row(
x = input$x,
y = input$y,
) %>%
xyTable()
})
output$xy_Table <- renderTable(xyTable())
}
shinyApp(ui, server)
Try this:
library(shiny)
library(tidyverse)
#ui.r
ui <- fluidPage(
# Application title
titlePanel("Test App"),
sidebarPanel(
numericInput("x", "Enter Value of X", 1),
numericInput("y", "Enter Value of Y", 1),
actionButton("add_data", "Add Data", width = "100%")
),
mainPanel(
tableOutput("xy_Table")
)
)
#server.R
server <- function(input, output) {
xyTable <- reactiveValues(
table1 = tibble(x = numeric(), y = numeric())
)
# what happens when `add_data` is clicked?
observeEvent(input$add_data, {
xyTable$table1 <- xyTable$table1 |>
add_row(x = input$x, y = input$y)
})
output$xy_Table <- renderTable({
xyTable$table1
})
}
shinyApp(ui, server)
#ui.r
library(shiny)
ui <- shinyUI(fluidPage(
# Application title
titlePanel("Test App"),
sidebarPanel(
numericInput("x", "Enter Value of X", 1),
numericInput("y", "Enter Value of Y", 1),
actionButton("add_data", "Add Data", width="100%")
),
mainPanel(
tableOutput("xy_Table")
)
)
)
#server.R
library(shiny)
library(tidyverse)
server <- shinyServer(function(input, output) {
x <- vector("numeric")
y <- vector("numeric")
xyTable <- reactiveValues()
xyTable$df <- tibble(x, y)
e <- reactive(input$x)
f <- reactive(input$y)
observeEvent(input$add_data, {
xyTable$df <- xyTable$df %>% add_row(x=e(), y=f())
})
output$xy_Table <- renderTable({
xyTable$df
})
})
shinyApp(ui,server)

How to use re-CAPTCHA with shinydashboard?

I am trying to use shinyCAPTCHA package with shinydashboard, but it is not working correctly. What is the correct way to use it?
library(shiny)
library(shinydashboard)
library(shinyCAPTCHA)
ui <- dashboardPage(
dashboardHeader(title = "Basic dashboard"),
dashboardSidebar(),
dashboardBody(recaptchaUI("test", sitekey = "6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI"),
uiOutput(outputId = "captcha"))
)
server <- function(input, output) {
result <- callModule(recaptcha, "test", secret = "6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe")
output$captcha <- renderUI({
req(result()$success)
output$plot1 <- renderPlot({
set.seed(122);histdata <- rnorm(500)
hist(histdata[seq_len(input$slider)], main = "Histrogram", xlab = "x") })
fluidRow(
box(plotOutput("plot1", height = 250)),
box(title = "Controls",sliderInput("slider", "Number of observations:", 1, 100, 50)))
})
}
shinyApp(ui, server)
This is less bad I think
library(shiny)
library(shinydashboard)
library(shinyCAPTCHA)
ui <- dashboardPage(
dashboardHeader(title = "Basic dashboard"),
dashboardSidebar(),
dashboardBody(recaptchaUI("test", sitekey = "6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI"),
uiOutput(outputId = "captcha"))
)
server <- function(input, output) {
result <- callModule(recaptcha, "test", secret = "6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe")
output$captcha <- renderUI({
req(result()$success)
output$plot1 <- renderPlot({
set.seed(122);histdata <- rnorm(500)
req(reactive(input$slider)())
hist(histdata[seq_len(reactive(input$slider)())], main = "Histrogram", xlab = "x") })
fluidRow(
box(plotOutput("plot1", height = 250)),
box(title = "Controls",sliderInput("slider", "Number of observations:", 1, 100, 50)))
})
}
shinyApp(ui, server)

Disable the vertical scroll bar in shiny dashboard

Let's say that I have a shiny dashboard uses by default a vertical scroll bar because of a large plot in the right side but for some reason I do not want it there even if the plot is not displayed as a whole. I do not want to reduce the plot height as well.Can this be done?
## app.R ##
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(title = "Basic dashboard"),
dashboardSidebar(),
dashboardBody(
# Boxes need to be put in a row (or column)
fluidRow(
box(plotOutput("plot1", height = 850)),
box(
title = "Controls",
sliderInput("slider", "Number of observations:", 1, 100, 50)
)
)
)
)
server <- function(input, output) {
set.seed(122)
histdata <- rnorm(500)
output$plot1 <- renderPlot({
data <- histdata[seq_len(input$slider)]
hist(data)
})
}
shinyApp(ui, server)
The following css should do it : body {overflow-y: hidden;}
## app.R ##
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(title = "Basic dashboard"),
dashboardSidebar(),
dashboardBody(
tags$head(
tags$style(
"body {overflow-y: hidden;}"
)
),
# Boxes need to be put in a row (or column)
fluidRow(
box(plotOutput("plot1", height = 850)),
box(
title = "Controls",
sliderInput("slider", "Number of observations:", 1, 100, 50)
)
)
)
)
server <- function(input, output) {
set.seed(122)
histdata <- rnorm(500)
output$plot1 <- renderPlot({
data <- histdata[seq_len(input$slider)]
hist(data)
})
}
shinyApp(ui, server)

User input in filtering dataframe in rshiny

I'm using R shiny Dashboard for data visualization in my case i wanted to insert user input into filtering data frame in r
library(shiny)
library(shinydashboard)
ui <-
dashboardPage(
dashboardHeader(title = "Basic dashboard"),
dashboardSidebar(
sidebarMenu(
)
),
dashboardBody(
# Boxes need to be put in a row (or column)
fluidRow(
box(plotOutput("plot1", height = 250)),
box(
title = "",
sliderInput("slider", "Number of Breaks:", 1, 180, 50)
),
box(
selectInput('BILLING_CENTRE', 'Select Billing Center', names(dfList))
)
)
)
)
server <-
function(input, output) {
d <- read.csv('Events_for_Jan_suspensions.csv')
dfList <- split(d, d$BILLING_CENTRE)
abc <- reactive({input$BILLING_CENTRE})
if (abc == "AD"){
histdata <- dfList$AD$SU_TO_OK_DURATION
output$plot1 <- renderPlot({
data <- histdata[seq_len(input$slider)]
hist(data)
})
}
}
shinyApp(ui, server)

r shiny: access input fields in UI

I'm trying to access an input field in mainPanel from the sidebarPanel, but I couldn't succeed.
Code:
shinyUI(pageWithSidebar{
sidebarPanel(
sliderInput("x", "X", min = 10, max = 100, value = 50)
),
mainPanel(
#this is where I wanna use the input from the sliderInput
#I tried input.x, input$x, paste(input.x)
)
})
Where seems to be the problem? Or isn't possible to use the input from the sidebarPanel in the mainPanel?
You can only use the inputs in the server side.
For example :
library(shiny)
runApp(list(
ui = pageWithSidebar(
headerPanel("test"),
sidebarPanel(
sliderInput("x", "X", min = 10, max = 100, value = 50)
),
mainPanel(
verbatimTextOutput("value")
)
),
server = function(input, output, session) {
output$value <- renderPrint({
input$x
})
}
))
EDIT ::
Dynamically set the dimensions of the plot.
Use renderUi to render a plot output using the values of your inputs.
library(shiny)
runApp(list(
ui = pageWithSidebar(
headerPanel("Test"),
sidebarPanel(
sliderInput("width", "Plot Width (%)", min = 0, max = 100, value = 100),
sliderInput("height", "Plot Height (px)", min = 0, max = 400, value = 400)
),
mainPanel(
uiOutput("ui")
)
),
server = function(input, output, session) {
output$ui <- renderUI({
plotOutput("plot", width = paste0(input$width, "%"), height = paste0(input$height, "px"))
})
output$plot <- renderPlot({
plot(1:10)
})
}
))

Resources