2016-11-10 20 views
0

感謝您的建議。在我問我自己之前,我檢查了這個問題,但我無法解決我的問題。我定義了一個向量var_name < -reactive(input $ xcol),然後我使用tableData [,c(「var_name」)]作爲hist函數的輸入,但沒有成功。Isuue與我的Shiny App中的renderPlot

我正在構建一個應用程序,用戶可以在其中上傳自己的文件並進行一些分析。我可以設法更新selectInput中每個用戶上傳不同文件的變量名稱。現在我的問題是,我無法通過selectInput中選擇的變量來構建直方圖。我如何將選中的變量作爲輸入傳遞給直方圖?這是我的ui.r代碼。一旦我執行我的代碼,我在我的應用程序中得到的錯誤是「類型'對象關閉'不是子集」。有什麼建議嗎? 感謝

library(shiny) 
ui<-fluidPage(
titlePanel(title = "My app"), 
sidebarLayout(
sidebarPanel(
    fileInput('file1', 'Cargar archivo', 
      accept = c(
       'text/csv', 
       'text/comma-separated-values', 
       'text/tab-separated-values', 
       'text/plain', 
       '.csv', 
       '.tsv' 
      ) 
), 
    checkboxInput('header', '¿Contiene Encabezado?', TRUE), 
    radioButtons('sep', 'Delimitador', 
       c(Comma=',', 
       "Punto y coma"=';', 
       Tab='\t'), 
       ','), 
    radioButtons('quote', 'Quote', 
       c(Ninguna='', 
       'Dobles'='"', 
       'Simples'="'"), 
       '"'), 
    radioButtons('resume', 'Summary', 
       c('Individual', 
       'Múltiple'), selected = "Múltiple", 
       inline = TRUE), 
    conditionalPanel("input.resume === 'Individual'", 
        selectInput('xcol', 'Variable X', ""), 
    sliderInput("bins","Seleccione el número de clases",min=5, max = 15, value = 6), 
    radioButtons("color","Seleccione un color", c("Amarillo","Azul","Rojo","Gris"), selected = "Azul", inline = TRUE) 
) 
), 
mainPanel(h3("Muestra del archivo cargado:"), 
      tableOutput('contents'), 
      verbatimTextOutput("summary"), 
      plotOutput("histog") 
) 
) 
) 


server<-function(input, output, session) { 
tableData <- reactive({ 

inFile <- input$file1 

if (!is.null(inFile)){ 
    read.csv(inFile$datapath, header=input$header, sep=input$sep, 
      quote=input$quote) 
} 
else { 
    return(NULL) 
} 
}) 
observe({ 
updateSelectInput(
    session, 
    "xcol", 
    choices=names(tableData())) 
    }) 
var_name<-reactive(input$xcol) 

output$contents <- renderTable({ 
head(tableData()) 
}) 
output$summary <- renderPrint({ 
summary(tableData()) 
}) 
output$histog<-renderPlot({ 

hist(tableData[,c("var_name")],breaks= seq(0,10,l=input$bins+1), col(input$color)) 
}) 
} 

shinyApp(ui = ui, server = server) 
+0

的可能的複製[錯誤在:類型的對象「閉合」不是subsettable](http://stackoverflow.com/questions/11308367/error -in-my-code-object-of-type-closure-is-not-subsettable) – yeedle

+0

謝謝@ yeedle。我檢查了這個問題,但它並沒有解決我的問題。我在我的server.R中定義了以下內容:var_name <-reactive(input $ xcol)output $ histog <-renderPlot(histData([,c(「var_name」)],bre​​aks = seq(0,10,l = input $ bins + 1),col(input $ color)) })但是這並沒有解決我的問題。 – Jrgsua83

回答

0

實際上有幾個問題你閃亮的應用程序:

  1. 要認識到的,顏色應該編碼是這樣的:

    c(Amarillo="yellow",Azul="blue",Rojo="red",Gris="grey") 
    
  2. var_name應如何處理NULL

    var_name<-reactive({ 
        if(input$xcol!="") 
         input$xcol 
        else 
         NULL 
        }) 
    
  3. 反應性值應始終與括號中:

    tableData() 
    var_name() 
    

這實際上是什麼產生錯誤

  • 顏色參數應該是這樣寫:

    col=input$color 
    
  • 你應該ch的eck您的反應性值是否NULL與否

    if(!is.null(tableData())&&!is.null(var_name())) 
        hist(tableData()[,var_name()],breaks= seq(0,10,l=input$bins+1), col=input$color)