2017-02-15 5 views
0

Я привязываю, чтобы создать R блестящее приложение, и я хотел бы иметь два имени набора данных и имя столбца selectInput i.e. Прямо сейчас, я могу получить имена наборов данных в первом входе, но я не могу создать зависимую колонку selectIput (список которой будет зависеть от выбранного набора данных). Пожалуйста, направляйте.Как создать зависимые подсказки в приложении Shiny в R

require(shiny) 
require(MASS) 

a <- data(package = "MASS")[3] 
b <- a$results[,3] 

ui <- fluidPage(
    sidebarPanel(width = 2, 

    selectInput(inputId = "dsname",label = "Select Dataset:",choices = c(b)), 

    colnames <- names("dsname"), 

    selectInput(inputId = "columns", label = "Choose columns", 
       choices = c(colnames))     
) 
) 

server <- function(input,output) {} 

shinyApp(ui <- ui, server <- server) 

ответ

0

Для того, чтобы иметь «чувствительные» элементы в блестящей, вам нужно обернуть выражения для вычисления элементов реагирующих в reactive({...}).

Вы можете использовать renderUI в своих server() и uiOutput в своем ui() с чем-то вроде этого. Вот пример, который я использовал для использования некоторых наборов данных R (диафрагмы, mtcars и бриллианты):

library(shinythemes) 
library(shiny) 
library(ggplot2) 

ui <- fluidPage(theme = shinytheme("superhero"), 
    titlePanel("Welcome to Responisve Shiny"), 
    sidebarLayout(
     sidebarPanel(
      selectInput("data", "Dataset:", 
         choices = c("mtcars", "iris", "diamonds") 
        ), 
     uiOutput("x_axis"), 
     uiOutput("y_axis"), 
     uiOutput("color") 
    ), 
     mainPanel(
     plotOutput("distPlot") 
    ) 
    ) 
) 
server <- function(input, output) { 
    output$x_axis <- renderUI({ 
     col_opts <- get(input$data) 
     selectInput("x_axis2", "X-Axis:", 
        choices = names(col_opts)) 
    }) 
    output$y_axis <- renderUI({ 
     cols2 <- reactive({ 
      col_opts2 <- get(input$data) 
      names(col_opts2)[!grepl(input$x_axis2, names(col_opts2))] 
     }) 
     selectInput("y_axis2", "Y-Axis:", 
        choices = cols2(), 
        selected = "hp") 
    }) 
    output$color <- renderUI({ 
     col_opts <- get(input$data) 
     selectInput("color", "Color:", 
        choices = names(col_opts), 
        selected = "cyl") 
    }) 
    output$distPlot <- renderPlot({ 
     if(input$data == "mtcars"){ 
      p <- ggplot(mtcars, aes_string(input$x_axis2, input$y_axis2, color = input$color)) + 
       geom_point() 
     } 
     if(input$data == "iris"){ 
      p <- ggplot(iris, aes_string(input$x_axis2, input$y_axis2, color = input$color)) + 
       geom_point() 
     } 
     if(input$data == "diamonds"){ 
      p <- ggplot(diamonds, aes_string(input$x_axis2, input$y_axis2, color = input$color)) + 
       geom_point() 
     } 
     print(p) 
    }) 
} 
shinyApp(ui = ui, server = server)