r - Create DataTable in Shiny -
i have dataframe (df). want users enter phrase in shiny. shiny app takes last 2 words of phrase , filters df according trigrams there. output (a datatable) should displayed in user interface.
however, server function not find lasttwo()
(and maybe not lastone()
). can tell me why?
futhermore, how can extract column x3
in df1
, print in ui?
require(dplyr) # data wrangling require(stringi) # string/text processing require(stringr) # extracting words require(shiny) df <- structure(list(term = c("one of the", "a lot of", "thanks the", "to a", "going be", "i want to", "out of the", "the end of", "it a", "as as", "some of the", "be able to", "part of the", "i have a", "i have to", "the rest of", "looking forward to", "is going to", "thank for", "this a"), freq = c(3418, 2969, 2322, 1851, 1749, 1518, 1514, 1466, 1431, 1376, 1372, 1313, 1230, 1213, 1093, 1084, 1078, 1054, 1030, 1013), share = c(0.000925656110901077, 0.000804058804349121, 0.000628839522970246, 0.000501284219215299, 0.000473660777637795, 0.000411101807006388, 0.000410018534787662, 0.000397019268162954, 0.000387540636249105, 0.000372645643241627, 0.000371562371022901, 0.000355584105796698, 0.000333106207258141, 0.000328502300328556, 0.000296004133766787, 0.000293566771274654, 0.000291941862946565, 0.000285442229634211, 0.000278942596321858, 0.000274338689392273), x1 = c("one", "a", "thanks", "to", "going", "i", "out", "the", "it", "as", "some", "be", "part", "i", "i", "the", "looking", "is", "thank", "this"), x2 = c("of", "lot", "for", "be", "to", "want", "of", "end", "was", "well", "of", "able", "of", "have", "have", "rest", "forward", "going", "you", "is"), x3 = c("the", "of", "the", "a", "be", "to", "the", "of", "a", "as", "the", "to", "the", "a", "to", "of", "to", "to", "for", "a")), .names = c("term", "freq", "share", "x1", "x2", "x3"), row.names = c(na, 20l), class = "data.frame") server <- function(input, output) { tokens <- reactive({ token <- tolower(input$sentence) token <- gsub("[^[:alnum:]['-]", " ", token) token <- gsub("^\\s+|\\s+$", "", token) }) output$lastone <- renderprint({ word(tokens(), -1) }) output$lasttwo <- renderprint({ word(tokens(), -2) }) output$table <- renderdatatable({ df1 <- df %>% filter(x1 == lasttwo() & x2 == laston()) return(df1) }) } ui <- navbarpage("filter", tabpanel("the app", column(8, offset = 4, textinput(inputid = "sentence", label = "enter phrase"), submitbutton("filter") ), fluidrow( datatableoutput('table') ) ), tabpanel("how use") ) shinyapp(ui = ui, server = server)
first, lastone
, lasttwo
should reactive such tokens
. still not run on computer since in code not specify package containing word
command.
Comments
Post a Comment