2016-04-19 10 views
1

ich ein anderes Problem mit meiner aplication habe. Ich brauche Histogramm-Funktion, um es hinzuzufügen.(Shiny, R) macht Histogramm aus CSV-Datei (mit Spaltenauswahl über Combobox/Select-Eingang.

Auf dritte Registerkarte Anwendung erstellen sollte Histogramm von hochgeladenen Datei (Spalte wird über Combobox/selectinput ausgewählt)

Anwendung tatsächlich Combobox mit Spalten aus cSV-Datei erstellen kann

Aber wenn ich Histogramm machen will, auf dem Registerkarte „Histogramm zeigt nur Fehler:..

ERROR: object of type 'closure' is not subsettable 

Ich weiß nicht, was ich falsch gemacht habe.

Es gibt Code

ui <- shinyUI(fluidPage(
    titlePanel("Aplikacja testowa nr 6. Praca z plikiem- wybór kolumny"), 

    sidebarLayout(
    sidebarPanel(
     fileInput("file", label = h3("Wgraj Plik")), 
     checkboxInput(inputId = 'header', label = 'Pierwszy wers to etykiety', value = FALSE), 
     radioButtons(inputId = 'sep', label = 'Co jest separatorem', choices = c("Przecinek"=',',"Średnik"=';',"Tabulator"='\t', "Spacja"=''), selected = ','), 

     checkboxGroupInput("choices1", label = h3("Wybierz Kolumny"), choices = NULL), 
     # there is combobox to pick column 
     selectInput("combobox", label = h3("(Histogram) Wybierz kolumne"), choices = NULL) 





    ), 

    mainPanel(
     uiOutput("tb") 
    ) 
) 
)) 

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

    data <- reactive({ 
    file1 <- input$file 
    if(is.null(file1)){return()} 
    dataSet <- read.csv(file=file1$datapath, sep=input$sep, header = input$header) 

    updateCheckboxGroupInput(session, "choices1", choices = colnames(dataSet)) 
     # this line updates selection in combobox 
    updateSelectInput(session, "combobox", choices = colnames(dataSet)) 

    dataSet 
    }) 

    output$table <- renderTable({ 
    if(is.null(data())){return()} 
    data()  
    }) 

    output$table2 <- renderTable({ 


    if(is.null(data()) || is.null(input$choices1)){return()} 
    data()[input$choices1]  
    }) 

# there is part of file where i make histogram 
output$wykres <- renderPlot({ 
x <- data[0, input$combobox] 
hist(x , col = 'blue', border = 'white') 
}) 


    output$tb <- renderUI({ 
    if(is.null(data())) 
     h5("Wgraj Plik jeśli chcesz cokolwiek zrobić.") 
    else 
     tabsetPanel(tabPanel("dane", tableOutput("table")),tabPanel("wybrane kolumny", tableOutput("table2")), tabPanel("Histogram", plotOutput("wykres"))) 
    }) 
} 

shinyApp(ui, server) 
+0

Innerhalb des 'hist' Codes haben Sie vergessen, Klammern hinter' data' zu setzen. Sollte sein 'x <- data() [0, .......' –

+0

Oh ... Es tut mir leid - Ich habe Ihren Kommentar nicht gesehen, bevor Sie die Frage beantworten :(. Übrigens, es sollte nicht auch nicht 0. –

Antwort

1

Das Problem war, dass man () zu data im folgenden Stück Code hinzufügen, vergessen haben.

output$wykres <- renderPlot({ 
    # x <- data[, input$combobox] # zapomniales klamry 
    x <- data()[, input$combobox] 
    hist(x , col = 'blue', border = 'white') 
    }) 

I erweitert zusätzlich den Code vorbei diskreten Variablen in hist Funktion zu vermeiden, indem sie einen Alarm mit shinyBS Paket erstellt und von req(is.numeric(x)) hinzufügen.

library(shinyBS) 

ui <- shinyUI(fluidPage(
    titlePanel("Aplikacja testowa nr 6. Praca z plikiem- wybór kolumny"), 

    sidebarLayout(
    sidebarPanel(
     fileInput("file", label = h3("Wgraj Plik")), 
     checkboxInput(inputId = 'header', label = 'Pierwszy wers to etykiety', value = FALSE), 
     radioButtons(inputId = 'sep', label = 'Co jest separatorem', choices = c("Przecinek"=',',"Średnik"=';',"Tabulator"='\t', "Spacja"=''), selected = ','), 

     checkboxGroupInput("choices1", label = h3("Wybierz Kolumny"), choices = NULL), 
     # there is combobox to pick column 
     selectInput("combobox", label = h3("(Histogram) Wybierz kolumne"), choices = NULL) 

    ), 

    mainPanel(
     uiOutput("tb") 
    ) 
) 
)) 

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

    data <- reactive({ 
    file1 <- input$file 
    if(is.null(file1)){return()} 
    dataSet <- read.csv(file=file1$datapath, sep=input$sep, header = input$header) 

    updateCheckboxGroupInput(session, "choices1", choices = colnames(dataSet)) 
    # this line updates selection in combobox 
    updateSelectInput(session, "combobox", choices = colnames(dataSet)) 

    dataSet 
    }) 

    output$table <- renderTable({ 
    if(is.null(data())){return()} 
    data()  
    }) 

    output$table2 <- renderTable({ 


    if(is.null(data()) || is.null(input$choices1)){return()} 
    data()[input$choices1]  
    }) 

    # there is part of file where i make histogram 
    output$wykres <- renderPlot({ 
    x <- data()[, input$combobox] 

    if (!is.numeric(x)) { 
     createAlert(session, "alarm", alertId = "niebezpieczenstwo", 
        title = "Niebezpieczenstwo: ", 
        content = "Histogram przyjmuje tylko wartosci ciagle!", 
        style = "danger", dismiss = TRUE, append = TRUE) 
    } 
    if (is.numeric(x)) { 
     closeAlert(session, "niebezpieczenstwo") 
    } 

    req(is.numeric(x)) 
    hist(x , col = 'blue', border = 'white') 
    }) 


    output$tb <- renderUI({ 
    if(is.null(data())) 
     h5("Wgraj Plik jeśli chcesz cokolwiek zrobić.") 
    else 
     tabsetPanel(tabPanel("dane", tableOutput("table")), 
        tabPanel("wybrane kolumny", 
          tableOutput("table2")), 
        tabPanel("Histogram", 
          bsAlert("alarm"), 
          plotOutput("wykres"))) 
    }) 
} 

shinyApp(ui, server) 
+0

Danke für deine Hilfe, das hat mir sehr geholfen. –