【发布时间】:2020-12-05 11:44:18
【问题描述】:
在dashboardBody 的tabPanel-“tab2”title="plot" 中,我有一个selectInput 对象,它的选择基于来自服务器的dataTable 输出“contents2”。这样做时,我没有在 selectInput 对象的下拉菜单中填充任何选项,而且当我尝试根据 selectInput 的选择绘制直方图时,我收到错误消息:“找不到对象‘contents2’” 请有人在这里指导我。
library(shinyWidgets)
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(title="Test"),
dashboardSidebar(
sidebarMenu(id = 'sbar', verbatimTextOutput("text1"),
menuItem("File Selection", tabName = 'page1', icon = icon('line-chart'),
fileInput("file1", "Select CSV File", accept = c("text/csv","text/comma-
separated-values,text/plain",".csv")),
menuSubItem(actionButton(inputId="next1", label="NEXT"), tabName="next",
icon="") ),
menuItem('File Edit', tabName = 'page2',icon = icon('line-chart')),
menuItem('Section 3',tabName = 'page3',icon = icon('line-chart')) )
),
dashboardBody(
tabItems(
tabItem(tabName = "next",fluidRow(
tabBox(id = "tabset1", height = "650px", width=12,
tabPanel("Input Data", value="tab1", " ",
# fluidRow(tags$head(tags$style(HTML(" label {float:left;} "))),
radioGroupButtons("disp", "",label=NULL,
choices = c('Display head data'="head",'Display entire
data'="all"), selected=NULL),
fluidRow(DT::dataTableOutput("contents1"),style = "height:500px;
overflow-y: scroll;overflow-x: scroll;",
title = "Dashboard example") ),
tabPanel("Plot", value="tab2", " ",
selectInput("select1","Select Variable for display",choices =
c(colnames(DT::dataTableOutput("mydata")))),
fluidRow(plotOutput("plot1"))),
tabPanel("tab3 title", value='tab3', " ",
valueBoxOutput('tab3_valuebox'))
) ) ),
tabItem(tabName="page2", fluidRow(
tabBox(id = "tabset2", height = "650px", width=12, title = "My Page2 info",
tabPanel("Input Data", value="tab1", " ",
fluidRow(DT::dataTableOutput("contents2"))),
tabPanel("Plot", value="tab2", " ",
fluidRow(plotOutput("plot2")) )
) ) ) ) ) )
server <- function(input, output, session) {
observeEvent(input$next1, {
updateTabItems(session, "sbar", "next")
req(input$next1)
if (input$next1 == 0) {
return(NULL)
}else if (input$next1 == 1 & is.null(input$file1)) {
return(NULL)
}else {
inFile <- input$file1
myfile <- read_csv(inFile$datapath)
output$contents1 <- renderDataTable({
if(input$disp == "head") {
return(head(myfile))
}else {
return(myfile) }})
output$contents2 <- renderDataTable({
myfile }) }
})
observe(input$select1)
output$text1 <- renderText(print(input$sbar))
output$plot1 <- renderPlot({hist(contents2$input$select1)})
output$plot2 <- renderPlot({hist(rnorm(20))})
output$tab3_valuebox <- renderValueBox({
valueBox('2020',subtitle = "Need to use this in future",icon = icon("car"),
color = "red") })
}
shinyApp(ui, server)
【问题讨论】:
-
您需要在
renderUI中的服务器端处理selectInput,然后在ui中处理uiOutput。
标签: shiny shinydashboard shiny-server shinyapps shiny-reactivity