r闪亮显示数据集中的特定行

时间:2015-02-22 01:18:41

标签: r shiny jscript

很抱歉,我正在重新发布此问题,因为版主未删除之前发布的此问题(https://stackoverflow.com/questions/28643738/r-shiny-user-input-text-box-and-button) 我试图在r闪亮写一个脚本

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.

例如,考虑虹膜数据集

头(光圈)

     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

用户输入一个值(5.1),闪亮检查Sepal.Length列中是否存在该值,如果存在则显示该值(5.1)和相应的Species值(Setosa)。如果该值不存在,则找不到闪亮的显示。

这是我到目前为止所做的。需要一些指示。

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") })
      }
    )

1 个答案:

答案 0 :(得分:2)

这样做:

您需要在ui中添加另一个textOutput,然后在服务器中获取被动,以便您访问它。

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

这会给你:

enter image description here

当您输入5.1时,有多个选项 - 并且都会返回。

相关问题