2016-10-26 16 views
0

は、これが私のアプリのコードです:Shinyで単純な棒グラフをモジュール化するには?

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) 

そして、これは私のモジュールのコードです:

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") 
    }) 
} 

このコード終了このエラーが発生しました:

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

どうすればこの作品を作成できますか?

答えて

1

このエラーが発生するのは、モジュールのサーバー部分の最初の3つの引数がinput,outputおよびsessionであることです。だから、あなたが変更する必要があります:

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

へ:

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

その変更に伴いだけで、あなたのコードは現在、エラーなしで実行されます。ただし、何も表示されません。これは、モジュールを使用するという別の重要なコンポーネントを忘れているためです。これは、ns()関数内のすべての入出力IDをラップすることです。だから、変更:

column(12, 
     plotOutput(stringName) 
) 

へ:

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

今、あなたはあなたのプロットが問題なく表示されるはずです。

+0

ありがとうございます! – user6777840

関連する問題