2016-10-26 7 views
0

Das ist meine App-Code:Wie man ein einfaches Balkendiagramm in Shiny modularisiert?

app.R

library(shiny) 

source("func.R") 

# create data 
name <- c("Moller", "Mayer", "Bernard") 
sales <- c(35000, 40000, 60000) 
df <- data.frame(name, sales) 

# app 
server <- function(input, output, session) { 
    x <- callModule(testPlot, "test", data = reactive(df), xAxis = reactive("name"), yAxis = reactive("sales")) 
} 
ui <- fluidPage(
    testPlotUI(id = "test", stringName = "test") 
) 

shinyApp(ui = ui, server = server) 

Und das ist mein Modul-Code:

func.R

library(shiny) 
library(ggplot2) 

testPlotUI <- function(id, stringName){ 
    ns <- NS(id) 
    fluidRow(
    column(12, 
     plotOutput(stringName) 
    ) 
) 
} 

testPlot <- function(data, xAxis, yAxis){ 
    output$test <- renderPlot({ 
    ggplot(data(), aes_string(x=xAxis(), y=yAxis())) + geom_bar(stat = "identity") 
    }) 
} 

Dieser Code Enden mit diesem Fehler:

Error in module(childScope$input, childScope$output, childScope, ...) : unused arguments (childScope$input, childScope$output, childScope)

Wie kann ich das machen?

Antwort

1

Der Grund, warum Sie diesen Fehler erhalten, ist, dass die ersten drei Argumente für den Server-Teil des Moduls unbedingt input, output und sein müssen. So müssen Sie ändern:

testPlot <- function(data, xAxis, yAxis){ 
    output$test <- renderPlot({ 
    ggplot(data(), aes_string(x=xAxis(), y=yAxis())) + geom_bar(stat = "identity") 
    }) 
} 

in:

testPlot <- function(input, output, session, data, xAxis, yAxis){ 
    output$test <- renderPlot({ 
    ggplot(data(), aes_string(x=xAxis(), y=yAxis())) + geom_bar(stat = "identity") 
    }) 
} 

Mit dieser Änderung allein wird Ihr Code jetzt ohne Fehler ausgeführt werden. Nichts wird jedoch erscheinen. Das ist, weil Sie eine andere Schlüsselkomponente der Verwendung von Modulen vergessen haben, nämlich das Umschließen aller Eingabe/Ausgabe-IDs in der ns()-Funktion. So ändern:

column(12, 
     plotOutput(stringName) 
) 

in:

column(12, 
     plotOutput(ns(stringName)) 
) 

Jetzt sollten Sie sehen, Ihr Grundstück ohne Probleme erscheinen.

+0

vielen dank! – user6777840

Verwandte Themen