两个Reactives错误无效运算符的参数

时间:2017-04-08 09:40:55

标签: r csv shiny

我正在尝试构建一个以表格格式显示.csv文件的应用程序。用户可以使用单选按钮选择两种方式之一来显示表格。我已使用filedata()data_ranked_words()被动方式定义了这两种方式。

要重现此错误,请首先运行此代码块以获取我的数据的一小部分:

test = rbind(
  c(0.00000009, 0.00000009, 0.00046605, 0.00015541, 0.00215630),
  c(0.00000016, 0.00137076, 0.00000016, 0.00000016, 0.00000016),
  c(0.00012633, 0.00000014, 0.00000014, 0.00000014, 0.00075729),
  c(0.00000013, 0.00000013, 0.00000013, 0.00000013, 0.00062728)
  )
colnames(test) = c('church', 'appearance', 'restrain', 'parity', 'favor')
rownames(test) = NULL
test = as.data.frame(test)
write.csv(test, 'test.csv', row.names = FALSE)

一旦程序启动,您将看到Error invalid argument to binary operator。然后从工作目录中的文件系统中选择test.csv,您会看到错误仍然存​​在,而Word视图'已选中,但表格正确显示“概率视图”。被选中。

这个程序非常简单。问题出现在第66行temp = matrix(row.names(data)[apply(-data, 2, order)], nrow(data))中。它不像-data中的apply。但是,尽我所能,我还没有能够重现这个错误只是在R控制台工作,除了闪亮。在常规R中,这条线运行得很好。

我想要做的是当用户选择单选按钮时显示两个不同的表。 '概率视图'是原始表格,' Word View'是表格上有一些操作(第61-71行)。我无法解决这个问题!

这是我的应用:

library(shiny)
library(markdown)
library(DT)
library(D3TableFilter)
options(shiny.maxRequestSize=50*1024^2) 

# ui.R
#-------------------------------------------------------------------------------------
ui <- shinyUI(
  navbarPage("Topic Model App v1.0",
             tabPanel("From CSV",
                      sidebarLayout(
                        sidebarPanel(
                          # Define what's in the sidebar
                          fileInput("file",
                                    "Choose CSV files from directory",
                                    multiple = TRUE,
                                    accept=c('text/csv', 
                                             'text/comma-separated-values,text/plain', 
                                             '.csv')),
                          radioButtons('toggle', 'Choose one:',
                                       list('Word View', 'Probability View'))
                          ),
                        # Define what's in the main panel
                        mainPanel(
                          title = 'Topic Model Viewer',
                          # How wide the main table will be
                          fluidRow(
                            column(width = 12, d3tfOutput('data'))
                          )
                        )
                      )
                  )
             )
)


# server.R
#-------------------------------------------------------------------------------------
server <- shinyServer(function(input, output, session) {
  # Set up the dataframe for display in the table
  # Define 'filedata()' as the .csv file that is uploaded
  filedata <- reactive({
    infile <- input$file
    if (is.null(infile)) {
      # User has not uploaded a file yet
      return(NULL)
    }
    # Read in .csv file and clean up
    data = read.csv(infile$datapath)
    data = t(data)
    data = as.data.frame(data)
    colnames(data) = paste0(rep('topic', ncol(data)), 1:ncol(data))
    data = format(data, scientific = FALSE)

    data
  })

  #PROBLEM
  # The ranked and ordered csv file
  data_ranked_words <- reactive({
    # Sort each column by probability, and substitute the correct word into that column
    # This will essentially rank each word for each topic
    # This is done by indexing the row names by the order of each column
    data = filedata()
    temp = matrix(row.names(data)[apply(-data, 2, order)], nrow(data))
    temp = as.data.frame(temp)
    colnames(temp) = paste0(rep('topic', ncol(data)), 1:ncol(data))

    temp
  })

  # Create table
  output$data <- renderD3tf({
    tableProps <- list(
      rows_counter = TRUE,
      rows_counter_text = "Rows: ",
      alternate_rows = TRUE
    );

    # Radio buttons
    # The reason why the extensions are in this if() is so that sorting can be
    # activated on Probability View, but not Word View
    if(input$toggle=='Word View'){
      df = data_ranked_words()
      extensions <-  list(
        list( name = "colsVisibility",
              text = 'Hide columns: ',
              enable_tick_all =  TRUE
        ),
        list( name = "filtersVisibility",
              visible_at_start =  FALSE)
      )
    } else if(input$toggle=='Probability View'){
      df = filedata()
      extensions <-  list(
        list(name = "sort"),  #this enables/disables sorting
        list( name = "colsVisibility",
              text = 'Hide columns: ',
              enable_tick_all =  TRUE
        ),
        list( name = "filtersVisibility",
              visible_at_start =  FALSE)
      )
    }

    if(is.null(filedata())){
    } else{
      d3tf(df,
           tableProps = tableProps,
           extensions = extensions,
           showRowNames = TRUE,
           tableStyle = "table table-bordered")
    }
  })



  # This line will end the R session when the Shiny app is closed
  session$onSessionEnded(stopApp)
})

# Run app in browser
runApp(list(ui=ui,server=server), launch.browser = TRUE)

1 个答案:

