我知道这个问题已经发布过几次了,但这是我第一次开发“ Shiny”的东西,而我对一些不同的东西感到困惑。其中之一是正确输入数据帧并在输出函数中使用它。
我目前唯一的目标是:
根据用户选择显示标题或完整数据框
我有一个名为status的二进制列(状态为“通过”或“失败”)。我想按日期分组以计算状态(任何人都可以做)并作图。
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(readxl)
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Data Quality Result Monitoring"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose xlsx file',
accept = c(".xlsx")
),
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)
),
radioButtons("disp", "Display",
choices = c(Head = "head",
All = "all"),
selected = "head")
),
# Show a plot of the generated distribution
mainPanel(
#plotOutput("linechart"),
h4("Observations"),
tableOutput("contents")
)
)
# Define server logic required to draw a histogram'
library(ggplot2)
server <- function(input, output) {
df <- reactive({
inFile <- input$file1
if (is.null(inFile))
return(NULL)
df <- read_xlsx(inFile$datapath, sheet = 1)
return(inFile)})
output$linechart <- renderPlot({
ndf() <- group_by(df,Execution_Date) %>% summarize( count = n() )
ggplot(ndf()) + geom_bar(aes(x=week,y=count),stat="identity")
})
output$contents <- renderTable({
# input$file1 will be NULL initially. After the user selects
# and uploads a file, head of that data file by default,
# or all rows if selected, will be shown.
dataset() <- df
if(input$disp == "head") {
return(head(dataset()))
}
else {
return(dataset())
}
})
}
# Run the application
shinyApp(ui = ui, server = server)
答案 0 :(得分:0)
这是由于ndf() <- group_by(df,Execution_Date) %>% summarize( count = n() )
ndf()
是不存在的NULL函数。
df
是反应式的,您可以将其与df()
而不是df
一起使用,这意味着每次反应性更改时都会对代码进行评估。
答案 1 :(得分:0)
dataset() <- df
这是您收到错误的地方:
"Error in <-: invalid (NULL) left side of assignment"
您不能为反应表达式分配值。反之亦然:
dataset <- df()
使用print
函数来解决这个问题。
您的代码中的另一个错误是:
df <- read_xlsx(inFile$datapath, sheet = 1)
return(inFile)
返回错误的变量,要返回df。
以下是适合您的代码:
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(readxl)
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Data Quality Result Monitoring"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose xlsx file',
accept = c(".xlsx")
),
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)
),
radioButtons("disp", "Display",
choices = c(Head = "head",
All = "all"),
selected = "head")
),
# Show a plot of the generated distribution
mainPanel(
#plotOutput("linechart"),
h4("Observations"),
tableOutput("contents")
)
)
# Define server logic required to draw a histogram'
library(ggplot2)
server <- function(input, output) {
df <- reactive({
inFile <- input$file1
if (is.null(inFile))
return(NULL)
df <- read_xlsx(inFile$datapath, sheet = 1)
df
})
output$linechart <- renderPlot({
ndf <- group_by(df(),Execution_Date) %>% summarize( count = n() )
ggplot(ndf + geom_bar(aes(x=week,y=count),stat="identity"))
})
output$contents <- renderTable({
# input$file1 will be NULL initially. After the user selects
# and uploads a file, head of that data file by default,
# or all rows if selected, will be shown.
dataset <- df()
if(input$disp == "head") {
return(head(dataset))
}
else {
return(dataset)
}
})
}
# Run the application
shinyApp(ui = ui, server = server)
我还建议您对代码中的结构和名称进行检查。