首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >为什么当我用上传的数据在我的r发亮的应用程序中绘制我的ggplot2条形图时,我只能得到一个实心条呢?

为什么当我用上传的数据在我的r发亮的应用程序中绘制我的ggplot2条形图时,我只能得到一个实心条呢?
EN

Stack Overflow用户
提问于 2018-12-25 10:17:57
回答 1查看 523关注 0票数 0

我正在尝试构建我的第一个闪亮的应用程序。我可以上传一个带有我创建的简单数据集的.csv文件,并在第一个选项卡中很好地显示该表。然后,我尝试使用相同的数据来使用ggplot2创建一个条形图。在光亮的外面,酒吧的情节完美地产生。但在应用程序中,它生成一个只有一个条形图,而不是表示每一个“宠物”类型的计数的条形图,就像它应该的那样,只是一个表示所有行数(30)的条形图。我不知道为什么它不计算每一种“宠物”在闪闪发光,请帮助?

我试过使用is.character和is.factor

脚本使用的数据列是30行不同类型的宠物(例如狗、猫、鸟、蜥蜴)。最后的情节应该是一个条形图,每种宠物都有计数。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
library(shiny)
library(dplyr)
library(ggplot2)
library(readr)

我的UI代码:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
ui <- fluidPage(

# App title ----
titlePanel("Magic Pets"),

# Sidebar layout with input and output definitions ----
sidebarLayout(

# Sidebar panel for inputs ----
sidebarPanel(

  # Input: Upload .csv 
  fileInput('file1', 'Choose .CSV File',
            accept=c('text/csv',
                     'text/comma-separated-values,text/plain',
                     '.csv')),


  selectInput("xcol", "Pets", "", selected = "")),


# Main panel for displaying outputs ----
mainPanel(

  # Output: Tabset for data table and plot
  tabsetPanel(type = "tabs",
              tabPanel("Pet Data", DT::dataTableOutput('contents')),
              tabPanel("Lucky Pet Plot", plotOutput("MyPlot"))

      )
     )
    )
   )

我的服务器代码:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
server <- function(input, output, session) {

output$MyPlot <- renderPlot({
z <- myData()[,input$xcol]

p <- ggplot(z, aes(x=input$xcol, fill = input$xcol)) + 
geom_bar(stat="count")
print(p)

})

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


df <- read_csv(input$file1$datapath) 


updateSelectInput(session,
                  inputId = "xcol", label = "Pets",
                  choices = names(df), selected = names(df)[sapply(df, 
is.character)])

return(df)
})


# Generate an HTML table view of the data ----

output$contents <- DT::renderDataTable({
DT::datatable(myData()) 
})
}

shinyApp(ui, server)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-26 00:50:13

试试这个:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
library(shiny)
library(dplyr)
library(ggplot2)
library(readr)

ui <- fluidPage(

  # App title ----
  titlePanel("Magic Pets"),

  # Sidebar layout with input and output definitions ----
  sidebarLayout(

    # Sidebar panel for inputs ----
    sidebarPanel(

      # Input: Upload .csv 
      fileInput('file1', 'Choose .CSV File',
                accept=c('text/csv',
                         'text/comma-separated-values,text/plain',
                         '.csv')),


      selectInput(inputId = "xcol", label = "Pets", choices = "", selected = "")

      ),


    # Main panel for displaying outputs ----
    mainPanel(

      # Output: Tabset for data table and plot
      tabsetPanel(type = "tabs",
                  tabPanel("Pet Data", DT::dataTableOutput('contents')),
                  tabPanel("Lucky Pet Plot", plotOutput("MyPlot"))

      )
    )
  )
)

server <- function(input, output, session) {
  myData <- reactive({
    req(input$file1) # require that the input is available

    df <- read_csv(input$file1$datapath) 

    return(df)

  })

  observe({ # observe is similar to recative expect that it doesn't yield results
    updateSelectInput(session,
                      inputId = "xcol", 
                      label = "Pets",
                      choices = names(myData()),
                      selected = names(myData())[1] # select 1st column name
    )

  })

  output$MyPlot <- renderPlot({
    #z <- myData()[,input$xcol] #not needed

    p <- ggplot(myData(), aes_string(x=input$xcol, fill=input$xcol)) + 
      # aes_string allows you to use strings or quoted names/calls to define the aesthetic mappings
                  geom_bar(stat="count")

    print(p)

  })

  # Generate an HTML table view of the data ----

  output$contents <- DT::renderDataTable({
    DT::datatable(myData()) 
  })

}

shinyApp(ui, server)

有两件事要注意:

  1. 使用observe (类似于reactive,但不产生结果)更新输入
  2. 使用aes_string而不是aes来允许使用刺或引号来定义美学映射。注意,input$xcol返回的是"pet"而不是pet
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53924642

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文