我的ggplot没有使用R显示

我的ggplot没有使用R显示,r,ggplot2,shiny,R,Ggplot2,Shiny,我不知道为什么在运行我的应用程序时我的ggplot没有出现。它使用plot显示,它工作正常。但使用ggplot时,什么都不会出现。没有图表!我尝试了print(),没有它,没有结果 我的应用程序我导入一个csv文件并从中绘制一个图表。 你能帮帮我吗 #服务器 server您的代码中存在一些问题:input$xcol和input$ycol包含字符值,因此必须在ggplot函数中使用aes_string。此外,还需要将选项卡面板和侧栏布局弄清楚(小问题) 除此之外,在您的数据响应中,您使用的sep和

我不知道为什么在运行我的应用程序时我的ggplot没有出现。它使用plot显示,它工作正常。但使用ggplot时,什么都不会出现。没有图表!我尝试了
print()
,没有它,没有结果

我的应用程序我导入一个csv文件并从中绘制一个图表。 你能帮帮我吗

#服务器


server您的代码中存在一些问题:
input$xcol
input$ycol
包含字符值,因此必须在
ggplot
函数中使用
aes_string
。此外,还需要将
选项卡面板
侧栏布局
弄清楚(小问题)

除此之外,在您的
数据
响应中,您使用的
sep
quote
输入在您的UI中找不到,从而导致错误。如果我把它们注释掉,一切都按预期进行。对于测试,我使用了
write.csv(diamonds,file=“diamonds.csv”)

库(闪亮)
图书馆(GG2)
shinyApp(
ui=fluidPage(
选项卡面板(
选项卡面板(
“上传文件”,
titlePanel(“上传文件”),
文件输入(
inputId=“file1”,
label=“选择CSV文件”,
多重=假,
accept=c(“文本/csv”,“文本/逗号分隔值,文本/普通”,“.csv”)
)),
选项卡面板(
“阴谋”,
带边框的页面(
headerPanel(“绘制数据”),
侧栏面板(
选择输入(“xcol”、“X变量”、“变量”),
选择输入(“ycol”、“Y变量”、“选定项”)
),
主面板(绘图输出(“MyPlot”))
)
)))
,

服务器ui.R在哪里?您不需要
print()
。将最后一行更改为
p
将返回它,但我不确定这是否能解决所有问题,因为我看不到您的ui代码。这是正确的,如果函数不包含return语句,则函数中的最后一行被视为returnui@AdamBirenbaum,请在我的ui上方。谢谢您的评论!非常有用普福!
server <- function(input, output, session) {
  # added "session" because updateSelectInput requires it

  data <- reactive({
    req(input$file1) # require that the input is available

    df <- read.csv(input$file1$datapath,
                       header = input$header,
                       sep = input$sep,
                       quote = input$quote)


  updateSelectInput(session, inputId = 'xcol', label = 'X Variable',
                    choices = names(df), selected = names(df)[sapply(df, is.numeric)])
  updateSelectInput(session, inputId = 'ycol', label = 'Y Variable',
                    choices = names(df), selected = names(df)[sapply(df, is.numeric)])

  return(df)

  })

  output$contents <- renderTable({
    data()
  })

  output$MyPlot <- renderPlot({
    x <- data()[, c(input$xcol, input$ycol)]

   p <- ggplot(x, aes(input$xcol,input$ycol)) 
   p <- p + geom_line() #+ geom_point()
   print(p)


    # plot(mydata, type = "l",
    #      xlab = input$xcol,
    #      ylab = input$ycol)
  })

  # Generate a summary table of the data uploaded
  output$summary <- renderPrint({
    y <- data()
    summary(y)

  })

}

# Create Shiny app
shinyApp( ui = ui, server = server)
library(shiny)
library(ggplot2)


shinyApp(
  ui = fluidPage(
    tabsetPanel(
      tabPanel(
    "Upload File",
    titlePanel("Uploading Files"),

      fileInput(
        inputId = "file1",
        label = "Choose CSV File",
        multiple = FALSE,
        accept = c("text/csv", "text/comma-separated-values, text/plain", ".csv")
      )),

      tabPanel(
        "Plot",
        pageWithSidebar(
          headerPanel("Plot your data"),
          sidebarPanel(
            selectInput("xcol", "X Variable", ""),
            selectInput("ycol", "Y Variable", "", selected = "")
          ),
          mainPanel(plotOutput("MyPlot"))
        )
      )))

  ,

  server <- function(input, output, session) {
    # added "session" because updateSelectInput requires it

    data <- reactive({
      req(input$file1) # require that the input is available

      df <- read.csv(input$file1$datapath)#,
      # no such inputs in your UI
      #   header = input$header,
      #   sep = input$sep,
      #   quote = input$quote
      # )


      updateSelectInput(session,
        inputId = "xcol", label = "X Variable",
        choices = names(df), selected = names(df)[sapply(df, is.numeric)]
      )
      updateSelectInput(session,
        inputId = "ycol", label = "Y Variable",
        choices = names(df), selected = names(df)[sapply(df, is.numeric)]
      )

      return(df)
    })

    output$contents <- renderTable({
      data()
    })

    output$MyPlot <- renderPlot({
      x <- data()[, c(input$xcol, input$ycol)]

      p <- ggplot(x, aes_string(input$xcol, input$ycol))
      p <- p + geom_line() #+ geom_point()
      print(p)

      # plot(mydata, type = "l",
      #      xlab = input$xcol,
      #      ylab = input$ycol)
    })

    # Generate a summary table of the data uploaded
    output$summary <- renderPrint({
      y <- data()
      summary(y)
    })
  }
)