我有以下代码,其中我在理解反应性方面遇到了麻烦
以下是我的UI.R
library(shiny)
library(shinydashboard)
header <- dashboardHeader(
title = sDashBordTitle
)
sidebar<-dashboardSidebar(
sidebarMenuOutput("menu")
)
body <- dashboardBody(
tabItems(
tabItem(tabName = "btnData",
tabsetPanel(
tabPanel(title="Home Page",
tags$br(),
tags$br(),
tags$br(),
tags$br(),
fluidRow(
column(
width=12,
box(
width = 12,title = sHomeDataTitle,status = "primary",solidHeader = T,collapsible = F,collapsed = F,
fluidRow(
column(width=2,
###########Add some label here##########
div(id="BrandDiv","Brand Information",style="height:200px;font-size:200%;color:black;border-right:1px solid")
),
column(width=4,
textInput(inputId="sIdBrandName",label="Enter the brand Name",width = "200px"),
actionBttn(inputId="btnAddBrandToList",label = "Add brand",style = "unite",color = "primary",size = "md"),
tags$br(),
tags$br(),
uiOutput("ddbBrandDropdDown")
),
column(width=2,
div(id="InstanceDiv","Instance Information",style="height:200px;font-size:200%;color:black;border-right:1px solid")
),
column(width=4,
##########Add instance text box and instance dropdown############
textInput(inputId="sIdInstanceName",label="Enter the Instance Name",width = "200px"),
actionBttn(inputId="btnAddInstanceToList",label = "Add Instance",style = "unite",color = "primary",size = "md"),
tags$br(),
tags$br(),
uiOutput("ddbInsatnce")
)
),
#############Adding row which will contains the OK button################
fluidRow(
column(width=12,
column(width=2),
column(width=6,
actionBttn(inputId = "btnOK",label="Continue",style = "fill",color = "primary",size = "md",block = T)
),
column(width=2)
)
)
)
)
)
),
tabPanel(title="Permissions"
)
)
),
tabItem(tabName = "btnSummary",
h2("this if for summary")
)
)
)
shinyUI(
dashboardPage(
dashboardHeader <- header,
dashboardSidebar <- sidebar,
dashboardBody <- body
)
)
以下是server.R
library(data.table)
library(DT)
library(shinyWidgets)
library(shiny)
library(shinythemes)
library(shinydashboard)
library(shinyjs)
# Define server logic required to draw a histogram
shinyServer(function(input, output,session) {
observe({
sNewBrand<<-input$sIdBrandName
sNewInstance<<-input$sIdInstanceName
sBrandNameSelectedInBrandNameDropDown<<-input$ddbBrandNameDropDown
})
###############################################################
# Below function creates the sidebar menu based on the booolean
# Need to update the function
###############################################################
output$menu<-renderMenu({
if(blnDisplaySideBar){
sidebarMenu(
menuItem(tabName = "btnData",text="Data",icon=icon("home")),
menuItem(tabName = "btnSummary",text="Summary",icon=icon("bar-chart"))
)
}
})
##############################################################################################################################
# Below function create the dropdown for the Brand, It filters the User-Brand-Scneario file on the logged in User Id
# and uses the unique values of brand to populate the dropdown
# Also, it removes empty string or NA from the dropdpwn if found in the file
##############################################################################################################################
output$ddbBrandDropdDown<-renderUI({
VBrandAccessibleByUser<-fcnReturnAccessibleBrandListByLoggedInUser()
if(is.null(VBrandAccessibleByUser)){
return(NULL)
}else{
selectInput(inputId="ddbBrandNameDropDown",label = "Select Brand",choices = VBrandAccessibleByUser,width = "200px",
selected = VBrandAccessibleByUser[1])
}
})
##############################################################################################################################
# Below function creates the dropdown for the Scenario. It filters the User-Brand-Scenario file on the logged in User Id and
# the brand value selected in the brand dropdown and uses the unique values of scenarios to populate the dropdown
# Also, it removes empty string or NA from the dropdown if found in the file
# P.S- below function is also reactive as it depends on the input values of the brand dropdown
##############################################################################################################################
output$ddbInsatnce<-renderUI({
sBrandName<-input$ddbBrandNameDropDown
vInstanceAccesibleByUser<-fcnReturnInstancesAccesibleByUserForABrand(sBrandName)
if(is.null(vInstanceAccesibleByUser)){
return(NULL)
}else{
selectInput(inputId = "ddbnstanceNameDropDown",label="Select Instance",choices=vInstanceAccesibleByUser,width="200px",
selected = vInstanceAccesibleByUser[1])
}
})
#############################################################################################################################
# Below observe event runs when the user clicks on Add brand button.
# It then appends the added Brand value to the old list and also selects the added brand value in the dropdown
# It does not do anything if Add brand button is clicked with no brand value in it
# Also, it updates the global brand data table with the user ID, Brand Name and Sceanrio fields with
# logged in User ID, entered brand Name and Null with the instance respecitively
#############################################################################################################################
observeEvent(input$btnAddBrandToList,
{
if(sNewBrand==""){
}
else{
vBrandAccessibleByUser<-fcnReturnAccessibleBrandListByLoggedInUser()
if(is.null(vBrandAccessibleByUser)){
vNewChoices<-c(sNewBrand)
output$ddbBrandDropdDown<-renderUI({
selectInput(inputId = "ddbBrandNameDropDown",label = "Select Brand",choices = vNewChoices,
selected = sNewBrand)
})
}else{
vNewChoices<-append(vBrandAccessibleByUser,sNewBrand)
updateSelectInput(session,"ddbBrandNameDropDown",label = "Select Brand",choices = vNewChoices,
selected = sNewBrand)
}
# updateSelectInput(session,"ddbBrandNameDropDown",label = "Select Brand",choices = vNewChoices,
# selected = sNewBrand)
############Update the global brand data table########################3
dtuserFileSceanrio<<-rbindlist(list(dtuserFileSceanrio,list(sLoggedInUser,sNewBrand,NA)))
print("test")
#write_csv(dtUpdated,"../Admin files/Userscenariomapping.csv")
}
}
)
#############################################################################################################################
# Below observe event runs when the user clicks on Add Instance button
# It then appends the added instance valye to the old list and also selects the added instance value in the dropdown
# It does not do anything if Add instance button is cliceked with no instance value in it
# Also, it updates the global brand data table with the User ID, Brand Name and Scenario Name fields with
# logged in User ID, brand name selected in brand name dropdown and entered instance name respectively
#############################################################################################################################
observeEvent(input$btnAddInstanceToList,
{
if( sNewInstance == "" || sBrandNameSelectedInBrandNameDropDown == "" ){
return(NULL)
}else{
vInstanceAccesibleByUser<-fcnReturnInstancesAccesibleByUserForABrand(sBrandNameSelectedInBrandNameDropDown)
if(is.null(vInstanceAccesibleByUser)){
vNewInstanceChoice=c(sNewInstance)
output$ddbInsatnce<-renderUI({
selectInput(inputId = "ddbnstanceNameDropDown",label = "Select Instance",choices = vNewInstanceChoice,
selected = sNewInstance)
})
}else{
vNewInstanceChoice=append(vInstanceAccesibleByUser,sNewInstance)
updateSelectInput(session,"ddbnstanceNameDropDown",label = "Select Instance",choices = vNewInstanceChoice,
selected = sNewInstance)
}
# updateSelectInput(session,"ddbnstanceNameDropDown",label = "Select Instance",choices = vNewInstanceChoice,
# selected = sNewInstance)
############Update the global brand data table########################
dtuserFileSceanrio<<-rbindlist(list(dtuserFileSceanrio,list(sLoggedInUser,sBrandNameSelectedInBrandNameDropDown,sNewInstance)))
print("test")
# write_csv(dtUpdated,"../Admin files/Userscenariomapping.csv")
}
}
)
})
1)在加载应用程序时,我很难理解server.R的代码执行(函数调用顺序)。
2)如果我更改了Brand
下拉菜单,那么我的instance
下拉列表会得到反应更新,但是一旦为observeevent
按钮调用Add brand
代码,那么Brand之间的反应性下拉列表和实例下拉列表停止工作
非常感谢任何帮助。
提前致谢
以下是我的global.R
library(data.table)
library(DT)
library(shinyWidgets)
library(shiny)
library(shinythemes)
library(shinydashboard)
library(shinyjs)
library(readr)
sDashBordTitle<-"Market-Mix"
blnDisplaySideBar<-TRUE
sHomeDataTitle<-"Market-Mix"
sLoggedInUser<-"A"
############Source R scripts################
source("GenericCode.R")
##################Read the User-Brand-Scenario file################
dfuserFileScenario<-read_csv("../Admin files/Userscenariomapping.csv")
dtuserFileSceanrio<-as.data.table(dfuserFileScenario)
sNewBrand<-""
sNewInstance<-""
sBrandNameSelectedInBrandNameDropDown<-""