簡體   English   中英

在有光澤的應用程序中通過selectInput傳遞列名

[英]Passing column name via selectInput in shiny application

我有一個簡單的閃亮應用程序,我想將selectInput的值作為數據框的列名傳遞,並在ggplot中使用它。 我的UI代碼如下所示:

library(shiny)

# Define UI for application that draws a histogram
shinyUI(fluidPage(

  # Application title
  titlePanel("Title"),

  # Sidebar with a slider input for the number of bins
  sidebarLayout(
    sidebarPanel(
      selectInput(inputId = "yaxis", 
                  label = "Y-axis",
                  choices = list("Overall Rank" = "overall_rank", 
                                 "Income Deprivation" = "income_deprivation_rank"), 
                  selected = "income_deprivation_rank"),

      selectInput(inputId = "xaxis", 
                  label = "X-axis",
                  choices = list("Overall Rank" = "overall_rank", 
                                 "Income Deprivation" = "income_deprivation_rank"), 
                  selected = "overall_rank")),

    # Show a plot of the generated distribution
    mainPanel(
      plotOutput("distPlot"),
      h5("Notes"),
      p("notes")
    )
      )
    ))

我的服務器端代碼非常簡單,我有一個SPARQL查詢(在此處剪裁以節省空間)可創建簡單的數據幀:

# Libs
require(shiny); require(SPARQL); require(ggplot2)

# Server function
shinyServer(function(input, output) {

  # Source the data

  ## Define endpoint URL.
  endpoint <- "http://data.opendatascotland.org/sparql.csv"

  ### Create Query 
  query.simd <- "PREFIX stats: <http://statistics.data.gov.uk/id/statistical-geography/>
      (...) cut to save space (...)"

  ## Make the data
  dta.simd<- SPARQL(url = endpoint, query = query.simd, format = "csv")$results


  ## Make the plot
  output$distPlot <- renderPlot({

    xaxis <- as.character(input$xaxis) 
    yaxis <- as.character(input$yaxis)

    # draw the the plot
    ggplot(data = dta.simd, aes(x = xaxis, y = yaxis)) +
      geom_point(shape=1)

})
})

該查詢將產生一個簡單的數據框,從而重新提取以下摘錄:

observation overall_rank income_deprivation_rank
a001        2            6
a002        10           7
a003        11           9

編譯應用程序后,我繼續收到錯誤:找不到對象“ xaxis” 這使我相信,無論出於何種原因, input$xaxis的值都不會傳遞給xaxis對象,因此無法在ggplot中使用。 如果我決定將as.character(input$yaxis)替換為與列名相對應的字符串,例如overall_rank和另一個income_deprivation_rank該應用程序應能正常工作,因此問題顯然與使用input$xaxis相關聯input$xaxis值。 我嘗試了沒有as.character()函數的代碼,但收到了相同的錯誤消息。

通常,在通常將列名引用為字符串的地方(如input$colName ,請將其替換為get(input$colName) 這樣,Shiny便知道獲取input$colName的值,而不是將其視為字符串。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM