Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201867

Two Reactives Error Invalid Argument to Unary Operator

$
0
0

I am trying to build an app that displays a .csv file in table format. The user can choose with radio buttons one of two ways to display the table. I have defined those two ways with the filedata() and data_ranked_words() reactives.

To reproduce this error, please first run this code chunk to get a small subset of my data:

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)

You will see that you get an Error invalid argument to binary operator as soon as the program launches. Then choose test.csv off your filesystem in your working directory and you will see that the error persists while 'Word View' is selected, but the table correctly displays while 'Probability View' is selected.

This app is very simple. The problem occurs in line 66 temp = matrix(row.names(data)[apply(-data, 2, order)], nrow(data)). It doesn't like the -data within the apply. However, try as I might, I have not been able to reproduce this error just working in the R console, outside of shiny. In regular R, this line runs just fine.

What I am trying to do is display two different tables when the user selects the radio buttons. 'Probability View' is the raw table as is, and 'Word View' is the table with some operations on it (lines 61-71). I can't figure this one out!

Here is my app:

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)

Viewing all articles
Browse latest Browse all 201867

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>