简体   繁体   English

从有光泽的renderUI中获取

[英]get from renderUI in shiny

I am building a shiny application for data visualization from a database. 我正在从数据库构建一个闪亮的数据可视化应用程序。 I am trying to run a ggplot using input values from renderUI but gives me an error of 我试图使用来自renderUI的输入值运行ggplot但是给了我一个错误

Error in na.omit(xVariable) : object 'xVariable' not found.

What could be the problem? 可能是什么问题呢?

Server.R Server.R

setwd("~/csv")
shiny.maxRequestSize=30*1024^2

database <- dbConnect(MySQL(), user='user', password='', dbname='database', host='host')
dbResultSet <- dbSendQuery(database, "select * from core") #Query database for information
core<- fetch(dbResultSet, n=-1) #fetch resultset into a data frame
on.exit(dbDisconnect(database)) #Safely close database connection on application close
 shinyServer(function(input, output) {


output$graphData <- renderUI({
    selectInput("dataset", "Choose a dataset:" ,choices = c("pneumonia","core"))

  }) 

  #defining the xVariables for plotting
  #Create variables to be used for graphing based on the data entered
  #datasetInput <- dataset

output$xVariable <- renderUI({
    #Initializes the variable
    selectInput("xVariable","X Variable",names(graphData))
  })
)


  graphType<- renderText({ as.character(input$graphType) }) #graph type
  graphSecondary<- renderText({ as.character(input$graphTypeSecondary) }) #graph type
  xAxis <- reactive ({ input$xVariable })
  yAxis <- reactive ({ input$yVariable })
  xTitle <- renderText ({ as.character(input$xLabel ) })
  yTitle <- renderText ({ as.character(input$yLabel ) })
  legend <- renderText ({ as.character(input$legendTitle) })#graph legend
  #xVar <- as.factor(xVariable)
 # yVar <- as.factor(yVariable)



output$plot <- renderPlot({
     pneumonia.df<- data.frame(pneumonia)  
    c <- ggplot(core, aes(x=na.omit (xVariable) ,fill=xVariable))
    plot <-  c + geom_bar() + xlab(xTitle())+ylab(yTitle())
    # hist(as.numeric(core.df$nresprate))
    print(plot)
    })
 })

ui.R ui.R

shinyUI(pageWithSidebar(
   # Application title.
  headerPanel("Core - Database"),

  sidebarPanel(
    #plotting data sidebar panel
    conditionalPanel(
      "$('li.active a').last().html()==='Graphs'",
      HTML("<div class='span6'>"),
      h4("Primary Plot"),
      tags$hr(),
     # selectInput("dataset", "Choose a dataset:" ,choices = c("pneumonia","core")),
      htmlOutput("graphData"),
      selectInput("graphType", "Choose graph to plot:",
                  list("Please Select a Graph" = "default",
                       "Histogram" = "hist",
                       "Box Plot" = "box",
                        "Scatter Plot" = "scatter"                      
                  )),
      htmlOutput("xVariable"),
      textInput("xLabel","X Label"),
      htmlOutput("yVariable"),
      textInput("yLabel","Y Label"),
      textInput("legendTitle","Legend Title"),
     helpText("Note: The graph only shows the data count only and scatter plots"),submitButton("Execute"),
     HTML("</div>")),
  mainPanel(
   tabsetPanel(
       tabPanel("Graphs",h4("Plot"),h4("Available Data"), plotOutput("plot")) 
)
)

In your renderPlot command your refer to xVariable in the fill and na.omit options. 在你renderPlot命令你指xVariablefillna.omit选项。 You should call it input$xVariable instead. 您应该将其命名为input$xVariable

EDIT 编辑

Based on the error message in the comment, you need to check the reactivity for the code. 根据注释中的错误消息,您需要检查代码的反应性。 In the renderPlot part you have c and plot that are used as names to hold plots based on reactive data. renderPlot部分中,您有cplot作为名称来保存基于反应数据的图。 I have two suggestions: 我有两个建议:

1) Rename these: c and plot are already the names of R functions and using these as names for you graphs could lead to unintended consequences. 1)重命名这些: cplot已经是R函数的名称,并使用这些作为图表的名称可能会导致意想不到的后果。

2) you need to add reactive ({}) wrappers around the lines that make these two things as they are using reactive data and you want them to change as the inputs change. 2)你需要在生成这两件事的行周围添加reactive ({})包装器,因为他们使用反应数据并且你希望它们随着输入的变化而变化。

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

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