カスタム散布図を表示するR Shinyアプリケーションを構築しようとしています。ユーザーがデータセットのどの変数を選択ボックスでx軸とy軸に配置するかを選択できるようにしました。さらに(これはトラブルが始まるところです)グラフでプロットされた各変数の値の範囲を、選択した各変数のカスタムスライダーで制限する機能が必要です。したがって、基本的に、ユーザーは各変数の最小値と最大値を指定します。私はこれをuiOutput
とrenderUi
の機能で実現します。これまでのところ、スライダーは正常に動作しているようです。今Shiny App:データをサブセット化した後にggplot2がプロットを表示しない、エラー:inccorect length()、expecting()
、問題:私は、入力値に基づいてデータをサブセットし、結果をプロットしてみたら、私はエラーを取得する:
"incorrect length(32), expecting 29".
ここで間違って行くことができるもの?
私がこれを使って実行しようとするデータセットはかなり大きいですが、サンプルコードではmtcarsデータセットを使用しました(同じ問題)。
library(shiny);library(ggplot2);library(dplyr)
#load data
data(mtcars)
ui <- shinyUI(fluidPage(
# Application title
titlePanel("Mtcars Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
selectInput(inputId = "x",
label = "x variable:",
choices = names(mtcars),
selected = names(mtcars)[1]),
uiOutput("xslider"),
selectInput(inputId = "y",
label = "y variable:",
choices = names(mtcars),
selected = names(mtcars)[4]),
uiOutput("yslider")
),
mainPanel(
plotOutput("scatterPlot")
)
)
))
# Define server logic required to draw scatterplot
server <- shinyServer(function(input, output) {
#Get summary stats for x and y variables,
#this is used to set the parameters for the sliders in renderUI
summary.x <- reactive({
summary(mtcars[,input$x])
})
summary.y <- reactive({
summary(mtcars[,input$y])
})
output$xslider <- renderUI({
sliderInput(inputId = "xlim",
label = "limit x axis:",
min = summary.x()[1],
max = summary.x()[6],
value = c(summary.x()[1], summary.x()[6]),
step = .01)
})
output$yslider <- renderUI({
sliderInput(inputId = "ylim",
label = "limit y axis:",
min = summary.y()[1],
max = summary.y()[6],
value = c(summary.y()[1], summary.y()[6]),
step = .01)
})
output$scatterPlot <- renderPlot({
#Now subset data so values of x variable
#are within the range specified by input$xlim
#and values of y are within range of input$ylim
subdata <- filter(mtcars, mtcars[,input$x] < input$xlim[2],
mtcars[,input$x] > input$xlim[1])
subdata <- filter(subdata, mtcars[,input$y] < input$ylim[2],
mtcars[,input$y] > input$ylim[1])
#display the scatterplot
ggplot(subdata, aes_string(input$x,input$y)) + geom_point()
})
})
# Run the application
shinyApp(ui = ui, server = server)