How to design customisable sliderInput in R Shiny application? - r

I have a skewed data and would like to design slider with predefined set of values. The key bins for data looks like - "Less than $100K", "$100K - $1M", "$1M - $3M", "$3M - $5M", "More than $5M").
Please, help me to design this kind of selector
sliderInput(
"revenue", "Revenue",
min = 0, max = max_revenue,
value = c(0, max_revenue),
step = 1000, pre = "$", sep = ",")
Thanks!

You can try the following:
library("shiny")
library("shinyWidgets")
target_range <- c('0','100k','1M','3M','5M','Max')
ui <- fluidPage(
br(),
sliderTextInput(
inputId = "revenue",
label = "Revenue:",
choices = target_range,
selected = target_range[c(1,2)]
),
verbatimTextOutput(outputId = "revenue_data")
)
server <- function(input, output, session) {
output$revenue_data <- renderPrint(str(input$revenue))
}
shinyApp(ui = ui, server = server)

Related

How can I graph in Shiny with the data provided by the user?

I am using numericImput to get the data introduced by the user.
I am trying to graph the balistic curve of the proyectil motion described by the input of the user (The user gives a value for intial velocity, angle measure, time of flight and k constant), but I can't solve the mistake :(
Any help would be thanked! :D
Here is my code
library(shiny)
ui <- fluidPage(
titlePanel("Movimiento de Projectiles"),
sidebarLayout(
sidebarPanel(
width = 2,
numericInput(inputId = "v",
label = "Velocidad",
value = " "),
numericInput(inputId = "th",
label = "Angulo",
value = " "),
numericInput(inputId = "tf",
label = "Tiempo",
value = " "),
numericInput(inputId = "k",
label = "Constante k",
value = " ")
),
mainPanel(
plotOutput("posicion")
)
)
)
server <- function(input, output) {
v <- renderPrint({ input$v })
th <- (renderPrint({ input$th}))
t<-renderPrint({ input$tf })
k<-renderPrint({ input$k })
output$posicion <- renderPlot({
# Para x
vx<-function(v,th){
input$v*cos(input$th*((2*pi)/360))
}
x<-function(t){
vx*input$t
}
## Para y
vy<-function(v,th){
input$v*sin(input$th*((2*pi)/360))
}
y<-function(t){
(vy*input$t)-(0.5*g*t*t)
}
# Dibujamos el grafico de X v.s. Y
plot(x)
})
}
shinyApp(ui = ui, server = server)
There are couple of issues in the ui and in the server
In the numericInput, the value argument would be numeric as well instead of a string (" ")
Not clear why renderPrint was initiated in server without having a corresponding ui element
Functions were created in server without invoking it.
Below is a minimal code that gives an output by making couple of changes
changed the value = " " to a numeric initial value
added verbatimTextOutput in ui to print the renderPrint called in `server
removed those multiple functions created and simplified with a single expression to plot
library(shiny)
ui <- fluidPage(
titlePanel("Movimiento de Projectiles"),
sidebarLayout(
sidebarPanel(
width = 2,
numericInput(inputId = "v",
label = "Velocidad",
value = 22),
numericInput(inputId = "th",
label = "Angulo",
value = 15),
numericInput(inputId = "tf",
label = "Tiempo",
value = 41),
numericInput(inputId = "k",
label = "Constante k",
value = 32)
),
mainPanel(
plotOutput("posicion"),
verbatimTextOutput("v"),
verbatimTextOutput("th"),
verbatimTextOutput("tf"),
verbatimTextOutput("k")
)
)
)
server <- function(input, output) {
output$v <- renderPrint({ input$v})
output$th <- (renderPrint({ input$th}))
output$tf<-renderPrint({ input$tf })
output$k<-renderPrint({ input$k })
output$posicion <- renderPlot({
plot(input$v* cos(input$th*((2*pi)/360)) * input$th)
})
}
shinyApp(ui = ui, server = server)
-output

Struggling in getting output in r shiny app

I'm a beginner in shiny app. so first I tried to build an app to calculate distance covered using time taken and speed. I got error as "argument of length zero". Then I entered req(input$num_time,input$select_time,input$slider_speed)this command after that error message is not displaying and also not getting output also. I'm not able to find where I gone wrong. Please help me in getting the output. I have shown the code I used below:
library(shiny)
#library(car)
ui <- fluidPage(
titlePanel("terrain model"),
sidebarLayout(
sidebarPanel(
helpText("To create a suitable model"),
br(),
numericInput("num_time",
label = h6("Enter time"),
value = 1),
selectInput("select_time",
label = h6(""),
choices = list("Hours"= 1,"Minutes" = 2),
selected = "1"),
sliderInput("Speed",
label = "Speed:",
min = 2, max = 4.5, value = 2),
br(),
actionButton("action",label="Refresh & Calculate")
),
mainPanel(
textOutput("text_distance")
)
)
)
server <- function(input, output) {
values <- reactiveValues()
#calculate distance travelled
observe({input$action_Calc
values$int <- isolate({ input$num_time * recode(input$select_time,"1='60';2='1'")*input$slider_speed
})
})
#Display values entered
output$text_distance <- renderText({
req(input$num_time,input$select_time,input$slider_speed)
if(input$action_Calc==0)""
else
paste("Distance:", round(values$int,0))
})
}
shinyApp(ui, server)
I don't find any use of "Refresh & Calculate" button since the calculation is performed as soon as any of the input changes.
You can try this code :
ui <- fluidPage(
titlePanel("terrain model"),
sidebarLayout(
sidebarPanel(
helpText("To create a suitable model"),
br(),
numericInput("num_time",
label = h6("Enter time"),
value = 1),
selectInput("select_time",
label = h6(""),
choices = list("Hours"= 1,"Minutes" = 2),
selected = "1"),
sliderInput("Speed",
label = "Speed:",
min = 2, max = 4.5, value = 2),
br(),
actionButton("action",label="Refresh & Calculate")
),
mainPanel(
textOutput("text_distance")
)
)
)
server <- function(input, output) {
#Display values entered
output$text_distance <- renderText({
val <- input$num_time/dplyr::recode(input$select_time,"1"=1,"2"=60)*input$Speed * 1000
paste("Distance:", round(val,0), 'meters')
})
}
shinyApp(ui, server)

R Shiny and reactive value in shapiro.test()

I want to perform the Shapiro-Wilk-Test in my Shiny App. Over the two sliders and the numeric input I get the values for rnorm. And because I can change my inputs to rnorm, I made it reactive.
But when running the app it doesn't work - I tried a lot, but it wasn't successful.
How must I change my code that it works? Thanks in advance!
My code:
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
sliderInput("slider1", label = h3("Quantity of n"), min = 10, max = 100, value = 50),
sliderInput("slider2", label = h3("Mean of group 1"), min = 0, max = 100, value = 50),
numericInput("num", label = h3("SD of group 1"), value = 1)
),
mainPanel(
textOutput("Text1")
)
)
)
server <- function(input, output) {
group1=reactive({
rnorm(n=input$slider1,mean=input$slider2,sd=input$num)
})
# Shapiro-Wilk-Test
### shapiro.test(....)[[2]][1] shows the p-value
ND_G1=shapiro.test(group1())[[2]][1] # with "rnorm(10,5,2)" instead of group1() it works
output$Text1 <- renderText({paste("The p-value is: ",ND_G1)})
}
# Create Shiny app ----
shinyApp(ui = ui, server = server)

How to add a "next" button to the sliderInput of R shiny?

I plot GPS data on a leaflet map using R,
the trip is visualised through an incrementing timeline provided by the sliderInput in animation mode.
sliderInput("animation", "Mesures GPS :",
min = ifelse( !exists("i.data"), 0, min(i.data$sequence)),
max = ifelse( !exists("i.data"), 1, max(i.data$sequence)),
value = 0,
step=20,
animate = animationOptions(interval = 1000, loop = FALSE),
width="100%"))
I can see the GPS points jumping over the map, and now I am looking for the way to have a "next" button ([<<], [>>]) to control more finely local steps. I have not seen any such option in the sliderInput documentation.
This could also be done by adding ad-hoc shiny actionButtons,
any suggestion?
Adapting the code from ?sliderInput:
library(shiny)
shinyApp(
ui = fluidPage(
fluidRow(
div(style = "display: inline-block;vertical-align:center;",
actionButton("left", label = "<<")),
div(style = "display: inline-block;vertical-align:center;",
sliderInput("obs", "Number of observations:",
min = 0, max = 1000, value = 500
)),
div(style = "display: inline-block;vertical-align:center;",
actionButton("right", label = ">>")),
),
plotOutput("distPlot")
),
# Server logic
server = function(input, output, session) {
output$distPlot <- renderPlot({
hist(rnorm(input$obs))
})
observeEvent(input$left, {
updateSliderInput(session, "obs", value = input$obs - 10)
})
observeEvent(input$right, {
updateSliderInput(session, "obs", value = input$obs + 10)
})
}
)
This updates by +/- 10.

Shiny ui.r and server.r - I can't figure out the code to make the app work

This is a linear regression on mtcars data set in R Studio. I am trying to create the app. No luck. Working on it for days. Doesn't help that I am new to programming. Thanks for the help.
Is this right?
#server.r
data(mtcars)
mtdata<-mtcars
fit<-lm(mpg~cyl+hp+wt+qsec+am+gear,data=mtcars)
shinyServer(function(input, output) {
formulaText <- reactive({
#Not sure what this does.
paste("mpg ~", input$variable)
})
output$caption <- renderText({ # Not sure what this does.
formulaText()
})
# I am lost here. I think this part needs to 'mate up' with ui.r
output$mpgPlot <- renderPlot({
boxplot(as.formula(formulaText()), # I don't understand.
data = mtdata,
outline = input$outliers) # I don't understand.
})
})# I don't understand.
# ui.R
#fit<-lm(mpg~cyl+hp+wt+qsec+am+gear,data=mtcars)
shinyUI(fluidPage(
titlePanel("Guess which variables affect MPG!"),
# I understand sliders, radio buttons. There is a disconnect between ur
# and server.
fluidRow(
# I understand this.
column(3,
radioButtons("radio", label = h3("Cylinders"), # I understand.
choices = list("4 Cyl" = 4, "6 Cyl" =6, # I understand.
"8 Cyl" = 8),selected = 1)),
radioButtons("radio", label = h3("Number of Gears"),
choices = list("3" = 3, "4" =4,
"5" = 5),selected = 1),
#I understand this.
column(3,
selectInput("select", label = h3("Transmission Type"),
choices = list("Manual " = 1, "Automatic" =
2), selected = 1)),
fluidRow(
column(3,
sliderInput("slider1", label = h3("Horse Power"),
min = 52, max = 230, step = 5,value = 52),
sliderInput("slider2", label = h3("Weight, in tons"),
min = 1.513, max = 5.42,step = .1, value =
"min" ),
sliderInput("slider3", label = h3("Quarter Mile, in
Seconds"),
min = 14.60, max = 22.90, step = .1, value
# I don't understand.
="min" )), # I don't understand.
mainPanel( # I don't understand.
h3(textOutput("caption")), # I don't understand this.
plotOutput("mpgPlot") # I don't understand this.
) # I don't understand.
) # I don't understand.
) # I don't understand.
))
Some of the input that server.R is expecting from ui.R is not defined (input$variable and input$outliers). If you add the following controllers to your ui.R you will have a working Shiny app
radioButtons("variable", label=h3("Variable"),
choices = list("Cylinders"="cyl",
"Gears"="gear",
"Transmission"="am",
"Horse Power"="hp",
"Weight, in tons"="wt",
"Quarter Mile, in seconds"="qsec"),
selected="cyl"),
checkboxInput("outliers", "Show outliers", FALSE))

Resources