繁体   English   中英

R闪亮中的选择输入

[英]selectInput in R shiny

我想从从 Mysql 查询读入的列表中进行选择。 我在代码中遇到错误。 我一定是在做一些完全错误的事情,但不确定是什么。

我想从从 sql 查询读入的 skus 列表中进行选择。 我在 ui 部分出现错误。

我什至不确定这是否可能,但列出所有 skus 将是非常及时的。

我收到以下错误:

标记错误(“div”,列表(...)):缺少参数“sidebarPanel”,没有默认值

ShinyApp(ui = ui, server = server) 强制错误(ui):找不到对象“ui”

library('RMySQL')
library('plyr')
library('shiny')
library('scales')
library(shinyapps)
library(ggplot2)



con <- dbConnect(MySQL(), user="user", password="password",dbname="DB", host="host");


rank<-dbGetQuery(con,"select sku from DB")




#build a shiny app to select which sku to pick
server <- function(input, output) {

  output$distPlot <- renderPlot({
    hist(rnorm(input$obs), col = 'darkgray', border = 'white')
  })
}

ui <- pageWithSidebar(
  ## Application title

    sidebarPanel(
      sliderInput("obs", "Number of observations:", min = 10, max = 500, value = 100),
      selectInput(
        'e0', '0. An ordinary select input', choices = unique(rank$sku),
        selectize = FALSE
      ),

      mainPanel(plotOutput("distPlot"))

  )

)

shinyApp(ui = ui, server = server)

您的selectize = FALSE附近有一个缺失的括号,并且(如@DavidRobinson 所建议的)您需要一个headerPanel

代码修复

library(shiny)
library(ggplot2)
# con <- dbConnect(MySQL(), user="user", password="password",dbname="DB", host="host");
# rank<-dbGetQuery(con,"select sku from DB")
# for test hard coding the rank as I dont have your data
# test rank
rank$sku <- c(1,2,3)

#build a shiny app to select which sku to pick
server <- function(input, output) {
  output$distPlot <- renderPlot({
    hist(rnorm(input$obs), col = 'darkgray', border = 'white')
  })
}

ui <- pageWithSidebar(
  ## Application title

  # missing headerPanel
  headerPanel(title = "Hello"),

  # missing bracket after selectize
  sidebarPanel(
    sliderInput("obs", "Number of observations:", min = 10, max = 500, value = 100),
    selectInput(
      'e0', '0. An ordinary select input', choices = unique(rank$sku),
      selectize = FALSE) 
    ),

  mainPanel(plotOutput("distPlot"))    
)

shinyApp(ui = ui, server = server)

结果

FixedSidebarShinyWebpageImage

另一个闪亮的页面 UI 选项

您还可以使用选项卡式页面结构,用此代码替换上面的ui (注意它不需要像上面那样的 headerPanel):

# navbar tabbed page example - without headerPanel
ui2 <- navbarPage(title = "Hello Another Style", 
           tabPanel("Chart Panel",
                    sidebarLayout(
                      sidebarPanel(
                        sliderInput("obs", "Number of observations:", 
                                    min = 10, max = 500, value = 100),
                        selectInput(
                          'e0', '0. An ordinary select input', 
                          choices = unique(rank$sku),
                          selectize = FALSE)
                        ),
                      mainPanel(
                        plotOutput("distPlot")
                      )
                    )
           ),
           tabPanel("Instructions",
                    mainPanel(
                       p("Notes here for example...")
                    )
           )        

)

第二个结果

Shiny-NavPanelExamplePage1

然后在第二个面板上......

Shiny-NavPanelExamplePage2

调试建议

这些Shiny页面可以有很多括号,所以在你的代码中仔细选择括号,比如 RStudio,以确保你的括号匹配正常。

祝一切顺利!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM