Reputation: 171
Sorry I am reposting this question because the moderator is not removing the hold on this questions posted before (https://stackoverflow.com/questions/28643738/r-shiny-user-input-text-box-and-button) I am trying to write a script in r shiny such that
Step 1) Accept input from the user
Step 2) Check if that value exist in the dataset (iris)
Step 3) if that value exists then display that value and another value from a different column associated with that value.
For example , considering the iris dataset
head(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
5.1 3.5 1.4 0.2 setosa
4.9 3.0 1.4 0.2 setosa
4.7 3.2 1.3 0.2 setosa
4.6 3.1 1.5 0.2 setosa
5.0 3.6 1.4 0.2 setosa
5.4 3.9 1.7 0.4 setosa
The user input's a value (5.1), shiny checks if this value exists in the Sepal.Length column, if it exists then display that value (5.1) and the corresponding Species value (Setosa). If that value does not exisits then shiny displays not found.
This is what I did so far. Need some pointers.
UI.r
shinyUI(fluidPage(
titlePanel("Test Case"),
sidebarLayout(
sidebarPanel((""),
textInput("mrnnumb", "Enter Sepal Length",""),
submitButton("Ok")),
mainPanel(("Results"),
textOutput("yn"))
)
))
server.r
data(iris)
library(shiny)
shinyServer(
function(input, output, session)
{
output$yn = reactive({ifelse((input$mrnnumb) %in% iris$Sepal.Length, "Found","Not Found") })
}
)
Upvotes: 0
Views: 1738
Reputation: 14192
This does it:
you need to add another textOutput
in ui and then get the reactive in server to let you access it.
library(shiny)
ui <- fluidPage(
titlePanel("Test Case"),
sidebarLayout(
sidebarPanel((""),
textInput("mrnnumb", "Enter Sepal Length",""),
submitButton("Ok")),
mainPanel(("Results"),
textOutput("yn"),
textOutput("species"))
)
)
server <- function(input, output) {
data(iris)
output$yn = reactive({ifelse((input$mrnnumb) %in% iris$Sepal.Length, "Found","Not Found")})
output$species = reactive({
n <- input$mrnnumb
myspecies <- iris[(iris$Sepal.Length == n),5]
return(myspecies)
})
}
shinyApp(ui = ui, server = server) #this runs the app
this gives you:
When you enter 5.1 there are multiple options - and all are returned.
Upvotes: 3