我正在尝试构建我的第一个Shiny应用程序。我可以上传带有创建的简单数据集的.csv文件,并在第一个标签中显示该表格。然后,我尝试使用相同的数据使用ggplot2创建条形图。在Shiny之外,条形图可以完美生成。但是在应用程序内部,它会生成一个带有一个条形图的图形,而没有一个条形图代表每个“宠物”类型的计数,而是一个仅代表所有行数的条形图(30)。我不确定为什么它不将Shiny中的每种“宠物”计算在内,请帮忙吗?
我尝试使用is.character和is.factor
脚本使用的数据列是30行不同类型的宠物(例如狗,猫,鸟,蜥蜴)。最终图应为条形图,其中应包含这些宠物的每种类型的数量。
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("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"))
)
)
)
)
我的服务器代码:
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)
答案 0 :(得分:0)
尝试一下:
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)
需要注意的两件事:
使用observe
(类似于reactive
,但不产生结果)来更新输入
使用aes_string
而不是aes
来允许使用ing或带引号的名称来定义美观映射。请注意,input$xcol
返回"pet"
而不是pet
。