首页 > 解决方案 > 如何接受参数、接受用户输入和存储数据?

问题描述

我有一个清单

PID_short  PID_full 
   420     44101445
   419     44101375
   418     44101344
   417     44100125
   416     44095813
   415     44100240
data <- data.frame(PID_short = seq(from = 420, to = 415, by = -1),  
                   PID_full = c(44101445, 44101375, 44101344, 44100125, 44095813, 44100240))

我想创建一个图形用户界面,当我键入 时PID_short,它将显示PID_full. 例如,当我键入时420,它会显示44101445

我真的很陌生R。但我试过了,这是我的代码:

  library(shiny)

ui <- shinyUI(fluidPage(

  titlePanel(title=h3("Please select the information :", align = "center")),
  sidebarLayout(
    sidebarPanel(
      sliderInput("slide","Select the value of PID_short ", min = 415, max = 420, value = 417)      
      ),
    mainPanel(
      textOutput("out")
    ))))


server <- shinyServer(function(input,output){

  output$out <- renderText(
    paste("The PID_full is:",input$slide)
)})

shinyApp(ui = ui, server = server)

我应该在哪里修改我的代码?或者我应该如何以更好的方式写作?请帮助我...我将非常感激。

标签: ruser-interfaceshiny

解决方案


您可以过滤数据框,以便只保留PID_short等于输入的行。然后选择PID_fullwith的值select

library(shiny)
library(dplyr)

data <- data.frame(PID_short = seq(from = 420, to = 415, by = -1),  
                   PID_full = c(44101445, 44101375, 44101344, 44100125, 44095813, 44100240))

ui <- shinyUI(fluidPage(

  titlePanel(title=h3("Please select the information :", align = "center")),
  sidebarLayout(
    sidebarPanel(
      sliderInput("slide","Select the value of PID_short ", min = 415, max = 420, value = 417)      
    ),
    mainPanel(
      textOutput("out")
    ))))


server <- shinyServer(function(input,output){

  output$out <- renderText(
    paste("The PID_full is:", 
          data %>% 
            filter(PID_short %in% input$slide) %>% 
            select(PID_full))
  )})

shinyApp(ui = ui, server = server)

推荐阅读