Access Input from another R file

Hi All,

Can anyone help me in accessing input from another R file . I am trying to call df based on input selected from the filters

app.R

library(shinydashboard)
library(shiny)
library(dplyr)
source("fun.R")


ui <- navbarPage('TEST', id='page', collapsible =TRUE, inverse=FALSE,
                 # define a message handler that will receive the variables on the client side
                 # from the server and update the page accordingly.
                 tags$head(tags$script("
        Shiny.addCustomMessageHandler('updateSelections',
            function(data) {
                var nav_ref = '#page a:contains(\"' + data.nav + '\")';
                var tabpanel_id = data.nav == 'Alpha' ? '#alpha_tabs' : '#beta_tabs';
                var tab_ref = tabpanel_id + ' a:contains(\"' + data.tab + '\")';
                $(nav_ref).tab('show');
                $(tab_ref).tab('show');
            }
        )
    ")),
                 tabPanel('Alpha',
                          tabsetPanel(id='alph`a_tabs',
                                      tabPanel('Tab')
                          )
                 ),
                 tabPanel('Beta',
                          tabsetPanel(id='beta_tabs',
                                      tabPanel('Golf'),
                                      tabPanel('Hotel',
                                               selectInput("beverage", "Choose a beverage:", choices = df_fil()$a),
                                               selectInput("beverage1", "ch", choices = df_fil()$b)
                                      )
                          )
                 )
)



server <- function(input, output, session) {
  
  # df_fil <- function(){
  #   return(df %>% filter(a %in% input$beverage))
  # }
  
  observe({
    data <- parseQueryString(session$clientData$url_search)
    session$sendCustomMessage(type='updateSelections', data)
    updateSelectInput(session, 'beverage', selected=data$beverage)
    updateSelectInput(session, 'beverage1', selected=data$beverage1)
  })
  
}

shinyApp(ui, server, enableBookmarking = "url")

fun.R

library(dplyr)
library(shiny)
source("app.R")

df <- data.frame(a = c("Tea", "Coffee", "Cocoa","Tea", "Coffee", "Cocoa"), b = c(1,2,3,4,5,6))

df_fil <- function(){
  df[df$a == input$beverage,]
}

First: your app.R and fun.R source each other, this will cause an infinite loop.

Second: this article may be helpful: Shiny - Scoping rules for Shiny apps

It wouldn't make sense for a function defined at the top level (as df_fil is) to be able to access input, which is a parameter of the server function. Instead, one way to go is for df_fil to take an input argument, which you'd pass in when calling it from your server function.

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.