PlotOutput讲述了意想不到的","

时间:2017-10-21 20:06:59

标签: r shiny

我正在使用mpg数据集练习我的闪亮技能,我想尝试PlotOutput功能,但是,它无法正常工作!它告诉我,我有一个意想不到的",#34;。我不知道为什么。

我的ui.R看起来像这样:

  library(shiny)
  library(ggplot2)
  library(dplyr)

  shinyUI(fluidPage(

   plotOutput("plot1",
         click = "plot_click",
         dblclick = "plot_dblclick",
         hover = "plot_hover",
         brush = "plot_brush"),

 verbatimTextOutput("info"),

 # Application title
 titlePanel("mpg"),


sidebarLayout(
sidebarPanel(
  selectInput(inputId = "var1",
              label = "Choose x variable",
              choices = 
                names(mpg)
  ),
  uiOutput('Var1Slider'),

  br(),
  selectInput(inputId = "var2",
              label = "Choose y variable",
              choices = 
                names(mpg)[sapply(mpg, class) != "character"])
),

mainPanel(
  plotOutput("distPlot")
)
)
))

我的server.R看起来像这样:

server <- function(input,output){

  output$distPlot <- renderPlot({

    # browser()

if(typeof(mpg[[input$var1]]) == "character")
{
  ggplot(mpg) +
    xlab(input$var1) +
    ylab(input$var2) +
    ggtitle(paste("Plot", input$var1, "vs", input$var2)) +
    geom_boxplot(mapping =
                   aes_string(x = input$var1,
                              y = input$var2))
}

else
{
  mpg %>%
    filter(get(input$var1) >= input$Var1Slide[1]) %>%
    filter(get(input$var1) <= input$Var1Slide[2]) %>%
    ggplot() +
    xlab(input$var1) +
    ylab(input$var2) +
    ggtitle(paste("Plot", input$var1, "vs", input$var2)) +
    geom_point(mapping =
                 aes_string(x = input$var1,
                            y = input$var2))
}
})

output$Var1Slider <- renderUI({

# browser()

if(typeof(mpg[[input$var1]]) == "character")
{
  return(NULL)
}
else
{
  sliderInput('Var1Slide',
              label = paste("selected:",input$var1),
              min = min(mpg[[input$var1]]),
              max = max(mpg[[input$var1]]),
              value = c(min(mpg[[input$var1]]),max(mpg[[input$var1]])),
              step = range(mpg[[input$var1]]) / 5)
}

xy_str <- ""
if(is.null(e)) return("NULL\n")
 xy_str <- paste0("x=", round(e$x, 1), " y=", round(e$y, 1), "\n")

xy_range_str <- ""
if(is.null(e)) return("NULL\n")
xy_range_str <- paste0("xmin=", round(e$xmin, 1), " xmax=",    round(e$xmax, 1), 
       " ymin=", round(e$ymin, 1), " ymax=", round(e$ymax, 1))

  paste0
  (
    "click: " = xy_str(input$plot_click),
    "dblclick: " = xy_str(input$plot_dblclick),
    "hover: " = xy_str(input$plot_hover),
    "brush: " = xy_range_str(input$plot_brush)
  )

  }
  )
}

1 个答案:

答案 0 :(得分:2)

如果将paste0函数替换为:

        paste0(
        "click: =", xy_str(input$plot_click),
        "dblclick: =", xy_str(input$plot_dblclick),
        "hover: =", xy_str(input$plot_hover),
        "brush: =", xy_range_str(input$plot_brush)
    )

这将解决该特定错误。我不认为paste0被注册为函数,因为括号在不同的行上,函数中的每个参数之间也需要逗号。