我正在尝试构建我的第一个闪亮的应用程序。我可以上传一个带有我创建的简单数据集的.csv文件,并在第一个选项卡中很好地显示该表。然后,我尝试使用相同的数据来使用ggplot2创建一个条形图。在光亮的外面,酒吧的情节完美地产生。但在应用程序中,它生成一个只有一个条形图,而不是表示每一个“宠物”类型的计数的条形图,就像它应该的那样,只是一个表示所有行数(30)的条形图。我不知道为什么它不计算每一种“宠物”在闪闪发光,请帮助?
我试过使用is.character和is.factor
脚本使用的数据列是30行不同类型的宠物(例如狗、猫、鸟、蜥蜴)。最后的情节应该是一个条形图,每种宠物都有计数。
library(shiny)
library(dplyr)
library(ggplot2)
library(readr)
我的UI代码:
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)
发布于 2018-12-26 08:50:13
试试这个:
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
来允许使用刺或引号来定义美学映射。注意,input$xcol
返回的是"pet"
而不是pet
。https://stackoverflow.com/questions/53924642
复制相似问题