【问题标题】:ggplot histogram fails inside shiny appggplot histogram 在闪亮的应用程序中失败
【发布时间】:2018-05-15 20:51:41
【问题描述】:

我正在尝试在闪亮的应用程序中绘制一个简单的直方图。在应用程序外部(在 R 脚本中),图表绘制没有任何问题,(see here)但相同的代码从应用程序内部生成一个看起来很奇怪的图表(see the wrong graph here

你能帮我找出问题所在吗?数据集链接:https://drive.google.com/open?id=1ITK0lTWm_mkb9KonHLq4nuKDv-PBUDeR

library(ggplot2)
library(ggthemes)
library(scales)
library(shiny)

# read data
cso_corruption <- read.csv("cso_corruption.csv", header = T, sep = ",")

ui <- fluidPage(
   sidebarLayout(
     sidebarPanel(
       radioButtons(inputId = "x",label ="Measurement Type",choices = 
                      c("freq_business", "freq_cso"), selected = NULL)
                  ),

     mainPanel(
       plotOutput(outputId = "hist")
      )
   )
)

server <- function(input, output) {

  output$hist <- renderPlot(ggplot(data = na.omit(cso_corruption), aes(x=input$x)) +
                                 geom_histogram(fill="lightgreen", stat="count"))
}

shinyApp(ui = ui, server = server)

【问题讨论】:

  • 为什么不发布您的输出/日志或我们可以用来帮助您的任何其他信息?
  • 当您说“代码失败”时,究竟是什么意思?您是否收到错误消息?如果是这样,您也需要共享该信息。
  • @ytpillai - 我已经编辑了帖子并添加了图表。请帮助
  • @MrFlick - 没有错误消息,只是一个错误的情节。我现在已经在帖子中添加了图表。
  • @umbi 不,我不想。 JK,顺便说一句,您使用的直方图将条形图粘在一起。我建议您检查一下您没有在 renderPlot 中复制数据

标签: r ggplot2 shiny histogram


【解决方案1】:

可能的原因是数据没有进入服务器。

我会将 csv 放入目录中的另一个文件中,以便您可以使用

访问它
read.csv("./Data/projectdata.csv")

因此,它不会在您发布时丢失。还要确保在发布时检查数据。最后一点是在服务器中包含 read.csv 函数。

【讨论】:

  • 谢谢,但这里的问题不在于发布。当我在 RStudio 上运行代码时,该图根本不起作用。我已经尝试在服务器中更改目录和读取 csv - 没有帮助......而且我没有收到任何错误消息,而只是一个错误的情节。
【解决方案2】:

经过多次反复试验,我找到了解决方案。诀窍是,在服务器中,我使用 aes_string 而不是 aes。我还没有弄清楚为什么 aes_string 在这里工作,因为它应该要求变量在引号中传递。但它出于某种原因起作用。

【讨论】:

    猜你喜欢
    • 2016-12-20
    • 1970-01-01
    • 2013-07-02
    • 1970-01-01
    • 2013-07-08
    • 2016-10-06
    • 2020-07-02
    • 2018-05-21
    • 2016-10-15
    相关资源
    最近更新 更多