Clarinetist - 3 months ago 10

R Question

I'm very new to Shiny.

Consider the following:

**ui.R**:

`library(shiny)`

shinyUI(fluidPage(

titlePanel("The Linear Equation y = 0.1x"),

# Sidebar with a slider input for number of bins

sidebarLayout(

sidebarPanel(

sliderInput("x",

"X-value:",

min = 0,

max = 100,

value = 0,

step = 0.1)

),

# Show a plot of the generated distribution

mainPanel(

plotOutput("linPlot")

)

)

))

`library(shiny)`

library(ggplot2)

shinyServer(function(input, output, session) {

f <- function(x){

0.1*x

}

output$linPlot <- renderPlot({

point_f <- data.frame(x = input$x,

y = f(input$x))

ggplot(NULL, aes(x = x)) +

stat_function(data = data.frame(x = c(0, 100)),

fun = f, geom = 'line', col = 'blue') +

geom_point(data = point_f, aes(x = x, y = y),

size = 4, col = 'blue')

})

session$onSessionEnded(function() { stopApp() })

})

I assume this isn't a solvable problem using any options for the slider, so I would also like a text box added to the sidebar panel which has the following things:

- The value in the text box can be used as input to do what the slider does (so the value should be the same in the slider and in the text input).
`input$x`

- As the slider moves, the text box changes its value to match the value of the slider.
- As the text box value changes, the slider moves to match its value of the text box.
- Values outside of the interval [0, 100] are unacceptable for the text box, and would ideally spit out an error.
- The input of the text box must be numeric, and in the form ab.c, where each of a, b, and c is a single-digit number.

Ideally, if the slider can handle this problem on its own, that would be great, but I haven't found anything that suggests that it can.

I know that this involves

`textInput`

Answer

If I understand you right you need observe and update like :

```
library(shiny)
ui=shinyUI(fluidPage(
titlePanel("The Linear Equation y = 0.1x"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("x",
"X-value:",
min = 0,
max = 100,
value = 0,
step = 0.1),
numericInput("x_txt","X-value:",min = 0,
max = 100,
value = 0,
step = 0.1)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("linPlot")
)
)
))
library(shiny)
library(ggplot2)
server=shinyServer(function(input, output, session) {
f <- function(x){
0.1*x
}
observeEvent(input$x,{
if(input$x!=input$x_txt){
updateNumericInput(session,"x_txt",value = input$x)
}
})
observeEvent(input$x_txt,{
if( is.numeric(input$x_txt) & input$x_txt>=0 & input$x_txt<=100){
if(input$x!=input$x_txt){
updateNumericInput(session,"x",value = input$x_txt)
}}else{
updateNumericInput(session,"x_txt",value = input$x)
}
})
point_x=reactive({
})
output$linPlot <- renderPlot({
point_f <- data.frame(x = input$x,
y = f(input$x))
ggplot(NULL, aes(x = x)) +
stat_function(data = data.frame(x = c(0, 100)),
fun = f, geom = 'line', col = 'blue') +
geom_point(data = point_f, aes(x = x, y = y),
size = 4, col = 'blue')
})
session$onSessionEnded(function() { stopApp() })
})
shinyApp(ui,server)
```

Source (Stackoverflow)

Comments