r代码-选择带因子变量的输入

r代码-选择带因子变量的输入,r,shiny,radio-button,histogram,R,Shiny,Radio Button,Histogram,我正在构建一个闪亮的应用程序。在下拉菜单中,我有一个因子变量的类别。我想问题出在服务器上,但我不知道如何解决 此外,我想在直方图中添加一条垂直线,当所选颜色为黄色时为15,当直方图中所选颜色为红色时为20。你能帮我查一下密码吗? 谢谢 库(闪亮) #创建假数据帧 类别看起来您对数据的子集设置不正确。我为数据子集创建了一个反应式表达式:data2(),并使用该表达式进行绘图输出。我还用if(){…}else{…}语句添加了您提到的垂直线 library(shiny) # Creating a f

我正在构建一个闪亮的应用程序。在下拉菜单中,我有一个因子变量的类别。我想问题出在服务器上,但我不知道如何解决

此外,我想在直方图中添加一条垂直线,当所选颜色为黄色时为15,当直方图中所选颜色为红色时为20。你能帮我查一下密码吗? 谢谢

库(闪亮)
#创建假数据帧

类别看起来您对
数据的子集设置不正确。我为数据子集创建了一个反应式表达式:
data2()
,并使用该表达式进行绘图输出。我还用
if(){…}else{…}
语句添加了您提到的垂直线

library(shiny)

# Creating a fake data frame
categories <- c("A", "B", "c")
values <- c(12, 15, 20)
data <- merge(categories, values)

# Define UI for application 
ui <- shinyUI(fluidPage(

  # Title panel
  titlePanel(title = h1("Title", align = "center")),
  sidebarLayout(

    # Sidebar panel 
    sidebarPanel(

      # Options
      selectInput(inputId = "xcol", label = "Select", choices = levels(data$x)),
      br(),
      #Colours histogram
      radioButtons(inputId = "colour", label = strong("Select the colour of 
                                                      histogram"), choices = c("Yellow", "Red"), selected = "Yellow"),
      br(),
      #Bins for histogram
      sliderInput(inputId = "bins", label = "Select the number of Bins for the 
                  histogram", min=5, max = 25, value = 15),
      br(),
      #Density curve
      checkboxInput(inputId = "density", label = strong("Show Density Curve"), 
                    value = FALSE),

      # Display this only if the density is shown
      conditionalPanel(condition = "input.density ==true",
                       sliderInput(inputId = "bw_adjust",
                                   label = "Bandwidth adjustment:",
                                   min = 0.2, max = 3, value = 1, step = 0.2))


      ),

    # Main Panel
    mainPanel(
      #plot histogram
      plotOutput("plot"),

      # Output: Verbatim text for data summary
      verbatimTextOutput("summary"))

      )))

# Define server logic 
server <- shinyServer(function(input, output) {
  data2 <- reactive({data[as.character(data$x)==input$xcol, "y"]})

  output$plot <-renderPlot({
    hist(data2(), breaks = seq(0, max(c(1, data2()), na.rm=TRUE), l= input$bins+1), col = input$colour, 
         probability = TRUE, xlab = "Values", main = "")
    abline(v = mean(data2()), col = "red", lty = 2)
    title(main = input$xcol)

    if (input$density) {
      dens <- density(data2(), adjust = input$bw_adjust)
      lines(dens, col = "blue", lwd = 1)
    }
    if(input$colour=="Red"){
      abline(v=20)}else{abline(v=15)}
    # Generate the summary
    output$summary <- renderPrint({
      #xcol <- xcolInput()
      summary(data2())
    })
  })
})

# Run the application 
shinyApp(ui = ui, server = server)
库(闪亮)
#创建假数据帧

感谢Alex检查我的代码。当我在原始数据集上尝试代码时,我得到了以下错误:'to'必须是有限数。数据集中没有nan值。谢谢Salex,在你的代码中直方图不会改变颜色。ThanksI更新了有关直方图颜色的代码。感谢它为你工作,考虑点击检查标记来调用它回答“否”,错误仍然存在,所以直方图和汇总框不会出现在应用程序中(只有侧栏)。谢谢
library(shiny)

# Creating a fake data frame
categories <- c("A", "B", "c")
values <- c(12, 15, 20)
data <- merge(categories, values)

# Define UI for application 
ui <- shinyUI(fluidPage(

  # Title panel
  titlePanel(title = h1("Title", align = "center")),
  sidebarLayout(

    # Sidebar panel 
    sidebarPanel(

      # Options
      selectInput(inputId = "xcol", label = "Select", choices = levels(data$x)),
      br(),
      #Colours histogram
      radioButtons(inputId = "colour", label = strong("Select the colour of 
                                                      histogram"), choices = c("Yellow", "Red"), selected = "Yellow"),
      br(),
      #Bins for histogram
      sliderInput(inputId = "bins", label = "Select the number of Bins for the 
                  histogram", min=5, max = 25, value = 15),
      br(),
      #Density curve
      checkboxInput(inputId = "density", label = strong("Show Density Curve"), 
                    value = FALSE),

      # Display this only if the density is shown
      conditionalPanel(condition = "input.density ==true",
                       sliderInput(inputId = "bw_adjust",
                                   label = "Bandwidth adjustment:",
                                   min = 0.2, max = 3, value = 1, step = 0.2))


      ),

    # Main Panel
    mainPanel(
      #plot histogram
      plotOutput("plot"),

      # Output: Verbatim text for data summary
      verbatimTextOutput("summary"))

      )))

# Define server logic 
server <- shinyServer(function(input, output) {
  data2 <- reactive({data[as.character(data$x)==input$xcol, "y"]})

  output$plot <-renderPlot({
    hist(data2(), breaks = seq(0, max(c(1, data2()), na.rm=TRUE), l= input$bins+1), col = input$colour, 
         probability = TRUE, xlab = "Values", main = "")
    abline(v = mean(data2()), col = "red", lty = 2)
    title(main = input$xcol)

    if (input$density) {
      dens <- density(data2(), adjust = input$bw_adjust)
      lines(dens, col = "blue", lwd = 1)
    }
    if(input$colour=="Red"){
      abline(v=20)}else{abline(v=15)}
    # Generate the summary
    output$summary <- renderPrint({
      #xcol <- xcolInput()
      summary(data2())
    })
  })
})

# Run the application 
shinyApp(ui = ui, server = server)