答案 0 :(得分:1)

所以这里有几个问题在相互作用,难以诊断:

  • 它正在运行并尝试在定义数据之前执行。避免这种情况的“现代”方法是使用req(input$file) - 现在插入filedata被动反应中。请注意,这将打破执行链,直到在闪亮的ui中定义input$file
  • data = format(data, scientific = FALSE)正在将您的列转换为“AsIs”类型的向量,单一减号命令不知道如何操作。它现在被评为filedata()
  • 为了获得抑制科学记数法的功能,在df显示filedata() d3tf之前,options显示了scipen
  • 注意:我觉得AsIs library(shiny) library(markdown) library(DT) library(D3TableFilter) options(shiny.maxRequestSize=50*1024^2) # ui.R #------------------------------------------------------------------------------------- ui <- shinyUI( navbarPage("Topic Model App v1.0", tabPanel("From CSV", sidebarLayout( sidebarPanel( # Define what's in the sidebar fileInput("file", "Choose CSV files from directory", multiple = TRUE, accept=c('text/csv', 'text/comma-separated-values,text/plain', '.csv')), radioButtons('toggle', 'Choose one:', list('Word View', 'Probability View')) ), # Define what's in the main panel mainPanel( title = 'Topic Model Viewer', # How wide the main table will be fluidRow( column(width = 12, d3tfOutput('data')) ) ) ) ) ) ) # server.R #------------------------------------------------------------------------------------- server <- shinyServer(function(input, output, session) { # Set up the dataframe for display in the table # Define 'filedata()' as the .csv file that is uploaded filedata <- reactive({ req(input$file) infile <- input$file if (is.null(infile)) { # User has not uploaded a file yet return(NULL) } # Read in .csv file and clean up data = read.csv(infile$datapath) data = t(data) data = as.data.frame(data) colnames(data) = paste0(rep('topic', ncol(data)), 1:ncol(data)) # data = format(data, scientific = FALSE) data }) #PROBLEM # The ranked and ordered csv file data_ranked_words <- reactive({ # Sort each column by probability, and substitute the correct word into that column # This will essentially rank each word for each topic # This is done by indexing the row names by the order of each column data = filedata() temp = matrix(row.names(data)[apply(-data, 2, order)], nrow(data)) temp = as.data.frame(temp) colnames(temp) = paste0(rep('topic', ncol(data)), 1:ncol(data)) temp }) # Create table output$data <- renderD3tf({ tableProps <- list( rows_counter = TRUE, rows_counter_text = "Rows: ", alternate_rows = TRUE ); # Radio buttons # The reason why the extensions are in this if() is so that sorting can be # activated on Probability View, but not Word View if(input$toggle=='Word View'){ df = data_ranked_words() extensions <- list( list( name = "colsVisibility", text = 'Hide columns: ', enable_tick_all = TRUE ), list( name = "filtersVisibility", visible_at_start = FALSE) ) } else if(input$toggle=='Probability View'){ df = filedata() df = format(df, scientific = FALSE) extensions <- list( list(name = "sort"), #this enables/disables sorting list( name = "colsVisibility", text = 'Hide columns: ', enable_tick_all = TRUE ), list( name = "filtersVisibility", visible_at_start = FALSE) ) } if(is.null(filedata())){ } else{ d3tf(df, tableProps = tableProps, extensions = extensions, showRowNames = TRUE, tableStyle = "table table-bordered") } }) # This line will end the R session when the Shiny app is closed session$onSessionEnded(stopApp) }) # Run app in browser runApp(list(ui=ui,server=server), launch.browser = TRUE) LinkedList<string> l = new LinkedList<string>(); l.AddLast("Kuku"); l.AddLast("Riku"); l.AddLast("Ok"); List<LinkedListNode<string>> lst = new List<LinkedListNode<string>>(); lst.Add(l.First); lst.Add(l.First.Next); lst.Add(l.Last); string json = JsonConvert.SerializeObject(lst, Formatting.Indented, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Serialize }); File.WriteAllText(@"C:\Student Routine\Data.txt", json);` 在这里不起作用很有意思。不确定为什么会这样,但这个List<LinkedListNode<object>>类可以解决问题。

以下是经过调整的代码:

Private Sub Form_AfterUpdate()
    Me!txtTimeIn.Requery
End Sub

Private Sub Form_Current()
    Me!txtTimeIn.Value = TimeValue(Me!TimeIn.Value)
End Sub

Private Sub txtTimeIn_AfterUpdate()
    Me!TimeIn.Value = Format(Me!TimeIn.Value, "hh\:mm\:ss")
End Sub

Private Sub txtTimeIn_BeforeUpdate(Cancel As Integer)
    On Error GoTo txtTimeIn_BeforeUpdate_Error
    Debug.Print Format(Me!txtTimeIn.Value, "hh\:mm\:ss")
    Me!TimeIn.Value = Format(Me!txtTimeIn.Value, "hh\:mm\:ss")
    Exit Sub

txtTimeIn_BeforeUpdate_Error:
    MsgBox "Start Time appears to be invalid."
    Cancel = True
End Sub

这是一个正在运行的屏幕截图:

enter image description here

enter image description here

相关问题