Two Reactives R Shiny -
i need render table based on user input 1 of 2 possible tables. have defined first table filedata
user selecting .csv file upload. second table, data_ranked_words
, has same dimensions.
what want output switch between 2 tables. defined each table in reactive()
. however, know data_ranked_words
reactive never being triggered. how trigger both of these reactives when user uploads file? in code issue 2 reactive()
statements @ beginning of server.r
.
library(shiny) library(markdown) library(dt) library(d3tablefilter) options(shiny.maxrequestsize=50*1024^2) setwd('~/desktop/dsi/topic model app interface') # ui.r #------------------------------------------------------------------------------------- ui <- shinyui( navbarpage("start", tabpanel("from data", sidebarlayout( sidebarpanel( radiobuttons("plottype", "plot type", c("scatter"="p", "line"="l") ) ), mainpanel( plotoutput("plot") ) ) ), tabpanel("from csv", sidebarlayout( sidebarpanel( # define what's in sidebar fileinput("file", "choose csv files directory", multiple = true, accept=c('text/csv', 'text/comma-separated-values,text/plain', '.csv')), h5(div(html('use radio butons toggle between <em>word view</em> , <em>probability view</em>.'))), radiobuttons('toggle', 'choose one:', list('word view', 'probability view')), p(div(html('<strong>note:</strong> <em>probability view</em> <u>not</u> yield top x number of words. instead return first x columns. can sort each column in ascending or descending order. keep in mind sort rows displayed, <u>not</u> rows.'))), br(), sliderinput('slider', div(html('how many rows display?')), 1, 100, 20), br(), h5('use buttons below show large numbers of rows.'), radiobuttons('rowidentifier', 'show more rows:', list('[ clear ]', '200', '500', '1000', '5000', '10000', 'all rows')), p(div(html('<strong>warning:</strong> printing rows screen may take while.'))), h3('tips:'), p("you can copy , paste table excel. if want copy 1 column, use 'show/hide' function @ top-right of table hide undesired columns."), p(div(html('sorting column available in <em>probability view</em> not <em>word view</em>.'))) ), # define what's in main panel mainpanel( title = 'topic model viewer', # how wide main table fluidrow( column(width = 12, d3tfoutput('data')) ) ) ) ), navbarmenu("more", tabpanel("temp" ), tabpanel("about", fluidrow( column(6 ), column(3 ) ) ) ) ) ) # server.r #------------------------------------------------------------------------------------- server <- shinyserver(function(input, output, session) { # set dataframe display in table # define 'filedata' .csv file uploaded filedata <- reactive({ infile <- input$file if (is.null(infile)) { # user has not uploaded file yet return(null) } temp = read.csv(infile$datapath) # save data rds file, faster csv saverds(temp, file = 'data.rds') # read in data file data = readrds('data.rds') # transpose data more intuitive viewing. words rows, topics cols data = t(data) # convert data frame data = as.data.frame(data) # return data }) # ranked , ordered csv file data_ranked_words <- reactive({ data = filedata() # sort each column probability, , substitute correct word column # rank each word each topic # done indexing row names order of each column temp = matrix(row.names(data)[apply(-data, 2, order)], nrow(data)) temp = as.data.frame(temp) # define column names (same before) new data frame colnames(temp) = paste0(rep('topic', ncol(data)), 1:ncol(data)) # return temp print('success') }) output$data <- renderd3tf({ # define table properties. see http://tablefilter.free.fr/doc.php # complete reference tableprops <- list( rows_counter = true, rows_counter_text = "rows: ", alternate_rows = true ); # radio buttons # reason why extensions in if() sorting can # activated on probability view, 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) ) } # radio button options more row viewing options if(input$rowidentifier=='clear'){ num_rows = input$slider } else if(input$rowidentifier==200){ num_rows = 200 } else if(input$rowidentifier==500){ num_rows = 500 } else if(input$rowidentifier==1000){ num_rows = 1000 } else if(input$rowidentifier==5000){ num_rows = 5000 } else if(input$rowidentifier==10000){ num_rows = 10000 } else if(input$rowidentifier=='all rows'){ num_rows = nrow(df) } else{ num_rows = input$slider } # create table if(is.null(filedata())){ } else{ d3tf(df, tableprops = tableprops, extensions = extensions, showrownames = true, tablestyle = "table table-bordered") } }) # line end r session when shiny app closed session$onsessionended(stopapp) }) # run app in browser runapp(list(ui=ui,server=server), launch.browser = true)
Comments
Post a Comment