【问题标题】:Invalid (NULL) left side of assignment error in ShinyShiny中分配错误的无效(NULL)左侧
【发布时间】:2019-09-18 03:52:15
【问题描述】:

我知道这个问题已经发布了几次,但这是我第一次开发我闪亮的东西,我对一些不同的东西感到困惑。其中之一是正确输入数据帧并在输出函数中使用它。

我现在唯一的目标是:

  1. 根据用户选择显示头部或完整数据帧

  2. 我有一个称为状态的二进制列(状态为通过或失败)。我想按日期分组以计算状态(任何人都可以)并绘制它。

#
# 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)

【问题讨论】:

    标签: r shiny


    【解决方案1】:
    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)
    

    我还建议您检查代码中的结构和名称。

    【讨论】:

      【解决方案2】:

      这是由于ndf() &lt;- group_by(df,Execution_Date) %&gt;% summarize( count = n() )

      ndf() 是不存在的 NULL 函数。

      df 是一个响应式,您可以将它与df() 一起使用而不是df,这意味着每次响应式更改时都会评估代码。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-10-01
        • 2020-08-02
        • 2017-12-30
        • 1970-01-01
        • 1970-01-01
        • 2017-11-08
        • 2023-04-01
        相关资源
        最近更新 更多