SelectInput 选项不会根据在 Shiny 中选择的 csv 文件动态填充
Posted
技术标签:
【中文标题】SelectInput 选项不会根据在 Shiny 中选择的 csv 文件动态填充【英文标题】:SelectInput choices are not getting populated dynamically based on csv file chosen in Shiny 【发布时间】: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
。
【参考方案1】:
在服务器端处理 selectInput
并创建一个响应式数据框以供使用。下面的代码有效。
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", " ", uiOutput("selectvar"),
# 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(DTOutput("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)
myfile <- reactive(read_csv(inFile$datapath))
output$contents1 <- renderDataTable(
if(input$disp == "head")
return(head(myfile()))
else
return(myfile()) )
output$contents2 <- renderDT(myfile())
output$selectvar <- renderUI(
req(input$file1)
selectInput("select1", "Select Variable for display",
choices = c(colnames(myfile())))
)
output$plot1 <- renderPlot(hist(myfile()[[input$select1]]))
)
#observe(input$select1)
output$text1 <- renderText(print(input$sbar))
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)
【讨论】:
我尝试执行您共享的上述代码,但出现错误:read_csv 中的错误:找不到函数“read_csv”。请帮忙。 谢谢我修好了。感谢您的帮助。以上是关于SelectInput 选项不会根据在 Shiny 中选择的 csv 文件动态填充的主要内容,如果未能解决你的问题,请参考以下文章
如何在 selectInput R Shiny 中为一长串选项设置值
Shiny - 更改 selectInput() 中的选项数量
基于选项卡面板选择在 R Shiny 中显示/隐藏 selectinput
从 SelectInput (R Shiny) 中的分组选项列表中获取组标签